-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
316 lines (250 loc) · 9.93 KB
/
main.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import time
import pygame.event
from pygame.locals import *
from base_classes import *
from enemies import *
from waves import ALL_WAVES
from weapons import PulseShot, HomingMissileLauncher
# Creating colors
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
class MainGame:
def __init__(self):
pygame.init()
self.view_pos_x = 0
self.view_pos_y = 0
self.you_died = None
self.quit_main_game = None
self.title_screen = None
self.paused = False
self.all_sprites_group = None
self.background = None
self.current_wave = None
self.display_surface = None
self.enemy_group = None
self.enemy_types = None
self.font = None
self.font_small = None
self.fps = None
self.framePerSec = None
self.game_over = None
self.next_stage = None
self.player1 = None
self.player_group = None
self.power_up_group = None
self.projectile_group = None
self.score_drops = None
self.screen_height = None
self.screen_width = None
self.spawn_enemy = None
self.spawn_power_up = None
self.enemy_spawn_frequency = 1000
self.next_stage_frequency = 30000 # 30 seconds
self.score = 0
self.orbs = 0
self.stage = 0
self.x_drift = 0.4
self.y_drift = 0
self.setup_display()
self.create_groups()
self.setup_game()
def setup_display(self):
# Setting up FPS
self.fps = 60
self.framePerSec = pygame.time.Clock()
# Other Variables for use in the program
self.screen_width = 500
self.screen_height = 700
# Create a white screen
self.display_surface = pygame.display.set_mode((self.screen_width, self.screen_height))
self.display_surface.fill(BLACK)
# Name the window
pygame.display.set_caption("Ideal Carnival")
# Setting up Fonts
self.font = pygame.font.SysFont("Menlo", 60)
self.font_small = pygame.font.SysFont("Menlo", 30)
# Scores to render
self.score_drops = []
def create_groups(self):
# Creating Sprites Groups
self.enemy_group = pygame.sprite.Group()
self.projectile_group = pygame.sprite.Group()
self.power_up_group = pygame.sprite.Group()
self.player_group = pygame.sprite.Group()
self.all_sprites_group = pygame.sprite.Group()
def setup_game(self):
# Create background
self.background = Background(self)
self.title_screen = TitleScreen(self)
self.you_died = YouDied(self)
# Setting up Sprites
self.player1 = Player(self)
self.player1.add_weapon(PulseShot(self))
self.player1.add_weapon(HomingMissileLauncher(self))
self.all_sprites_group.add(self.player1)
self.spawn_enemy = pygame.USEREVENT + 1
self.go_to_next_stage()
def end_game(self):
self.quit_main_game = True
time.sleep(2)
def go_to_next_stage(self):
if self.stage > len(ALL_WAVES) - 1:
for enemy in self.enemy_group:
if enemy.is_dead is False:
return
print("Ran out of stages! Game over until you get off your arse and make more")
self.end_game()
self.current_wave = ALL_WAVES[self.stage]
print(f"Start Wave No. {self.current_wave.wave_no} - {self.current_wave.wave_text}")
# Spawn Enemy event every n seconds
pygame.time.set_timer(self.spawn_enemy, self.current_wave.spawn_frequency)
self.stage += 1
def start(self):
"""
- START SCREEN -
"""
while True:
for event in pygame.event.get():
pass
# Draw the background
self.title_screen.update()
self.title_screen.render()
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_SPACE]:
break
pygame.display.update()
self.framePerSec.tick(self.fps)
"""
- MAIN GAME -
"""
self.quit_main_game = False
while True:
# Cycles through all events occurring
for event in pygame.event.get():
if event.type == self.spawn_enemy: # TODO: Replace with match case when Python 3.10 lands
# If you've spawned every enemy in the wave, move onto the next
current_wave_length = len(self.current_wave.enemies) - 1
if self.current_wave.current_enemy > current_wave_length:
self.go_to_next_stage()
else:
# Spawn the next enemy
new_enemy = self.current_wave.enemies[self.current_wave.current_enemy](self)
self.enemy_group.add(new_enemy)
if current_wave_length > 0:
new_enemy.dice_sides = current_wave_length
new_enemy.speed *= self.current_wave.enemy_speed_multiplier
self.current_wave.current_enemy += 1
if self.quit_main_game:
break
self.view_pos_x = (self.player1.rect.x - (self.screen_width / 2)) * self.x_drift
self.view_pos_y = (self.player1.rect.y - (self.screen_height / 2)) * self.y_drift
# Draw the background
self.background.update()
self.background.render()
# Draw the scores
scores = self.font_small.render(f"Score: {self.score}", True, WHITE)
self.display_surface.blit(scores, (self.screen_width / 40, self.screen_height / 60))
# Draw the orb count
orbs = self.font_small.render(f"Orbs: {self.orbs}", True, WHITE)
self.display_surface.blit(orbs, (self.screen_width / 40, (self.screen_height / 60) + 40))
# Moves and Re-draws all Sprites
for entity in self.all_sprites_group:
entity.move()
entity.update()
final_pos = pygame.rect.Rect(0, 1, 2, 3)
final_pos.x = entity.rect.x
final_pos.y = entity.rect.y
final_pos.x -= self.view_pos_x
final_pos.y -= self.view_pos_y
#if entity is self.player1 or entity in self.projectile_group:
# self.display_surface.blit(entity.image, entity.rect)
#else:
self.display_surface.blit(entity.image, final_pos)
# If the game is quitting then stop doing stuff
if self.quit_main_game:
print("Quitting")
break
if self.quit_main_game:
print("Quitting")
break
# Render all the score drops
dead_score_drops = []
for score_drop in self.score_drops:
score_drop.update()
if score_drop.age > score_drop.lifespan:
dead_score_drops.append(score_drop)
continue
#score_drop.render()
final_pos = pygame.rect.Rect(0, 1, 2, 3)
final_pos.x = score_drop.parent.rect.x
final_pos.y = score_drop.parent.rect.y
final_pos.x -= self.view_pos_x
final_pos.y -= self.view_pos_y
self.display_surface.blit(score_drop.font_render, final_pos)
# Clean up any old score drops, so they don't keep rendering
for i in reversed(dead_score_drops):
del (i)
pygame.display.update()
self.framePerSec.tick(self.fps)
"""
- END GAME -
"""
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
pass
# Draw the background
self.you_died.update()
self.you_died.render()
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_SPACE]:
pygame.quit()
break
pygame.display.update()
self.framePerSec.tick(self.fps)
class Background:
def __init__(self, main_game):
self.bg_image = pygame.image.load('images/backgrounds/Background.png')
self.rect_bg_img = self.bg_image.get_rect()
self.main_game = main_game
# Position an image, Y2 on top of Y1
self.bg_y1 = 0
self.bg_x1 = self.main_game.view_pos_x
self.bg_y2 = -self.rect_bg_img.height
self.bg_x2 = self.main_game.view_pos_x
self.moving_up_speed = 1
def update(self):
# Scroll the images both down
self.bg_y1 += self.moving_up_speed
self.bg_y2 += self.moving_up_speed
# Once the bottom image is fully off-screen, put it back on top
if self.bg_y1 >= self.rect_bg_img.height:
self.bg_y1 = -self.rect_bg_img.height
if self.bg_y2 >= self.rect_bg_img.height:
self.bg_y2 = -self.rect_bg_img.height
def render(self):
self.main_game.display_surface.blit(self.bg_image, (self.bg_x1, self.bg_y1))
self.main_game.display_surface.blit(self.bg_image, (self.bg_x2, self.bg_y2))
class TitleScreen:
def __init__(self, main_game):
self.main_game = main_game
self.bg_image = pygame.image.load('images/backgrounds/TitleScreen.png')
def update(self):
return
def render(self):
self.main_game.display_surface.blit(self.bg_image, (0, 0))
class YouDied:
def __init__(self, main_game):
self.main_game = main_game
self.bg_image = pygame.image.load('images/backgrounds/YouDied.png')
def update(self):
return
def render(self):
self.main_game.display_surface.blit(self.bg_image, (0, 0))
if __name__ == "__main__":
mainGame = MainGame()
mainGame.start()