Skip to content

Commit 3f6d085

Browse files
committed
Create part11.py
1 parent de442c2 commit 3f6d085

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

part11.py

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

0 commit comments

Comments
 (0)