Skip to content

Commit 9cff335

Browse files
committed
Create part15.py
1 parent f329ed7 commit 9cff335

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

part15.py

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

0 commit comments

Comments
 (0)