-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
119 lines (83 loc) · 3 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
from pygame import QUIT, display, event, time
from source.core.game import Game
from source.core.player import Player
from source.core.sound import Sound
from source.core.updater import Updater
from source.level.world import World
from source.screen.color import Color
from source.screen.hotbar import Hotbar
from source.screen.shader import Shader
from source.screen.startmenu import StartMenu
from source.utils.constants import GAME_TICKS
from source.utils.saveload import Saveload
def main() -> None:
Game.initialize()
Sound.initialize()
player = Player()
world = World(player)
updater = Updater(world, player)
title = StartMenu(world, Game.font)
hotbar = Hotbar(player, Game.font)
shader = Shader()
# You might be wondering, why complicate things with a complex main loop when
# Pygame makes it so much simpler and faster? The answer is straightforward
# and comes down to two reasons:
#
# - While a basic Pygame main loop is easy to implement, it tends to consume
# an excessive and valuable amount of CPU resources. I'm not exaggerating,
# even when limiting the game to 30 FPS, it's still inefficient
#
# - Lastly, I'm accustomed to using the original main loop design from Minicraft
# on Java, so it's a method I'm familiar and comfortable with
clock = time.Clock()
timer = time.get_ticks()
delta = 0.00
this_time: int = time.get_ticks()
last_time: int = time.get_ticks()
frame_time = 1000 // GAME_TICKS
running: bool = True
drawing: bool = False
while running:
this_time = time.get_ticks()
delta += this_time - last_time
last_time = this_time
drawing = False
# GAME LOGIC UPDATE
while delta >= frame_time:
for _ in event.get(QUIT):
running = False
if world.loaded:
updater.update()
hotbar.update()
else:
title.update()
delta -= frame_time
drawing = True
# SCREEN UPDATE
if drawing:
if Game.debug:
render_start = time.get_ticks()
# Clear the screen improve the performance
Game.buffer.fill(Color.BLACK)
if world.loaded:
world.render(Game.buffer)
hotbar.render(Game.buffer)
else:
title.render(Game.buffer)
shader.render(Game.buffer)
display.flip()
if Game.debug:
render_time = time.get_ticks() - render_start
clock.tick_busy_loop(GAME_TICKS)
# DEBUG ...
if Game.debug and (time.get_ticks() - timer) >= 1000:
if drawing:
print(f"> render time: {render_time} ms")
timer = time.get_ticks()
# This prevents corrupted save files in case the game is closed
if world.loaded:
Saveload.save(updater, world, player)
Sound.quit()
Game.quit()
if __name__ == "__main__":
main()