Guest User

python 2.7 snake

a guest
Sep 19th, 2012
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. from curses import initscr,curs_set,newwin,endwin,KEY_RIGHT,KEY_LEFT,KEY_DOWN,KEY_UP
  2. from random import randrange
  3. initscr()
  4. curs_set(0)
  5. win = newwin(16,60,0,0)
  6. win.keypad(1)
  7. win.nodelay(1)
  8. win.border('|','|','-','-','+','+','+','+')
  9. win.addch(4,44,'O')
  10. snake = [ [30,7],[29,8],[28,7],[27,7],[26,7],[25,7] ]
  11. key = KEY_RIGHT
  12. while key != 27:
  13.     win.addstr(0,2,' Score: '+str(len(snake)-6)+' ')
  14.     win.timeout(180+ ( (len(snake)-6) % 10- (len(snake)-6) ) * 3 )
  15.     getkey = win.getch()
  16.     key = key if getkey==-1 else getkey
  17.     snake.insert(0,[snake[0][0]+(key==KEY_RIGHT and 1 or key==KEY_LEFT and -1), snake[0][1]+(key==KEY_DOWN and 1 or key==KEY_UP and -1)])
  18.     win.addch(snake[len(snake)-1][1],snake[len(snake)-1][0],' ')
  19.     if win.inch(snake[0][1],snake[0][0]) & 255 == 32: snake.pop()
  20.     elif win.inch(snake[0][1],snake[0][0]) & 255 == ord('O'):  
  21.         c = [n for n in [[randrange(1,58,1),randrange(1,14,1)] for x in range(len(snake))] if n not in snake]
  22.         win.addch(c == [] and 4 or c[0][1],c == [] and 44 or c[0][0],'O')
  23.     else: break
  24.     win.addch(snake[0][1],snake[0][0],'X')
  25. endwin()
  26. print '\n  Game over! your score: '+str(len(snake)-7)+'\n'
Advertisement
Add Comment
Please, Sign In to add comment