snake.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. """
  4. PyAISnake - 2018 - by psy (epsylon@riseup.net)
  5. You should have received a copy of the GNU General Public License along
  6. with PyAISnake; if not, write to the Free Software Foundation, Inc., 51
  7. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  8. """
  9. import curses, random, time
  10. from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
  11. # generate a random AI thought (evading suicidal moves)
  12. def move(prevKey):
  13. if prevKey == KEY_UP:
  14. moves = [KEY_UP, KEY_LEFT, KEY_RIGHT]
  15. elif prevKey == KEY_LEFT:
  16. moves = [KEY_UP, KEY_LEFT, KEY_DOWN]
  17. elif prevKey == KEY_RIGHT:
  18. moves = [KEY_UP, KEY_RIGHT, KEY_DOWN]
  19. elif prevKey == KEY_DOWN:
  20. moves = [KEY_LEFT, KEY_RIGHT, KEY_DOWN]
  21. else:
  22. moves = [KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_DOWN]
  23. key = random.choice(moves)
  24. return key
  25. # translate a thought to a text
  26. def text_to_thought(key):
  27. if key == KEY_UP:
  28. thought = "UP"
  29. elif key == KEY_LEFT:
  30. thought = "LEFT"
  31. elif key == KEY_RIGHT:
  32. thought = "RIGHT"
  33. elif key == KEY_DOWN:
  34. thought = "DOWN"
  35. else:
  36. thought = "LEARNING..."
  37. return thought
  38. # create matrix (with some random init values)
  39. def init(evol, record, max_moves):
  40. curses.initscr()
  41. win = curses.newwin(20, 60, 0, 0)
  42. win.keypad(1)
  43. curses.noecho()
  44. curses.curs_set(0)
  45. win.border(0)
  46. win.nodelay(1)
  47. score = 0
  48. thought = "LEARNING..."
  49. moves = 0
  50. key = move(None)
  51. food = [random.randint(1, 18), random.randint(1, 58)]
  52. snake = [[random.randint(0, 11),random.randint(0, 11)], [random.randint(0, 11),random.randint(0, 11)], [random.randint(0, 11),random.randint(0, 11)]]
  53. win.addch(food[0], food[1], '*')
  54. # build game
  55. while key != 27:
  56. win.border(0)
  57. win.addstr(0, 4, '| Moves: '+str(moves)+' - Max: '+str(max_moves)+' | Score: '+str(score)+' - Record: '+str(record)+' |')
  58. win.addstr(19, 4, '| PyAISnake -> MUTATION: '+str(evol)+' [IDEA: '+str(thought)+'] |')
  59. win.timeout(150 - (len(snake)/5 + len(snake)/10)%120) # if > length: > speed
  60. prevKey = key
  61. event = win.getch()
  62. key = move(prevKey) # AI model reply (Brain -> HERE!)
  63. moves += 1
  64. thought = text_to_thought(key)
  65. key = key if event == -1 else event
  66. if key == ord(' '): # SPACE BAR for pause
  67. key = -1 # pause
  68. win.addstr(9, 18, ' GAME PAUSED... ')
  69. while key != ord(' '):
  70. key = win.getch()
  71. key = prevKey
  72. continue
  73. if score >= record: # NEW record!
  74. record = score
  75. if moves >= max_moves: # NEW max moves!
  76. max_moves = moves
  77. snake.insert(0, [snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1), snake[0][1] + (key == KEY_LEFT and -1) + (key == KEY_RIGHT and 1)])
  78. if snake[0][0] == 0:
  79. snake[0][0] = 18
  80. if snake[0][1] == 0:
  81. snake[0][1] = 58
  82. if snake[0][0] == 19:
  83. snake[0][0] = 1
  84. if snake[0][1] == 59:
  85. snake[0][1] = 1
  86. if snake[0] in snake[1:]: # GAME LOST ;-(
  87. win.addstr(9, 18, ' SORRY: GAME OVER !!!')
  88. win.addstr(19, 4, '| PyAISnake -> MUTATION: '+str(evol)+' [IDEA: TRYING AGAIN!] |')
  89. if score >= record: # NEW record!
  90. record = score
  91. if moves >= max_moves: # NEW max moves!
  92. max_moves = moves -1
  93. event = win.getch()
  94. time.sleep(2)
  95. evol += 1
  96. init(evol, record, max_moves)
  97. if snake[0] == food:
  98. food = []
  99. score += 1
  100. while food == []:
  101. food = [random.randint(1, 18), random.randint(1, 58)]
  102. if food in snake: food = []
  103. win.addch(food[0], food[1], '*')
  104. else:
  105. last = snake.pop()
  106. try:
  107. win.addch(last[0], last[1], ' ')
  108. except:
  109. pass
  110. try:
  111. win.addch(snake[0][0], snake[0][1], '#')
  112. except:
  113. pass
  114. init(0, 0, 0) # start a new GAME ;-)