Skip to content

Instantly share code, notes, and snippets.

@icio
Created January 5, 2014 07:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icio/8265329 to your computer and use it in GitHub Desktop.
Save icio/8265329 to your computer and use it in GitHub Desktop.
Python command-line tile world for unix terminals
#!/usr/bin/env python
def world():
world = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
]
player = {
'x': 1,
'y': len(world) - 2
}
UP, DOWN, LEFT, RIGHT = 'w', 's', 'a', 'd'
controls = {
UP: {'x': 0, 'y': -1},
DOWN: {'x': 0, 'y': 1},
LEFT: {'x': -1, 'y': 0},
RIGHT: {'x': 1, 'y': 0},
}
controls_keys = (UP, LEFT, DOWN, RIGHT)
paint(world, player, controls_keys)
for key in keypresses(controls_keys):
v = controls[key]
if world[player['y'] + v['y']][player['x'] + v['x']] == 0:
player['x'] += v['x']
player['y'] += v['y']
paint(world, player, controls_keys)
import os
def paint(world, player, controls_keys):
# Clear the screen
os.system('clear')
# Draw the world
for y in range(0, len(world)):
for x in range(0, len(world[0])):
if player['x'] == x and player['y'] == y:
char = '#'
elif world[y][x] == 1:
char = '.'
else:
char = ' '
print char,
print # line break
print ", ".join(controls_keys), "to move."
print "Ctrl+C to quit."
import sys
import tty
import termios
def keypresses(keys):
keymap = {
'\x03': KeyboardInterrupt,
'\x04': EOFError,
}
while True:
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
key = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
if key in keymap:
raise keymap[key]
elif key in keys:
yield key
if __name__ == '__main__':
try:
world()
except KeyboardInterrupt:
pass
@icio
Copy link
Author

icio commented Jan 5, 2014

python <(curl -s https://gist.github.com/icio/8265329/raw/tiles.py)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment