-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_game.py
159 lines (130 loc) · 4.66 KB
/
setup_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
"""Handle the loading and initialization of game sessions."""
from __future__ import annotations
import copy
import lzma
import os
import pickle
import sys
import traceback
from typing import Optional
import tcod
from tcod import libtcodpy
import color
from engine import Engine
import actor_factories
from event_handlers.base_event_handler import BaseEventHandler
from event_handlers.main_game_event_handler import MainGameEventHandler
from event_handlers.pop_up_message_event_handler import PopupMessage
import item_factories
from game_world import GameWorld
from global_vars import VERSION
import global_vars
from turn_manager import TurnManager
# Load the background image and remove the alpha channel.
# This tries to open the file bundled in the executable or the current directory
try:
background_image = tcod.image.load(
os.path.join(os.path.dirname(__file__), "assets/menu_background.png"),
)[:, :, :3]
except NameError:
background_image = tcod.image.load(
os.path.join(os.path.dirname(sys.argv[0]), "assets/menu_background.png")
)[:, :, :3]
def new_game() -> Engine:
"""Return a brand new game session as an Engine instance."""
map_width = 80
map_height = 43
player = copy.deepcopy(actor_factories.player)
engine = Engine(player=player, debug_mode=global_vars.DEBUG_MODE)
engine.game_world = GameWorld(
engine=engine,
map_width=map_width,
map_height=map_height,
)
engine.turn_manager = TurnManager()
engine.turn_manager.add_actor(player)
if global_vars.DEBUG_MODE:
engine.game_world.load_prefab_map("debug_room")
engine.message_log.add_message(
"DEBUG MODE ENABLED",
color.yellow,
)
else:
engine.game_world.generate_floor()
engine.update_fov()
engine.message_log.add_message(
"As you step into the darkness, a malevolent laughter echoes from the castle!",
color.welcome_text,
)
engine.message_log.add_message(
"Press F1 for help",
color.white,
)
dagger = copy.deepcopy(item_factories.dagger)
dagger.parent = player.inventory
player.inventory.items.append(dagger)
player.equipment.toggle_equip(dagger, add_message=False)
return engine
def load_game(filename: str) -> Engine:
"""Load an Engine instance from a file."""
with open(filename, "rb") as f:
engine = pickle.loads(lzma.decompress(f.read()))
assert isinstance(engine, Engine)
return engine
class MainMenu(BaseEventHandler):
"""Handle the main menu rendering and input."""
def on_render(self, console: tcod.console.Console) -> None:
"""Render the main menu on a background image."""
# Make the image fill in the console size with the background image
console.draw_semigraphics(background_image, 0, 0)
console.print(
console.width // 2,
console.height // 2 - 4,
"Castle of the Eternal Night",
fg=color.menu_title,
alignment=libtcodpy.CENTER,
)
console.print(
console.width // 2,
console.height - 2,
"By Julio Valls",
fg=color.menu_title,
alignment=libtcodpy.CENTER,
)
# Version text
console.print(
console.width - 6,
console.height - 2,
VERSION,
fg=color.menu_title,
bg=color.black,
alignment=libtcodpy.CENTER,
bg_blend=libtcodpy.BKGND_ALPHA(64),
)
menu_width = 24
for i, text in enumerate(
["[N] Play a new game", "[C] Continue last game", "[Q] Quit"]
):
console.print(
console.width // 2,
console.height // 2 - 2 + i,
text.ljust(menu_width),
fg=color.menu_text,
bg=color.black,
alignment=libtcodpy.CENTER,
bg_blend=libtcodpy.BKGND_ALPHA(64),
)
def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[BaseEventHandler]:
if event.sym in (tcod.event.KeySym.q, tcod.event.KeySym.ESCAPE):
raise SystemExit()
elif event.sym == tcod.event.KeySym.c:
try:
return MainGameEventHandler(load_game("savegame.sav"))
except FileNotFoundError:
return PopupMessage(self, "No saved game to load.")
except Exception as exc:
traceback.print_exc() # Print to stderr.
return PopupMessage(self, f"Failed to load save:\n{exc}")
elif event.sym == tcod.event.KeySym.n:
return MainGameEventHandler(new_game())
return None