Skip to content

Commit 766d4d3

Browse files
committed
Create part8.py
1 parent 3ceaf17 commit 766d4d3

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

part8.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
block_color = (53,115,255)
15+
16+
car_width = 73
17+
18+
gameDisplay = pygame.display.set_mode((display_width,display_height))
19+
pygame.display.set_caption('A bit Racey')
20+
clock = pygame.time.Clock()
21+
22+
carImg = pygame.image.load('racecar.png')
23+
24+
25+
def things_dodged(count):
26+
font = pygame.font.SysFont(None, 25)
27+
text = font.render("Dodged: "+str(count), True, black)
28+
gameDisplay.blit(text,(0,0))
29+
30+
def things(thingx, thingy, thingw, thingh, color):
31+
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
32+
33+
def car(x,y):
34+
gameDisplay.blit(carImg,(x,y))
35+
36+
def text_objects(text, font):
37+
textSurface = font.render(text, True, black)
38+
return textSurface, textSurface.get_rect()
39+
40+
def message_display(text):
41+
largeText = pygame.font.Font('freesansbold.ttf',115)
42+
TextSurf, TextRect = text_objects(text, largeText)
43+
TextRect.center = ((display_width/2),(display_height/2))
44+
gameDisplay.blit(TextSurf, TextRect)
45+
46+
pygame.display.update()
47+
48+
time.sleep(2)
49+
50+
game_loop()
51+
52+
53+
54+
def crash():
55+
message_display('You Crashed')
56+
57+
def game_loop():
58+
x = (display_width * 0.45)
59+
y = (display_height * 0.8)
60+
61+
x_change = 0
62+
63+
thing_startx = random.randrange(0, display_width)
64+
thing_starty = -600
65+
thing_speed = 4
66+
thing_width = 100
67+
thing_height = 100
68+
69+
thingCount = 1
70+
71+
dodged = 0
72+
73+
gameExit = False
74+
75+
while not gameExit:
76+
77+
for event in pygame.event.get():
78+
if event.type == pygame.QUIT:
79+
pygame.quit()
80+
quit()
81+
82+
if event.type == pygame.KEYDOWN:
83+
if event.key == pygame.K_LEFT:
84+
x_change = -5
85+
if event.key == pygame.K_RIGHT:
86+
x_change = 5
87+
88+
if event.type == pygame.KEYUP:
89+
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
90+
x_change = 0
91+
92+
x += x_change
93+
gameDisplay.fill(white)
94+
95+
# things(thingx, thingy, thingw, thingh, color)
96+
things(thing_startx, thing_starty, thing_width, thing_height, block_color)
97+
98+
99+
100+
thing_starty += thing_speed
101+
car(x,y)
102+
things_dodged(dodged)
103+
104+
if x > display_width - car_width or x < 0:
105+
crash()
106+
107+
if thing_starty > display_height:
108+
thing_starty = 0 - thing_height
109+
thing_startx = random.randrange(0,display_width)
110+
dodged += 1
111+
thing_speed += 1
112+
thing_width += (dodged * 1.2)
113+
114+
if y < thing_starty+thing_height:
115+
print('y crossover')
116+
117+
if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and x + car_width < thing_startx+thing_width:
118+
print('x crossover')
119+
crash()
120+
121+
pygame.display.update()
122+
clock.tick(60)
123+
124+
game_loop()
125+
pygame.quit()
126+
quit()

0 commit comments

Comments
 (0)