Add the two scripts from the libtcod docs. These are mostly useful basic functionality checks.

This commit is contained in:
Eryn Wells 2022-04-26 19:00:22 -07:00
parent 8130a8ee82
commit 2c9b390b23
2 changed files with 70 additions and 0 deletions

40
01_fixed_size_console.py Normal file
View file

@ -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()

View file

@ -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()