Skip to content

Commit b00eb08

Browse files
committed
Create part18.py
1 parent cfcc39c commit b00eb08

File tree

1 file changed

+240
-0
lines changed

1 file changed

+240
-0
lines changed

part18.py

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
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 = (200,0,0)
14+
green = (0,200,0)
15+
16+
bright_red = (255,0,0)
17+
bright_green = (0,255,0)
18+
19+
block_color = (53,115,255)
20+
21+
car_width = 73
22+
23+
gameDisplay = pygame.display.set_mode((display_width,display_height))
24+
pygame.display.set_caption('A bit Racey')
25+
clock = pygame.time.Clock()
26+
27+
carImg = pygame.image.load('racecar.png')
28+
29+
pause = False
30+
#crash = True
31+
32+
def things_dodged(count):
33+
font = pygame.font.SysFont("comicsansms", 25)
34+
text = font.render("Dodged: "+str(count), True, black)
35+
gameDisplay.blit(text,(0,0))
36+
37+
def things(thingx, thingy, thingw, thingh, color):
38+
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
39+
40+
def car(x,y):
41+
gameDisplay.blit(carImg,(x,y))
42+
43+
def text_objects(text, font):
44+
textSurface = font.render(text, True, black)
45+
return textSurface, textSurface.get_rect()
46+
47+
def message_display(text):
48+
largeText = pygame.font.SysFont("comicsansms",115)
49+
TextSurf, TextRect = text_objects(text, largeText)
50+
TextRect.center = ((display_width/2),(display_height/2))
51+
gameDisplay.blit(TextSurf, TextRect)
52+
53+
pygame.display.update()
54+
55+
time.sleep(2)
56+
57+
game_loop()
58+
59+
60+
61+
def crash():
62+
63+
64+
largeText = pygame.font.SysFont("comicsansms",115)
65+
TextSurf, TextRect = text_objects("You Crashed", largeText)
66+
TextRect.center = ((display_width/2),(display_height/2))
67+
gameDisplay.blit(TextSurf, TextRect)
68+
69+
70+
while True:
71+
for event in pygame.event.get():
72+
#print(event)
73+
if event.type == pygame.QUIT:
74+
pygame.quit()
75+
quit()
76+
77+
#gameDisplay.fill(white)
78+
79+
80+
button("Play Again",150,450,100,50,green,bright_green,game_loop)
81+
button("Quit",550,450,100,50,red,bright_red,quitgame)
82+
83+
pygame.display.update()
84+
clock.tick(15)
85+
86+
def button(msg,x,y,w,h,ic,ac,action=None):
87+
mouse = pygame.mouse.get_pos()
88+
click = pygame.mouse.get_pressed()
89+
print(click)
90+
if x+w > mouse[0] > x and y+h > mouse[1] > y:
91+
pygame.draw.rect(gameDisplay, ac,(x,y,w,h))
92+
if click[0] == 1 and action != None:
93+
action()
94+
else:
95+
pygame.draw.rect(gameDisplay, ic,(x,y,w,h))
96+
smallText = pygame.font.SysFont("comicsansms",20)
97+
textSurf, textRect = text_objects(msg, smallText)
98+
textRect.center = ( (x+(w/2)), (y+(h/2)) )
99+
gameDisplay.blit(textSurf, textRect)
100+
101+
102+
def quitgame():
103+
pygame.quit()
104+
quit()
105+
106+
def unpause():
107+
global pause
108+
pause = False
109+
110+
111+
def paused():
112+
113+
largeText = pygame.font.SysFont("comicsansms",115)
114+
TextSurf, TextRect = text_objects("Paused", largeText)
115+
TextRect.center = ((display_width/2),(display_height/2))
116+
gameDisplay.blit(TextSurf, TextRect)
117+
118+
119+
while pause:
120+
for event in pygame.event.get():
121+
#print(event)
122+
if event.type == pygame.QUIT:
123+
pygame.quit()
124+
quit()
125+
126+
#gameDisplay.fill(white)
127+
128+
129+
button("Continue",150,450,100,50,green,bright_green,unpause)
130+
button("Quit",550,450,100,50,red,bright_red,quitgame)
131+
132+
pygame.display.update()
133+
clock.tick(15)
134+
135+
136+
def game_intro():
137+
138+
intro = True
139+
140+
while intro:
141+
for event in pygame.event.get():
142+
#print(event)
143+
if event.type == pygame.QUIT:
144+
pygame.quit()
145+
quit()
146+
147+
gameDisplay.fill(white)
148+
largeText = pygame.font.SysFont("comicsansms",115)
149+
TextSurf, TextRect = text_objects("A bit Racey", largeText)
150+
TextRect.center = ((display_width/2),(display_height/2))
151+
gameDisplay.blit(TextSurf, TextRect)
152+
153+
button("GO!",150,450,100,50,green,bright_green,game_loop)
154+
button("Quit",550,450,100,50,red,bright_red,quitgame)
155+
156+
pygame.display.update()
157+
clock.tick(15)
158+
159+
160+
161+
162+
163+
164+
def game_loop():
165+
global pause
166+
167+
x = (display_width * 0.45)
168+
y = (display_height * 0.8)
169+
170+
x_change = 0
171+
172+
thing_startx = random.randrange(0, display_width)
173+
thing_starty = -600
174+
thing_speed = 4
175+
thing_width = 100
176+
thing_height = 100
177+
178+
thingCount = 1
179+
180+
dodged = 0
181+
182+
gameExit = False
183+
184+
while not gameExit:
185+
186+
for event in pygame.event.get():
187+
if event.type == pygame.QUIT:
188+
pygame.quit()
189+
quit()
190+
191+
if event.type == pygame.KEYDOWN:
192+
if event.key == pygame.K_LEFT:
193+
x_change = -5
194+
if event.key == pygame.K_RIGHT:
195+
x_change = 5
196+
if event.key == pygame.K_p:
197+
pause = True
198+
paused()
199+
200+
201+
if event.type == pygame.KEYUP:
202+
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
203+
x_change = 0
204+
205+
x += x_change
206+
gameDisplay.fill(white)
207+
208+
# things(thingx, thingy, thingw, thingh, color)
209+
things(thing_startx, thing_starty, thing_width, thing_height, block_color)
210+
211+
212+
213+
thing_starty += thing_speed
214+
car(x,y)
215+
things_dodged(dodged)
216+
217+
if x > display_width - car_width or x < 0:
218+
crash()
219+
220+
if thing_starty > display_height:
221+
thing_starty = 0 - thing_height
222+
thing_startx = random.randrange(0,display_width)
223+
dodged += 1
224+
thing_speed += 1
225+
thing_width += (dodged * 1.2)
226+
227+
if y < thing_starty+thing_height:
228+
print('y crossover')
229+
230+
if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and x + car_width < thing_startx+thing_width:
231+
print('x crossover')
232+
crash()
233+
234+
pygame.display.update()
235+
clock.tick(60)
236+
237+
game_intro()
238+
game_loop()
239+
pygame.quit()
240+
quit()

0 commit comments

Comments
 (0)