-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
118 lines (96 loc) · 3.13 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
import pygame as pg
import random
import core
class Game:
def __init__(self):
self.windowSize = (800, 800)
self.renderScale = (400, 400)
self.title = 'They are falling from above!'
self.FPS = 60
self.enemies = []
self.enemySpawnCooldown = 0
# initialize window
self.window = pg.display.set_mode(self.windowSize)
self.frame_surface = pg.Surface(self.renderScale)
self.clock = pg.time.Clock()
self.core = core
self.level_generator = self.core.LevelGenerator()
# map selection
self.level_map = self.level_generator.new()
self.tiles = []
self.targetPositions = []
self.loadLevel()
self.player = self.core.Player()
self.score_board = self.core.ScoreBoard(self.player)
self.input_handler = self.core.InputHandler()
pg.display.set_caption(self.title)
def loadLevel(self):
tile_size = self.core.constants.TILE_SIZE
for y, line in enumerate(self.level_map):
for x, char in enumerate(line):
if char != ' ' and char != '.':
self.tiles.append(self.core.Tile((x * tile_size, y * tile_size),))
elif char == '.':
self.targetPositions.append([x * tile_size, y * tile_size],)
def onStart(self):
self.camera = self.core.Camera(self.player)
self.background = self.core.Background()
self.target = self.core.Target(random.choice(self.targetPositions))
self.score_board.update()
pg.mixer.music.load('assets/music/music.ogg')
pg.mixer.music.set_volume(0.2)
pg.mixer.music.play(-1)
def run(self):
self.onStart()
self.running = True
while self.running:
for event in pg.event.get():
if event.type == pg.QUIT:
self.running = False
self.update()
self.render()
def updateEnemies(self):
toRemove = []
for enemy in self.enemies:
enemy.update(self.tiles, self.player)
if enemy.deathAnim < 1:
toRemove.append(enemy)
for enemy in toRemove:
self.enemies.remove(enemy)
if self.enemySpawnCooldown > 32 and not self.player.dead:
self.enemies.append(self.core.enemy())
self.enemySpawnCooldown = 0
if not self.player.dead:
self.enemySpawnCooldown += 1
def render(self):
# clear frame
self.frame_surface.fill((0, 0, 0))
self.background.render(self.frame_surface)
# level & enemies
for tile in self.tiles:
tile.render(self.frame_surface, self.camera)
for enemy in self.enemies:
enemy.render(self.frame_surface, self.camera)
self.target.render(self.frame_surface, self.camera)
self.player.render(self.frame_surface, self.camera)
self.score_board.render(self.frame_surface)
# update window
self.window.blit(pg.transform.scale(self.frame_surface, self.windowSize), (0, 0))
self.clock.tick(self.FPS)
pg.display.flip()
def update(self):
self.camera.update()
player_inputs = self.input_handler.get_player_inputs()
self.player.update(self.tiles, player_inputs)
self.updateEnemies()
self.target.update(self.player, self.targetPositions, self.score_board.update, self)
if self.player.dead:
self.score_board.update()
self.core.writeToJSON('score.save', {'score': self.score_board.high_score})
if self.player.pos[1] > 400:
self.player.dead = True
if __name__ == '__main__':
pg.mixer.init()
pg.font.init()
pg.init()
Game().run()