Skip to content

Commit f329ed7

Browse files
committed
Create part14.py
1 parent f0e54bb commit f329ed7

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

part14.py

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

0 commit comments

Comments
 (0)