|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | + This script shows the basic use of the tdl module. |
| 4 | + |
| 5 | + The font is configured and the module is initialized, the script then goes |
| 6 | + into an infinite loop where it will draw "Hello World" on the screen and |
| 7 | + then check for an event that tells that the user has closed the window. |
| 8 | + |
| 9 | + When the window is closed the script quits out by raising a SystemExit |
| 10 | + exception. |
| 11 | +""" |
| 12 | + |
| 13 | +import tdl |
| 14 | + |
| 15 | +# Define the window size (in character tiles.) We'll pick something small. |
| 16 | +WIDTH, HEIGHT = 40, 30 # 320x240 when the font size is taken into account |
| 17 | + |
| 18 | +# Set the font to the example font in the tutorial folder. This font is |
| 19 | +# equivalent to the default font you will get if you skip this call. |
| 20 | +# With the characters rows first and the font size included in the filename |
| 21 | +# you won't have to specify any parameters other than the font file itself. |
| 22 | +tdl.setFont('terminal8x8_gs_ro.png') |
| 23 | + |
| 24 | +# Call tdl.init to create the root console. |
| 25 | +# We will call drawing operations on the returned object. |
| 26 | +console = tdl.init(WIDTH, HEIGHT, 'python-tdl tutorial') |
| 27 | + |
| 28 | +# Start an infinite loop. Drawing and game logic will be put in this loop. |
| 29 | +while True: |
| 30 | + |
| 31 | + # Reset the console to a blank slate before drawing on it. |
| 32 | + console.clear() |
| 33 | + |
| 34 | + # Now draw out 'Hello World' starting at an x,y of 1,2. |
| 35 | + console.drawStr(1, 2, 'Hello World') |
| 36 | + |
| 37 | + # Now to update the image on the window we make sure to call tdl.flush |
| 38 | + # in every loop. |
| 39 | + tdl.flush() |
| 40 | + |
| 41 | + # Handle events by iterating over the values returned by tdl.event.get |
| 42 | + for event in tdl.event.get(): |
| 43 | + # Check if this is a 'QUIT' event |
| 44 | + if event.type == 'QUIT': |
| 45 | + # Later we may want to save the game or confirm if the user really |
| 46 | + # wants to quit but for now we break out of the loop by raising a |
| 47 | + # SystemExit exception. |
| 48 | + # The optional string parameter will be printed out on the |
| 49 | + # terminal after the script exits. |
| 50 | + raise SystemExit('The window has been closed.') |
0 commit comments