Skip to content

Instantly share code, notes, and snippets.

@x13machine
Created January 1, 2015 21:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x13machine/e2cc2a51886b054b7701 to your computer and use it in GitHub Desktop.
Save x13machine/e2cc2a51886b054b7701 to your computer and use it in GitHub Desktop.
codenotworking
import curses, math, signal, time, thread
#init
screen = curses.initscr()
curses.noecho()
curses.curs_set(0)
curses.start_color();
curses.use_default_colors()
#colors
for i in range(0, curses.COLORS):
curses.init_pair(i + 1,-1,i)
#ctrl-c handler
running=True
def signal_handler(signal, frame):
global running
running=False
signal.signal(signal.SIGINT, signal_handler)
#player
pos=[0,0]
direction=[0,-1]
parts=[]
screen.timeout(-1)
screen.nodelay(1)
lastframe=time.time()
#game loop
while running:
ch=screen.getch()
if ch==ord('w'): direction=[0,-1]
if ch==ord('s'): direction=[0,1]
if ch==ord('a'): direction=[-1,0]
if ch==ord('d'): direction=[1,0]
if time.time()-lastframe>0.1:
#update
lastframe=time.time()
parts+=[pos]
dims=screen.getmaxyx()
pos[0]+=direction[0]
pos[1]+=direction[1]
if pos[0]<0: pos[0]=dims[1]-1
if pos[0]>dims[1]-1: pos[0]=0
if pos[1]<0: pos[1]=dims[0]-1
if pos[1]>dims[0]-1: pos[1]=0
#render
screen.clear()
for i in parts:
screen.addstr(i[1],i[0],' ',curses.color_pair(3))
screen.addstr(pos[1],pos[0],' ',curses.color_pair(2))
screen.refresh()
curses.endwin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment