Skip to content

Commit 344287b

Browse files
committed
Add a simple graveyard system.
1 parent a60ba1b commit 344287b

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ savegame.sav
77
*.exe
88
*.zip
99
.tool-versions
10+
graveyard.txt

event_handlers/game_over_event_handler.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from event_handlers.event_handler import EventHandler
1212
from event_handlers.main_game_event_handler import MainGameEventHandler
1313
import exceptions
14+
from graveyard import create_graveyard_entry
1415

1516

1617
class GameOverEventHandler(EventHandler):
@@ -43,6 +44,7 @@ def on_render(self, console: tcod.console.Console) -> None:
4344

4445
def on_quit(self) -> None:
4546
"""Handle exiting out of a finished game."""
47+
create_graveyard_entry(self.engine.player, self.engine)
4648
if os.path.exists("savegame.sav"):
4749
os.remove("savegame.sav") # Deletes the active save file.
4850
raise exceptions.QuitWithoutSaving() # Avoid saving a finished game.
@@ -51,6 +53,7 @@ def on_restart(self) -> MainGameEventHandler:
5153
"""Handle restarting the game."""
5254
from setup_game import new_game # pylint: disable=import-outside-toplevel
5355

56+
create_graveyard_entry(self.engine.player, self.engine)
5457
if os.path.exists("savegame.sav"):
5558
os.remove("savegame.sav") # Deletes the active save file.
5659

graveyard.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import datetime
2+
import os
3+
4+
from engine import Engine
5+
from entity import Actor
6+
7+
GRAVERYARD_FILE = "graveyard.txt"
8+
9+
10+
def check_graveyard_file_exists():
11+
root_dir = os.getcwd() # Get the current working directory
12+
graveyard_file = os.path.join(
13+
root_dir, GRAVERYARD_FILE
14+
) # Assuming the file name is "graveyard.txt"
15+
16+
if os.path.isfile(graveyard_file):
17+
return True
18+
else:
19+
return False
20+
21+
22+
def create_graveyard_entry(player: Actor, engine: Engine) -> None:
23+
root_dir = os.getcwd() # Get the current working directory
24+
graveyard_file = os.path.join(
25+
root_dir, GRAVERYARD_FILE
26+
) # Assuming the file name is "graveyard.txt"
27+
28+
current_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
29+
with open(graveyard_file, "a") as file:
30+
file.write(
31+
f"+ Player Exp: {player.level.total_xp} Turns: {engine.current_turn} Died at floor {engine.game_world.current_floor} on {current_date} +\n"
32+
)

0 commit comments

Comments
 (0)