diff --git a/01_fixed_size_console.py b/01_fixed_size_console.py new file mode 100644 index 0000000..41608a2 --- /dev/null +++ b/01_fixed_size_console.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# Make sure 'dejavu10x10_gs_tc.png' is in the same directory as this script. +import tcod + +WIDTH, HEIGHT = 80, 60 # Console width and height in tiles. + + +def main() -> None: + """Script entry point.""" + + # Load the font, a 32 by 8 tile font with libtcod's old character layout. + tileset = tcod.tileset.load_tilesheet( + "dejavu10x10_gs_tc.png", 32, 8, tcod.tileset.CHARMAP_TCOD, + ) + + # Create the main console. + console = tcod.Console(WIDTH, HEIGHT, order="F") + + # Create a window based on this console and tileset. + # New window for a console of size columnsĂ—rows. + with tcod.context.new(columns=console.width, rows=console.height, tileset=tileset) as context: + # Main loop, runs until SystemExit is raised. + while True: + console.clear() + console.print(x=0, y=0, string="Hello World!") + context.present(console) # Show the console. + + # This event loop will wait until at least one event is processed before exiting. + # For a non-blocking event loop replace with . + for event in tcod.event.wait(): + context.convert_event(event) # Sets tile coordinates for mouse events. + print(event) # Print event names and attributes. + if isinstance(event, tcod.event.Quit): + raise SystemExit() + + # The window will be closed after the above with-block exits. + + +if __name__ == "__main__": + main() diff --git a/02_dynamically_sized_console.py b/02_dynamically_sized_console.py new file mode 100644 index 0000000..db8c5a8 --- /dev/null +++ b/02_dynamically_sized_console.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +import tcod + +WIDTH, HEIGHT = 720, 480 # Window pixel resolution (when not maximized.) +FLAGS = tcod.context.SDL_WINDOW_RESIZABLE | tcod.context.SDL_WINDOW_MAXIMIZED + + +def main() -> None: + """Script entry point.""" + # New window with pixel resolution of widthĂ—height. + with tcod.context.new(width=WIDTH, height=HEIGHT, sdl_window_flags=FLAGS) as context: + while True: + # Console size based on window resolution and tile size. + console = context.new_console(order="F") + console.print(0, 0, "Hello World") + context.present(console, integer_scaling=True) + + for event in tcod.event.wait(): + # Sets tile coordinates for mouse events. + context.convert_event(event) + print(event) + + if isinstance(event, tcod.event.Quit): + raise SystemExit() + elif isinstance(event, tcod.event.WindowResized) and event.type == "WINDOWRESIZED": + pass # The next call to context.new_console may return a different size. + + +if __name__ == "__main__": + main()