-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
242 lines (176 loc) · 6.35 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import pygame
import time
import random
pygame.init()
crash_sound = pygame.mixer.Sound("crash.wav")
white = (255,255,255)
black = (0,0,0)
red = (200,0,0)
blue = (0,0,200)
green = (0,200,0)
light_red = (150,0,0)
light_green = (0,150,0)
high_score = [0]
display_width = 800
display_height = 600
car_width = 73
game_display = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("A Bit Racey")
clock = pygame.time.Clock()
carImg = pygame.image.load('racecar.png')
pause = False
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(game_display,color,[thingx,thingy,thingw, thingh])
def car(x,y):
game_display.blit(carImg,(x,y))
def crash():
pygame.mixer.music.stop()
pygame.mixer.Sound.play(crash_sound)
crash_font = pygame.font.SysFont("comicsansms",115)
crash_text = crash_font.render("You Crashed!",True,black)
game_display.blit(crash_text,((display_width/4 - 150), (display_height/4 + 100)))
while True:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
#button(x,y,w,h,ic,ac, action = None)
button("Play Again",150,450,160,50,green,light_green,game_loop)
button("Quit",550,450,100,50,red,light_red,quit_game)
print_highscore()
pygame.display.update()
def score(count):
font = pygame.font.SysFont("comicsansms",25)
text = font.render("Score: " + str(count), True, black)
game_display.blit(text, (1,1))
def get_highscore():
high_score = 0
high_score_file = open('score.txt','r')
high_score = int(high_score_file.read())
high_score_file.close()
return high_score
def save_highscore(count):
high_score_file = open("score.txt","w")
high_score_file.write(str(count))
high_score_file.close()
def print_highscore():
font = pygame.font.SysFont("comicsansms",25)
text = font.render("High Score: " + str(get_highscore()), True, black)
game_display.blit(text, (1,25))
def quit_game():
pygame.quit()
quit()
def button(msg,x,y,w,h,ic,ac,action = None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(game_display, ic,(x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(game_display, ac,(x,y,w,h))
b_font = pygame.font.SysFont("comicsansms",20)
b_text = b_font.render(msg,True,black)
game_display.blit(b_text,((x+w/4),(y + h/4)))
pygame.display.update()
def unpaused():
global pause
pause = False
pygame.mixer.music.unpause()
def paused():
#game_display.fill(white)
while pause:
pygame.mixer.music.pause()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pause_font = pygame.font.SysFont("comicsansms",115)
pause_text = pause_font.render("Paused",True,black)
game_display.blit(pause_text,((display_width/4 ), (display_height/4 + 50)))
get_highscore()
#button(x,y,w,h,ic,ac, action = None)
button("Continue",150,450,150,50,green,light_green,unpaused)
button("Quit",550,450,120,50,red,light_red,quit_game)
pygame.display.update()
clock.tick(15)
def intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
game_display.fill(white)
intro_font = pygame.font.SysFont("comicsansms",115)
intro_text = intro_font.render("A Bit Racey",True,black)
game_display.blit(intro_text,((display_width/4 - 100), (display_height/4 + 50)))
#button(x,y,w,h,ic,ac, action = None)
button("Go!!",150,450,100,50,green,light_green,game_loop)
button("Quit",550,450,100,50,red,light_red,quit_game)
print_highscore()
pygame.display.update()
clock.tick(15)
def game_loop():
pygame.mixer.music.load('music2.wav')
pygame.mixer.music.play(-1)
game_exit = False
x = (display_width / 2)
y = (display_height * 0.8)
x_change = 0
thing_startx = random.randrange(0, display_width)
thing_starty = -600
thing_speed = 7
thing_width = 100
thing_height = 100
block_color = (random.randrange(1,255),random.randrange(1,255),random.randrange(1,255))
count = 0
high_score = get_highscore()
while not game_exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
if event.key == pygame.K_RIGHT:
x_change = 5
if event.key == pygame.K_p:
global pause
pause = True
paused()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
x += x_change
game_display.fill(white)
# def things(thingx, thingy, thingw, thingh, color):
things(thing_startx, thing_starty, thing_width,thing_height,block_color)
thing_starty += thing_speed
car(x,y)
score(count)
crash_text = False
if x > display_width - car_width or x < 0:
crash()
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(0,display_width)
block_color = (random.randrange(1,255),random.randrange(1,255),random.randrange(1,255))
count += 1
if count > 10:
thing_speed += 0.5
thing_width += 1.5
if y < thing_starty+thing_height:
if x > thing_startx and x < thing_startx + thing_width or x+car_width > thing_startx and x + car_width < thing_startx+thing_width:
if count > high_score:
save_highscore(count)
crash()
print_highscore()
pygame.display.update()
clock.tick(60)
intro()
game_loop()
pygame.quit()
quit()