Advertisement
OreganoHauch

Snake game v2

Mar 27th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. import curses
  2. from random import randint
  3. import time
  4.  
  5. # constants and game settings
  6. WIN_HEIGHT = 20
  7. WIN_WIDTH = 50
  8. snake_init_length = 3
  9. init_direction = curses.KEY_RIGHT
  10. ESC = 27 # is defined as key #27 in curses module
  11.  
  12. # set up window
  13. curses.initscr()
  14. win = curses.newwin(WIN_HEIGHT, WIN_WIDTH, 0, 0) # first y, then x coordinate
  15. win.keypad(1) # equally 1 could be used
  16. curses.noecho() # no echo of inputs
  17. curses.curs_set(0) # hide cursor
  18. win.border(0) # no border
  19. win.nodelay(1) # Don't wait for next user key input
  20.  
  21. # initialize snake
  22. def init_margin(var):
  23. return randint(var//3, 2*var//3)
  24.  
  25. snake_init_y = (init_margin(WIN_HEIGHT))
  26. snake_init_x = (init_margin(WIN_WIDTH))
  27.  
  28. def snake(length):
  29. snake = []
  30. counter = 0
  31. for i in range(length+1):
  32. snake.append((snake_init_y,snake_init_x-counter))
  33. counter += 1
  34. return snake
  35.  
  36. snake = snake(snake_init_length)
  37.  
  38. # initialize food
  39.  
  40. while True:
  41. food_init_y = (init_margin(WIN_HEIGHT))
  42. food_init_x = (init_margin(WIN_WIDTH))
  43. if (food_init_y, food_init_x) not in snake:
  44. break
  45.  
  46. food = (food_init_y, food_init_x)
  47. win.addch(food[0], food[1], "#")
  48.  
  49. # start the game
  50.  
  51. key = init_direction
  52.  
  53. score = 0
  54.  
  55. while key != ESC:
  56. win.addstr(0,2, f"Score {score}") # line,column,str
  57. win.timeout(150 - (len(snake))//5 + (len(snake)//10 % 120)) # increase speed
  58.  
  59. prev_key = key
  60. event = win.getch()
  61.  
  62. if False:
  63. pause_active = True
  64.  
  65. if key == ord('P'):
  66. pause_active = not pause_active
  67.  
  68. if pause_active:
  69. continue
  70.  
  71. if event != -1:
  72. key = event
  73. else:
  74. key = prev_key
  75.  
  76. if key not in [curses.KEY_LEFT, curses.KEY_RIGHT, curses.KEY_UP, curses.KEY_DOWN, ESC]:
  77. key = prev_key
  78.  
  79. # calculate the next coordinates
  80. y = snake[0][0]
  81. x = snake[0][1]
  82. if key == curses.KEY_DOWN:
  83. y += 1
  84. if key == curses.KEY_UP:
  85. y -= 1
  86. if key == curses.KEY_LEFT:
  87. x -= 1
  88. if key == curses.KEY_RIGHT:
  89. x += 1
  90.  
  91. snake.insert(0, (y,x)) # append O(n)
  92.  
  93. # check if we hit the border
  94. if y == 0:
  95. break
  96. if y == WIN_HEIGHT-1:
  97. break
  98. if x == 0:
  99. break
  100. if x == WIN_WIDTH-1:
  101. break
  102.  
  103. # check if snake runs over itself
  104. if snake[0] in snake[1:]:
  105. break
  106.  
  107. # check if snake hits food
  108. if snake[0] == food:
  109. # eat the food
  110. score += 1
  111. food = ()
  112. while food == ():
  113. food = (randint(1,WIN_HEIGHT-2), randint(1,WIN_WIDTH-2))
  114. if food in snake:
  115. food = ()
  116. win.addch(food[0], food[1], "#")
  117. else:
  118. # move the snake
  119. last = snake.pop()
  120. win.addch(last[0], last[1], " ")
  121.  
  122. win.addch(snake[0][0], snake[0][1], '*')
  123.  
  124. curses.endwin()
  125. print(f"Hooray! Your final score is {score}.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement