-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
230 lines (201 loc) · 7.02 KB
/
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
import pygame
import pygame_gui
from pygame.time import Clock
from state_manager import GameStateManager
from states.start_menu import StartMenu
from states.turn_based_fight_state import TurnBasedFight
from states.character_selection_menu import CharacterSelectionMenu
from states.level_selection_menu import LevelSelectionMenu
from states.quest_menu import QuestMenu
from states.end_menu import EndMenu
from characters.players.mage import Mage
from characters.players.ranger import Ranger
from characters.players.warrior import Warrior
from characters.players.berserker import Berserker
from characters.enemies.dreadnought import DreadNought
from characters.enemies.devourer import Devourer
from characters.enemies.enigma import Enigma
from characters.enemies.base_enemy import BaseEnemy
from characters.players.base_player import BasePlayer
from xp import XP
from quest import Quest
from utilities.json_utility import write_default_if_not_exist, read_json
class Game:
"""
Game class to initialize and run the game.
"""
__game_state_manager: GameStateManager = None
__clock: Clock = None
__running: bool = True
def __init__(self) -> None:
"""
Initializes the game, sets up pygame, UI, and game states.
"""
# Initialize pygame modules
pygame.init()
pygame.font.init()
pygame.mixer.init()
# Set the display mode with a screen resolution of 1280x720
screen = pygame.display.set_mode((1280, 720))
# Initialize the UI manager with the same screen resolution
ui_manager = pygame_gui.UIManager((1280, 720))
# Load various UI themes from JSON files
ui_manager.get_theme().load_theme("settings/general.json")
ui_manager.get_theme().load_theme("settings/character_selection_theme.json")
ui_manager.get_theme().load_theme("settings/level_selection_theme.json")
ui_manager.get_theme().load_theme("settings/combat_theme.json")
ui_manager.get_theme().load_theme("settings/health_bar.json")
# Default user data for initialization
default_user_data = {
"xp": 5000,
"quest_progress": {
"Fireball": 0,
"Kill DreadNoughts": 0,
},
"quest_claimed": {
"Fireball": False,
"Kill DreadNoughts": False,
},
"character_level": {
"Warrior": 1,
"Mage": 1,
"Berserker": 1,
"Ranger": 1,
},
"character_abilities": {
"Warrior": ["Power Slash"],
"Mage": ["Fireball"],
"Berserker": ["Reckless Charge"],
"Ranger": ["Arrow Barrage"],
},
}
# Write the default user data to file if it doesn't already exist
write_default_if_not_exist(
"settings/user_settings.json", default_data=default_user_data
)
# Set up the game clock
self.set_clock(pygame.time.Clock())
# Initialize the game state manager
self.set_game_state_manager(GameStateManager())
# Create player character instances
players: list[BasePlayer] = [
Warrior("assets/characters/players/warrior/idle/0.png"),
Mage("assets/characters/players/mage/idle/0.png"),
Berserker("assets/characters/players/berserker/idle/0.png"),
Ranger("assets/characters/players/ranger/idle/0.png"),
]
# Create enemy character instances
enemies: list[BaseEnemy] = [
DreadNought("assets/characters/enemies/dreadnought/idle/0000.png"),
Devourer("assets/characters/enemies/devourer/idle/0000.png"),
Enigma("assets/characters/enemies/enigma/idle/0000.png"),
]
# Read XP from the user settings file
xp = XP(read_json("settings/user_settings.json")["xp"])
# Define quests
quests: list[Quest] = [
Quest(
"Fireball",
"Cast 10 Fireballs",
10,
1000,
),
Quest(
"Kill DreadNoughts",
"Kill 5 DreadNoughts",
5,
2000,
),
]
# Initialize various game menus and states
StartMenu(screen, ui_manager, self.get_game_state_manager())
CharacterSelectionMenu(
screen,
ui_manager,
self.get_game_state_manager(),
players,
xp,
)
LevelSelectionMenu(
screen,
ui_manager,
self.get_game_state_manager(),
enemies,
)
QuestMenu(
screen,
ui_manager,
self.get_game_state_manager(),
quests,
xp,
)
TurnBasedFight(
screen,
ui_manager,
self.get_game_state_manager(),
quests,
temp_quest=Quest(
"Normal Attack",
"use normal attack 10 times",
10,
1000,
is_temporary=True,
),
)
EndMenu(
screen,
ui_manager,
self.get_game_state_manager(),
xp,
)
# Set the initial game state to the start menu
self.get_game_state_manager().set_initial_state("start_menu")
def run(self) -> None:
"""
Runs the main game loop.
"""
# Main game loop
while self.get_running():
# Calculate the time delta for the current frame
time_delta = self.get_clock().tick_busy_loop(60)
# Update the game state and check if the game should continue running
self.set_running(self.get_game_state_manager().run(time_delta))
# Quit pygame and exit the program
pygame.quit()
quit()
def get_game_state_manager(self) -> GameStateManager:
"""
Gets the game state manager.
:return: The game state manager.
"""
return self.__game_state_manager
def set_game_state_manager(self, game_state_manager: GameStateManager) -> None:
"""
Sets the game state manager.
:param game_state_manager: The new game state manager.
"""
self.__game_state_manager = game_state_manager
def set_clock(self, clock: Clock) -> None:
"""
Sets the game clock.
:param clock: The new game clock.
"""
self.__clock = clock
def get_clock(self) -> Clock:
"""
Gets the game clock.
:return: The game clock.
"""
return self.__clock
def get_running(self) -> bool:
"""
Checks if the game is running.
:return: True if the game is running, False otherwise.
"""
return self.__running
def set_running(self, running: bool) -> None:
"""
Sets the running status of the game.
:param running: The new running status.
"""
self.__running = running