Skip to content

Commit c094c83

Browse files
committed
Create part12.py
1 parent 3f6d085 commit c094c83

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed

part12.py

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

0 commit comments

Comments
 (0)