Skip to content

Commit 9134110

Browse files
4B796C65@gmail.com4B796C65@gmail.com
authored andcommitted
Uploading partial tutorial so that I don't accidently lose it again.
1 parent 9ef6e73 commit 9134110

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

examples/tutorial/1-Basics.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.')

examples/tutorial/2-Movement.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env python
2+
"""
3+
4+
"""
5+
6+
import tdl
7+
8+
WIDTH, HEIGHT = 40, 30 # Defines the window size.
9+
10+
# Create a dictionary that maps keys to vectors.
11+
# Names of the available keys can be found in the online documentation:
12+
# http://packages.python.org/tdl/tdl.event-module.html
13+
MOVEMENT_KEYS = {
14+
# standard arrow keys
15+
'UP': [0, -1],
16+
'DOWN': [0, 1],
17+
'LEFT': [-1, 0],
18+
'RIGHT': [1, 0],
19+
20+
# diagonal keys
21+
# keep in mind that the keypad won't use these keys even if
22+
# num-lock is off
23+
'HOME': [-1, -1],
24+
'PAGEUP': [1, -1],
25+
'PAGEDOWN': [1, 1],
26+
'END': [-1, 1],
27+
28+
# number-pad keys
29+
# These keys will always show as KPx regardless if num-lock
30+
# is on or off. Keep in mind that some keyboards and laptops
31+
# may be missing a keypad entirely.
32+
# 7 8 9
33+
# 4 6
34+
# 1 2 3
35+
'KP1': [-1, 1],
36+
'KP2': [0, 1],
37+
'KP3': [1, 1],
38+
'KP4': [-1, 0],
39+
'KP6': [1, 0],
40+
'KP7': [-1, -1],
41+
'KP8': [0, -1],
42+
'KP9': [1, -1],
43+
}
44+
45+
46+
tdl.setFont('terminal8x8_gs_ro.png') # Configure the font.
47+
48+
# Create the root console.
49+
console = tdl.init(WIDTH, HEIGHT, 'python-tdl tutorial')
50+
51+
# player coordinates
52+
playerX, playerY = 1, 2
53+
54+
while True: # Continue in an infinite game loop.
55+
56+
console.clear() # Blank the console.
57+
58+
# Using "(x, y) in console" we can quickly check if a position is inside of
59+
# a console. And skip a draw operation that would otherwise fail.
60+
if (playerX, playerY) in console:
61+
console.drawChar(playerX, playerY, '@')
62+
63+
tdl.flush() # Update the window.
64+
65+
for event in tdl.event.get(): # Iterate over recent events.
66+
if event.type == 'KEYDOWN':
67+
# We mix special keys with normal characters so we use keychar.
68+
if event.keychar.upper() in MOVEMENT_KEYS:
69+
# Get the vector and unpack it into these two variables.
70+
keyX, keyY = MOVEMENT_KEYS[event.keychar.upper()]
71+
# Then we add the vector to the current player position.
72+
playerX += keyX
73+
playerY += keyY
74+
75+
if event.type == 'QUIT':
76+
# Halt the script using SystemExit
77+
raise SystemExit('The window has been closed.')
3.61 KB
Loading

0 commit comments

Comments
 (0)