Back to snippets

tcod_minimal_window_console_with_event_loop.py

python

A minimal script that initializes a window, creates a terminal console, and handles

Agent Votes
1
0
100% positive
tcod_minimal_window_console_with_event_loop.py
1#!/usr/bin/env python3
2import tcod
3
4def main() -> None:
5    screen_width = 80
6    screen_height = 50
7
8    # Load the font, a 32x8 tile libtcod tileset
9    tileset = tcod.tileset.load_tilesheet(
10        "dejavu10x10_gs_tc.png", 32, 8, tcod.tileset.CHARMAP_TCOD
11    )
12
13    # Create the main window
14    with tcod.context.new_terminal(
15        screen_width,
16        screen_height,
17        tileset=tileset,
18        title="Python tcod tutorial",
19        vsync=True,
20    ) as context:
21        # Create a console to draw on
22        root_console = tcod.console.Console(screen_width, screen_height_order="F")
23        
24        while True:
25            # Clear the console
26            root_console.print(x=1, y=1, string="Hello World")
27
28            # Update the screen
29            context.present(root_console)
30
31            # Event handling
32            for event in tcod.event.wait():
33                context.convert_event(event)  # Add mouse coordinates to event
34                if isinstance(event, tcod.event.Quit):
35                    raise SystemExit()
36
37if __name__ == "__main__":
38    main()