-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_game.py
258 lines (211 loc) · 7.57 KB
/
my_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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import pygame
import random
import time
import sys
pygame.init()
display_width = 1366
display_height = 768
gameDisplay = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Avoid the rocks!')
clock = pygame.time.Clock()
playerImg = pygame.image.load("player.png")
fireballImg = pygame.image.load("fireball.png")
display_color = (255, 255, 255)
class Player(object):
x = 0
y = 0
x_speed = 0
y_speed = 0
speed_bonus = 0
width = 40
height = 40
def __init__(self, x, y):
self.x = x
self.y = y
def update(self):
if self.x_speed > 0:
self.x_speed += self.speed_bonus
elif self.x_speed < 0:
self.x_speed -= self.speed_bonus
if self.y_speed > 0:
self.y_speed += self.speed_bonus
elif self.y_speed < 0:
self.y_speed -= self.speed_bonus
self.x += self.x_speed
self.y += self.y_speed
gameDisplay.blit(playerImg, (self.x, self.y))
def left_bound(self):
if self.x <= 0:
self.x = 0;
self.x_speed = self.x_speed * -1
def right_bound(self):
if self.x > display_width - self.width:
self.x = display_width - self.width
self.x_speed = self.x_speed * -1
def top_bound(self):
if self.y <= 0:
self.y = 0
self.y_speed = self.y_speed * -1
def bottom_bound(self):
if self.y >= display_height - self.height:
self.y = display_height - self.height
self.y_speed = self.y_speed * -1
def bound(self):
self.left_bound()
self.right_bound()
self.top_bound()
self.bottom_bound()
def rectangle(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
class Fireball(object):
x = 0
y = 0
x_speed = 0
y_speed = 0
width = 40
height = 40
has_reached_limit = False #This will let us know if it can de-spawn
side = 0
def __init__(self):
self.side = random.randint(1,4)
# Where does the fireball spawn?
# 1 - left
# 2 - top
# 3 - right
# 4 - bottom
if self.side == 1:
self.x = -60 # get to the left of the window
self.y = random.randint(0, display_height-self.height)
self.x_speed = 10
elif self.side == 2:
self.x = random.randint(0, display_width-self.width)
self.y = -60
self.y_speed = 10
elif self.side == 3:
self.x = display_width + 60
self.y = random.randint(0, display_height-self.height)
self.x_speed = -10
elif self.side == 4:
self.x = random.randint(0, display_width-self.width)
self.y = display_height + 60
self.y_speed = -10
def update(self):
self.x += self.x_speed
self.y += self.y_speed
gameDisplay.blit(fireballImg, (self.x, self.y))
if self.side == 1 and self.x > display_width:
self.has_reached_limit = True
if self.side == 2 and self.y > display_height:
self.has_reached_limit = True
if self.side == 3 and self.x < -40:
self.has_reached_limit = True
if self.side == 4 and self.y < -40:
self.has_reached_limit = True
def rectangle(self):
return pygame.Rect(self.x, self.y, self.width, self.height)
def game_loop():
player = Player(display_width/2, display_height/2)
fireballs = []
difficulty = 1.0
score = 0
global display_color
gameDisplay.fill(display_color)
player.update()
pygame.display.update()
base_time = time.time()
current_time = time.time()
elapsed_time = 0
alive = True
while alive:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
player.x_speed = 2
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
player.x_speed = -2
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
player.y_speed = 2
if event.key == pygame.K_UP or event.key == pygame.K_w:
player.y_speed = -2
if event.type == pygame.KEYUP:
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
player.x_speed = 0
if event.key == pygame.K_d or event.key == pygame.K_a:
player.x_speed = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
player.y_speed = 0
if event.key == pygame.K_w or event.key == pygame.K_s:
player.y_speed = 0
current_time = time.time()
elapsed_time = current_time - base_time
if elapsed_time >= (2 - difficulty/10):
display_color = random_color()
base_time = time.time()
gameDisplay.fill(display_color)
player.bound()
player.update()
if len(fireballs) < difficulty:
fireballs.append(Fireball())
for index, fireball in enumerate(fireballs):
fireball.update()
if fireball.rectangle().colliderect(player.rectangle()):
death_screen(score)
if fireball.has_reached_limit:
fireballs.pop(index)
score += 1
difficulty += 0.1
player.speed_bonus += 0.01
print score
print player.speed_bonus
pygame.display.update()
clock.tick(50)
def main_screen():
gameDisplay.fill((0,0,0))
messages = ["Let's Play!",
"Ready, set, don't...",
"Ready?",
"Ready to lose?",
"Oh, not you again...",
"Watch out!",
"Don't drop the soap!",
"Warning: Rage Material!",
"Future Score: -100!",
"It's over 9000 losses!"]
message = messages[random.randint(0, len(messages) - 1)]
text = pygame.font.Font('freesansbold.ttf', 60)
text_on_screen = text.render(message, True, (255, 255, 255))
text_rect = text_on_screen.get_rect()
text_rect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(text_on_screen, text_rect)
pygame.display.update()
time.sleep(2)
game_loop()
def death_screen(score):
gameDisplay.fill((0,0,0))
text = pygame.font.Font('freesansbold.ttf', 60)
messages = ['Pls, leave...',
'Stop Losing!',
'Is it over yet?',
'U dead bruh!',
'Is that all?',
'Try harder...',
'Pls, stop, it hurts!']
message = messages[random.randint(0,(len(messages)-1))]
message_on_screen = text.render(message, True, (255, 255, 255))
score_message = "Score: {}".format(score)
score_on_screen = text.render(score_message, True, (255, 255, 255))
message_rect = message_on_screen.get_rect()
message_rect.center = ((display_width/2),(display_height/2 + 40))
gameDisplay.blit(message_on_screen, message_rect)
score_rect = score_on_screen.get_rect()
score_rect.center = ((display_width/2), (display_height/2 - 40))
gameDisplay.blit(score_on_screen, score_rect)
pygame.display.update()
time.sleep(2)
main_screen()
def random_color():
return (random.randint(0, 255),random.randint(0, 255),
random.randint(0, 255))
main_screen()