Last active
May 6, 2016 08:49
-
-
Save tomviner/11365314 to your computer and use it in GitHub Desktop.
One dimensional snake
This file contains 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 os | |
import random | |
import string | |
import sys | |
import time | |
from utils import getch | |
def snake1d(): | |
universe_width = 100 | |
x = universe_width/2 | |
direction = 1 | |
speed = 1 | |
snake_width = 1 | |
kgen = getch(0.1) | |
key_map = {'D': -1, 'C': 1} | |
rand_x = random.randint(0, universe_width-1) | |
food_waiting = False | |
while 1: | |
c = kgen.next() | |
direction = key_map.get(c, direction) | |
x += direction * speed | |
if x in range(rand_x, rand_x+speed): | |
speed += 1 | |
food_waiting = False | |
if x not in range(universe_width): | |
print 'Game Over' | |
print 'You scored {}!'.format(speed) | |
sys.exit() | |
line = ['.'] * universe_width | |
line[x:x+speed] = 'X'*speed | |
if not food_waiting: | |
rand_x = random.randint(0, universe_width-1) | |
food = random.choice(string.letters) | |
food_waiting = True | |
line[rand_x] = food | |
os.system('clear') | |
print str(speed).center(universe_width) | |
print ''.join(line) | |
if __name__ == '__main__': | |
snake1d() |
This file contains 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
""" | |
Based on: https://docs.python.org/2/faq/library.html#how-do-i-get-a-single-keypress-at-a-time | |
""" | |
from contextlib import contextmanager | |
import os | |
import sys | |
import sys | |
import time | |
import fcntl | |
import termios | |
@contextmanager | |
def stdin_setup(): | |
fd = sys.stdin.fileno() | |
oldterm = termios.tcgetattr(fd) | |
newattr = termios.tcgetattr(fd) | |
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO | |
termios.tcsetattr(fd, termios.TCSANOW, newattr) | |
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) | |
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) | |
try: | |
yield | |
finally: | |
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) | |
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags) | |
def getch(timeout=0): | |
while True: | |
c = None | |
start = time.time() | |
with stdin_setup(): | |
while True: | |
duration = time.time() - start | |
if timeout and timeout < duration: | |
break | |
try: | |
c = sys.stdin.read(1) | |
except IOError: | |
pass | |
if c: | |
break | |
yield c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment