Skip to content

Commit c2359a8

Browse files
committed
Create part19.py
1 parent b00eb08 commit c2359a8

File tree

1 file changed

+243
-0
lines changed

1 file changed

+243
-0
lines changed

part19.py

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

0 commit comments

Comments
 (0)