|
| 1 | +""" |
| 2 | +adding sounds: https://www.youtube.com/audiolibrary/music |
| 3 | +
|
| 4 | +
|
| 5 | +jazz in paris |
| 6 | +
|
| 7 | +
|
| 8 | +crash.. |
| 9 | +
|
| 10 | +
|
| 11 | +""" |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +import pygame |
| 16 | +import time |
| 17 | +import random |
| 18 | + |
| 19 | +pygame.init() |
| 20 | + |
| 21 | +############# |
| 22 | +crash_sound = pygame.mixer.Sound("crash.wav") |
| 23 | + |
| 24 | + |
| 25 | +############# |
| 26 | + |
| 27 | +display_width = 800 |
| 28 | +display_height = 600 |
| 29 | + |
| 30 | +black = (0,0,0) |
| 31 | +white = (255,255,255) |
| 32 | + |
| 33 | +red = (200,0,0) |
| 34 | +green = (0,200,0) |
| 35 | + |
| 36 | +bright_red = (255,0,0) |
| 37 | +bright_green = (0,255,0) |
| 38 | + |
| 39 | +block_color = (53,115,255) |
| 40 | + |
| 41 | +car_width = 73 |
| 42 | + |
| 43 | +gameDisplay = pygame.display.set_mode((display_width,display_height)) |
| 44 | +pygame.display.set_caption('A bit Racey') |
| 45 | +clock = pygame.time.Clock() |
| 46 | + |
| 47 | +carImg = pygame.image.load('racecar.png') |
| 48 | +gameIcon = pygame.image.load('carIcon.png') |
| 49 | + |
| 50 | +pygame.display.set_icon(gameIcon) |
| 51 | + |
| 52 | +pause = False |
| 53 | +#crash = True |
| 54 | + |
| 55 | +def things_dodged(count): |
| 56 | + font = pygame.font.SysFont("comicsansms", 25) |
| 57 | + text = font.render("Dodged: "+str(count), True, black) |
| 58 | + gameDisplay.blit(text,(0,0)) |
| 59 | + |
| 60 | +def things(thingx, thingy, thingw, thingh, color): |
| 61 | + pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh]) |
| 62 | + |
| 63 | +def car(x,y): |
| 64 | + gameDisplay.blit(carImg,(x,y)) |
| 65 | + |
| 66 | +def text_objects(text, font): |
| 67 | + textSurface = font.render(text, True, black) |
| 68 | + return textSurface, textSurface.get_rect() |
| 69 | + |
| 70 | + |
| 71 | +def crash(): |
| 72 | + #################################### |
| 73 | + pygame.mixer.Sound.play(crash_sound) |
| 74 | + pygame.mixer.music.stop() |
| 75 | + #################################### |
| 76 | + largeText = pygame.font.SysFont("comicsansms",115) |
| 77 | + TextSurf, TextRect = text_objects("You Crashed", largeText) |
| 78 | + TextRect.center = ((display_width/2),(display_height/2)) |
| 79 | + gameDisplay.blit(TextSurf, TextRect) |
| 80 | + |
| 81 | + |
| 82 | + while True: |
| 83 | + for event in pygame.event.get(): |
| 84 | + if event.type == pygame.QUIT: |
| 85 | + pygame.quit() |
| 86 | + quit() |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | + button("Play Again",150,450,100,50,green,bright_green,game_loop) |
| 91 | + button("Quit",550,450,100,50,red,bright_red,quitgame) |
| 92 | + |
| 93 | + pygame.display.update() |
| 94 | + clock.tick(15) |
| 95 | + |
| 96 | +def button(msg,x,y,w,h,ic,ac,action=None): |
| 97 | + mouse = pygame.mouse.get_pos() |
| 98 | + click = pygame.mouse.get_pressed() |
| 99 | + |
| 100 | + if x+w > mouse[0] > x and y+h > mouse[1] > y: |
| 101 | + pygame.draw.rect(gameDisplay, ac,(x,y,w,h)) |
| 102 | + if click[0] == 1 and action != None: |
| 103 | + action() |
| 104 | + else: |
| 105 | + pygame.draw.rect(gameDisplay, ic,(x,y,w,h)) |
| 106 | + smallText = pygame.font.SysFont("comicsansms",20) |
| 107 | + textSurf, textRect = text_objects(msg, smallText) |
| 108 | + textRect.center = ( (x+(w/2)), (y+(h/2)) ) |
| 109 | + gameDisplay.blit(textSurf, textRect) |
| 110 | + |
| 111 | + |
| 112 | +def quitgame(): |
| 113 | + pygame.quit() |
| 114 | + quit() |
| 115 | + |
| 116 | +def unpause(): |
| 117 | + global pause |
| 118 | + pygame.mixer.music.unpause() |
| 119 | + pause = False |
| 120 | + |
| 121 | + |
| 122 | +def paused(): |
| 123 | + ############ |
| 124 | + pygame.mixer.music.pause() |
| 125 | + ############# |
| 126 | + largeText = pygame.font.SysFont("comicsansms",115) |
| 127 | + TextSurf, TextRect = text_objects("Paused", largeText) |
| 128 | + TextRect.center = ((display_width/2),(display_height/2)) |
| 129 | + gameDisplay.blit(TextSurf, TextRect) |
| 130 | + |
| 131 | + |
| 132 | + while pause: |
| 133 | + for event in pygame.event.get(): |
| 134 | + if event.type == pygame.QUIT: |
| 135 | + pygame.quit() |
| 136 | + quit() |
| 137 | + |
| 138 | + |
| 139 | + button("Continue",150,450,100,50,green,bright_green,unpause) |
| 140 | + button("Quit",550,450,100,50,red,bright_red,quitgame) |
| 141 | + |
| 142 | + pygame.display.update() |
| 143 | + clock.tick(15) |
| 144 | + |
| 145 | + |
| 146 | +def game_intro(): |
| 147 | + |
| 148 | + intro = True |
| 149 | + |
| 150 | + while intro: |
| 151 | + for event in pygame.event.get(): |
| 152 | + #print(event) |
| 153 | + if event.type == pygame.QUIT: |
| 154 | + pygame.quit() |
| 155 | + quit() |
| 156 | + |
| 157 | + gameDisplay.fill(white) |
| 158 | + largeText = pygame.font.SysFont("comicsansms",115) |
| 159 | + TextSurf, TextRect = text_objects("A bit Racey", largeText) |
| 160 | + TextRect.center = ((display_width/2),(display_height/2)) |
| 161 | + gameDisplay.blit(TextSurf, TextRect) |
| 162 | + |
| 163 | + button("GO!",150,450,100,50,green,bright_green,game_loop) |
| 164 | + button("Quit",550,450,100,50,red,bright_red,quitgame) |
| 165 | + |
| 166 | + pygame.display.update() |
| 167 | + clock.tick(15) |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | +def game_loop(): |
| 175 | + global pause |
| 176 | + ############ |
| 177 | + pygame.mixer.music.load('jazz.wav') |
| 178 | + pygame.mixer.music.play(-1) |
| 179 | + ############ |
| 180 | + x = (display_width * 0.45) |
| 181 | + y = (display_height * 0.8) |
| 182 | + |
| 183 | + x_change = 0 |
| 184 | + |
| 185 | + thing_startx = random.randrange(0, display_width) |
| 186 | + thing_starty = -600 |
| 187 | + thing_speed = 4 |
| 188 | + thing_width = 100 |
| 189 | + thing_height = 100 |
| 190 | + |
| 191 | + thingCount = 1 |
| 192 | + |
| 193 | + dodged = 0 |
| 194 | + |
| 195 | + gameExit = False |
| 196 | + |
| 197 | + while not gameExit: |
| 198 | + |
| 199 | + for event in pygame.event.get(): |
| 200 | + if event.type == pygame.QUIT: |
| 201 | + pygame.quit() |
| 202 | + quit() |
| 203 | + |
| 204 | + if event.type == pygame.KEYDOWN: |
| 205 | + if event.key == pygame.K_LEFT: |
| 206 | + x_change = -5 |
| 207 | + if event.key == pygame.K_RIGHT: |
| 208 | + x_change = 5 |
| 209 | + if event.key == pygame.K_p: |
| 210 | + pause = True |
| 211 | + paused() |
| 212 | + |
| 213 | + |
| 214 | + if event.type == pygame.KEYUP: |
| 215 | + if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: |
| 216 | + x_change = 0 |
| 217 | + |
| 218 | + x += x_change |
| 219 | + gameDisplay.fill(white) |
| 220 | + |
| 221 | + things(thing_startx, thing_starty, thing_width, thing_height, block_color) |
| 222 | + |
| 223 | + |
| 224 | + |
| 225 | + thing_starty += thing_speed |
| 226 | + car(x,y) |
| 227 | + things_dodged(dodged) |
| 228 | + |
| 229 | + if x > display_width - car_width or x < 0: |
| 230 | + crash() |
| 231 | + |
| 232 | + if thing_starty > display_height: |
| 233 | + thing_starty = 0 - thing_height |
| 234 | + thing_startx = random.randrange(0,display_width) |
| 235 | + dodged += 1 |
| 236 | + thing_speed += 1 |
| 237 | + thing_width += (dodged * 1.2) |
| 238 | + |
| 239 | + if y < thing_starty+thing_height: |
| 240 | + print('y crossover') |
| 241 | + |
| 242 | + if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and x + car_width < thing_startx+thing_width: |
| 243 | + print('x crossover') |
| 244 | + crash() |
| 245 | + |
| 246 | + pygame.display.update() |
| 247 | + clock.tick(60) |
| 248 | + |
| 249 | +game_intro() |
| 250 | +game_loop() |
| 251 | +pygame.quit() |
| 252 | +quit() |
0 commit comments