|
| 1 | +import pygame |
| 2 | +import time |
| 3 | +import random |
| 4 | + |
| 5 | +pygame.init() |
| 6 | + |
| 7 | +display_width = 800 |
| 8 | +display_height = 600 |
| 9 | + |
| 10 | +black = (0,0,0) |
| 11 | +white = (255,255,255) |
| 12 | +red = (255,0,0) |
| 13 | + |
| 14 | +car_width = 73 |
| 15 | + |
| 16 | +gameDisplay = pygame.display.set_mode((display_width,display_height)) |
| 17 | +pygame.display.set_caption('A bit Racey') |
| 18 | +clock = pygame.time.Clock() |
| 19 | + |
| 20 | +carImg = pygame.image.load('racecar.png') |
| 21 | + |
| 22 | +def things(thingx, thingy, thingw, thingh, color): |
| 23 | + pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh]) |
| 24 | + |
| 25 | +def car(x,y): |
| 26 | + gameDisplay.blit(carImg,(x,y)) |
| 27 | + |
| 28 | +def text_objects(text, font): |
| 29 | + textSurface = font.render(text, True, black) |
| 30 | + return textSurface, textSurface.get_rect() |
| 31 | + |
| 32 | +def message_display(text): |
| 33 | + largeText = pygame.font.Font('freesansbold.ttf',115) |
| 34 | + TextSurf, TextRect = text_objects(text, largeText) |
| 35 | + TextRect.center = ((display_width/2),(display_height/2)) |
| 36 | + gameDisplay.blit(TextSurf, TextRect) |
| 37 | + |
| 38 | + pygame.display.update() |
| 39 | + |
| 40 | + time.sleep(2) |
| 41 | + |
| 42 | + game_loop() |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +def crash(): |
| 47 | + message_display('You Crashed') |
| 48 | + |
| 49 | +def game_loop(): |
| 50 | + x = (display_width * 0.45) |
| 51 | + y = (display_height * 0.8) |
| 52 | + |
| 53 | + x_change = 0 |
| 54 | + |
| 55 | + thing_startx = random.randrange(0, display_width) |
| 56 | + thing_starty = -600 |
| 57 | + thing_speed = 7 |
| 58 | + thing_width = 100 |
| 59 | + thing_height = 100 |
| 60 | + |
| 61 | + gameExit = False |
| 62 | + |
| 63 | + while not gameExit: |
| 64 | + |
| 65 | + for event in pygame.event.get(): |
| 66 | + if event.type == pygame.QUIT: |
| 67 | + pygame.quit() |
| 68 | + quit() |
| 69 | + |
| 70 | + if event.type == pygame.KEYDOWN: |
| 71 | + if event.key == pygame.K_LEFT: |
| 72 | + x_change = -5 |
| 73 | + if event.key == pygame.K_RIGHT: |
| 74 | + x_change = 5 |
| 75 | + |
| 76 | + if event.type == pygame.KEYUP: |
| 77 | + if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: |
| 78 | + x_change = 0 |
| 79 | + |
| 80 | + x += x_change |
| 81 | + gameDisplay.fill(white) |
| 82 | + |
| 83 | + # things(thingx, thingy, thingw, thingh, color) |
| 84 | + things(thing_startx, thing_starty, thing_width, thing_height, black) |
| 85 | + thing_starty += thing_speed |
| 86 | + car(x,y) |
| 87 | + |
| 88 | + if x > display_width - car_width or x < 0: |
| 89 | + crash() |
| 90 | + |
| 91 | + if thing_starty > display_height: |
| 92 | + thing_starty = 0 - thing_height |
| 93 | + thing_startx = random.randrange(0,display_width) |
| 94 | + |
| 95 | + #### |
| 96 | + if y < thing_starty+thing_height: |
| 97 | + print('y crossover') |
| 98 | + |
| 99 | + if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and x + car_width < thing_startx+thing_width: |
| 100 | + print('x crossover') |
| 101 | + crash() |
| 102 | + #### |
| 103 | + |
| 104 | + pygame.display.update() |
| 105 | + clock.tick(60) |
| 106 | + |
| 107 | + |
| 108 | +game_loop() |
| 109 | +pygame.quit() |
| 110 | +quit() |
0 commit comments