fdriving gam in python

Solutions on MaxInterview for fdriving gam in python by the best coders in the world

showing results for - "fdriving gam in python"
Jakob
02 Nov 2020
1import pygame
2import time
3import random
4
5pygame.init()
6
7display_width = 800
8display_height = 600
9
10black = (0,0,0)
11white = (255,255,255)
12red = (255,0,0)
13
14car_width = 73
15
16gameDisplay = pygame.display.set_mode((display_width,display_height))
17pygame.display.set_caption('A bit Racey')
18clock = pygame.time.Clock()
19
20carImg = pygame.image.load('racecar.png')
21
22def things(thingx, thingy, thingw, thingh, color):
23    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
24
25def car(x,y):
26    gameDisplay.blit(carImg,(x,y))
27
28def text_objects(text, font):
29    textSurface = font.render(text, True, black)
30    return textSurface, textSurface.get_rect()
31
32def 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
46def crash():
47    message_display('You Crashed')
48    
49def 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
108game_loop()
109pygame.quit()
110quit()
111
similar questions
queries leading to this page
fdriving gam in python