Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 25c244e

Browse filesBrowse files
Crashing Car Game
Car Games Using Python Pygame Module
1 parent f581e7e commit 25c244e
Copy full SHA for 25c244e

File tree

Expand file treeCollapse file tree

1 file changed

+110
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+110
-0
lines changed
Open diff view settings
Collapse file

‎Car Game in Python‎

Copy file name to clipboard
+110Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.