-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.py
More file actions
135 lines (100 loc) · 3.43 KB
/
main.py
File metadata and controls
135 lines (100 loc) · 3.43 KB
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
# -*- coding: utf-8 -*-
"""
CopyLeft 2021 Michael Rouves
This file is part of Pygame-DoodleJump.
Pygame-DoodleJump is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Pygame-DoodleJump is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Pygame-DoodleJump. If not, see <https://www.gnu.org/licenses/>.
"""
import pygame, sys
from singleton import Singleton
from camera import Camera
from player import Player
from level import Level
import settings as config
class Game(Singleton):
"""
A class to represent the game.
used to manage game updates, draw calls and user input events.
Can be access via Singleton: Game.instance .
(Check Singleton design pattern for more info)
"""
# constructor called on new instance: Game()
def __init__(self) -> None:
# ============= Initialisation =============
self.__alive = True
# Window / Render
self.window = pygame.display.set_mode(config.DISPLAY,config.FLAGS)
self.clock = pygame.time.Clock()
# Instances
self.camera = Camera()
self.lvl = Level()
self.player = Player(
config.HALF_XWIN - config.PLAYER_SIZE[0]/2,# X POS
config.HALF_YWIN + config.HALF_YWIN/2,# Y POS
*config.PLAYER_SIZE,# SIZE
config.PLAYER_COLOR# COLOR
)
# User Interface
self.score = 0
self.score_txt = config.SMALL_FONT.render("0 m",1,config.GRAY)
self.score_pos = pygame.math.Vector2(10,10)
self.gameover_txt = config.LARGE_FONT.render("Game Over",1,config.GRAY)
self.gameover_rect = self.gameover_txt.get_rect(
center=(config.HALF_XWIN,config.HALF_YWIN))
def close(self):
self.__alive = False
def reset(self):
self.camera.reset()
self.lvl.reset()
self.player.reset()
def _event_loop(self):
# ---------- User Events ----------
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.close()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.close()
if event.key == pygame.K_RETURN and self.player.dead:
self.reset()
self.player.handle_event(event)
def _update_loop(self):
# ----------- Update -----------
self.player.update()
self.lvl.update()
if not self.player.dead:
self.camera.update(self.player.rect)
#calculate score and update UI txt
self.score=-self.camera.state.y//50
self.score_txt = config.SMALL_FONT.render(
str(self.score)+" m", 1, config.GRAY)
def _render_loop(self):
# ----------- Display -----------
self.window.fill(config.WHITE)
self.lvl.draw(self.window)
self.player.draw(self.window)
# User Interface
if self.player.dead:
self.window.blit(self.gameover_txt,self.gameover_rect)# gameover txt
self.window.blit(self.score_txt, self.score_pos)# score txt
pygame.display.update()# window update
self.clock.tick(config.FPS)# max loop/s
def run(self):
# ============= MAIN GAME LOOP =============
while self.__alive:
self._event_loop()
self._update_loop()
self._render_loop()
pygame.quit()
if __name__ == "__main__":
# ============= PROGRAM STARTS HERE =============
game = Game()
game.run()