Created
September 19, 2015 14:55
-
-
Save AndyNovo/7390df33d8f36820442c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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