22"""Main entry-point module. This script is used to start the program."""
33from __future__ import annotations
44
5- import attrs
65import tcod .console
76import tcod .context
87import tcod .event
98import tcod .tileset
109
11-
12- @attrs .define (eq = False )
13- class ExampleState :
14- """Example state with a hard-coded player position."""
15-
16- player_x : int
17- """Player X position, left-most position is zero."""
18- player_y : int
19- """Player Y position, top-most position is zero."""
20-
21- def on_event (self , event : tcod .event .Event ) -> None :
22- """Move the player on events and handle exiting. Movement is hard-coded."""
23- match event :
24- case tcod .event .Quit ():
25- raise SystemExit ()
26- case tcod .event .KeyDown (sym = tcod .event .KeySym .LEFT ):
27- self .player_x -= 1
28- case tcod .event .KeyDown (sym = tcod .event .KeySym .RIGHT ):
29- self .player_x += 1
30- case tcod .event .KeyDown (sym = tcod .event .KeySym .UP ):
31- self .player_y -= 1
32- case tcod .event .KeyDown (sym = tcod .event .KeySym .DOWN ):
33- self .player_y += 1
34-
35- def on_draw (self , console : tcod .console .Console ) -> None :
36- """Draw the player with print. Bounds do not need to be checked with this function."""
37- console .print (self .player_x , self .player_y , "@" )
10+ import g
11+ import game .state_tools
12+ import game .states
3813
3914
4015def main () -> None :
@@ -43,15 +18,10 @@ def main() -> None:
4318 "data/Alloy_curses_12x12.png" , columns = 16 , rows = 16 , charmap = tcod .tileset .CHARMAP_CP437
4419 )
4520 tcod .tileset .procedural_block_elements (tileset = tileset )
46- console = tcod .console .Console (80 , 50 )
47- state = ExampleState (player_x = console .width // 2 , player_y = console .height // 2 )
48- with tcod .context .new (console = console , tileset = tileset ) as context :
49- while True : # Main loop
50- console .clear () # Clear the console before any drawing
51- state .on_draw (console ) # Draw the current state
52- context .present (console ) # Render the console to the window and show it
53- for event in tcod .event .wait (): # Event loop, blocks until pending events exist
54- state .on_event (event ) # Dispatch events to the state
21+ g .console = tcod .console .Console (80 , 50 )
22+ g .states = [game .states .ExampleState (player_x = g .console .width // 2 , player_y = g .console .height // 2 )]
23+ with tcod .context .new (console = g .console , tileset = tileset ) as g .context :
24+ game .state_tools .main_loop ()
5525
5626
5727if __name__ == "__main__" :
0 commit comments