Skip to content

Instantly share code, notes, and snippets.

@AndyNovo
Created September 19, 2015 14:55
Show Gist options
  • Save AndyNovo/7390df33d8f36820442c to your computer and use it in GitHub Desktop.
Save AndyNovo/7390df33d8f36820442c to your computer and use it in GitHub Desktop.
import random, os, sys, time
#Animate some ASCII art (thanks Philip: http://stackoverflow.com/a/2785568/1347629)
def draw_to_screen(content):
clear_console = 'clear' if os.name == 'posix' else 'CLS'
os.system(clear_console)
sys.stdout.write(content)
sys.stdout.flush()
time.sleep(0.2)
def board(w, h, x, y):
result = ""
for i in range(h):
if i != y:
result += "-"*w + "\n"
else:
result += "-"*(x-1)+"*"+"-"*(w-x)+"\n"
return result
def make_move(w, h, x, y, direction):
#direction = random.randint(0,3)
if direction == 0:
#up
if y > 0:
return x, y-1
else:
return x,y
elif direction == 1:
#right
if x < w-1:
return x+1, y
else:
return x,y
elif direction == 2:
#down
if y < h-1:
return x, y+1
else:
return x,y
elif direction == 3:
#left
if x > 0:
return x-1, y
else:
return x,y
width = 30
height = 15
creatureX = 10
creatureY = 10
for i in range(300):
draw_to_screen(board(width, height, creatureX, creatureY))
creatureX, creatureY = make_move(width, height, creatureX, creatureY, random.randint(0,3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment