1
1
from __future__ import annotations
2
2
3
+ import os
3
4
from typing import Optional , TYPE_CHECKING , Callable , Tuple , Union
4
5
5
6
import tcod
@@ -87,17 +88,19 @@ def on_render(self, console: tcod.Console) -> None:
87
88
def ev_quit (self , event : tcod .event .Quit ) -> Optional [Action ]:
88
89
raise SystemExit ()
89
90
91
+
90
92
class PopupMessage (BaseEventHandler ):
91
93
"""Display a popup text window"""
94
+
92
95
def __init__ (self , parent_handler : BaseEventHandler , text : str ):
93
96
self .parent = parent_handler
94
97
self .text = text
95
98
96
99
def on_render (self , console : tcod .Console ) -> None :
97
100
"""Render the parent and dim the result then print the message on top"""
98
101
self .parent .on_render (console )
99
- console .rgb ["fg" ] //= 8
100
- console .rgb ["bg" ] //= 8
102
+ console .rgb ["fg" ] //= 8
103
+ console .rgb ["bg" ] //= 8
101
104
102
105
console .print (
103
106
console .width // 2 ,
@@ -112,6 +115,7 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[BaseEventHandler]:
112
115
"""any key returns to the parent handler"""
113
116
return self .parent
114
117
118
+
115
119
class EventHandler (BaseEventHandler ):
116
120
def __init__ (self , engine : Engine ):
117
121
self .engine = engine
@@ -194,9 +198,18 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
194
198
195
199
196
200
class GameOverEventHandler (EventHandler ):
201
+ def on_quit (self ) -> None :
202
+ """Handle exiting a finished game"""
203
+ if os .path .exists ("savegame.sav" ):
204
+ os .remove ("savegame.sav" ) # deletes the active save file
205
+ raise exceptions .QuitWithoutSaving () # avoid saving a finished game
206
+
207
+ def ev_quit (self , event : tcod .event .Quit ) -> None :
208
+ self .on_quit ()
209
+
197
210
def ev_keydown (self , event : tcod .event .KeyDown ) -> None :
198
211
if event .sym == tcod .event .K_ESCAPE :
199
- raise SystemExit ()
212
+ self . on_quit ()
200
213
201
214
202
215
CURSOR_Y_KEYS = {
0 commit comments