OreganoHauch

Snake game

Mar 27th, 2021 (edited)
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 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 key == ord("P"):
  63. time.sleep(2)
  64.  
  65. if event != -1:
  66. key = event
  67. else:
  68. key = prev_key
  69.  
  70. if key not in [curses.KEY_LEFT, curses.KEY_RIGHT, curses.KEY_UP, curses.KEY_DOWN, ESC]:
  71. key = prev_key
  72.  
  73. # calculate the next coordinates
  74. y = snake[0][0]
  75. x = snake[0][1]
  76. if key == curses.KEY_DOWN:
  77. y += 1
  78. if key == curses.KEY_UP:
  79. y -= 1
  80. if key == curses.KEY_LEFT:
  81. x -= 1
  82. if key == curses.KEY_RIGHT:
  83. x += 1
  84.  
  85. snake.insert(0, (y,x)) # append O(n)
  86.  
  87. # check if we hit the border
  88. if y == 0:
  89. break
  90. if y == WIN_HEIGHT-1:
  91. break
  92. if x == 0:
  93. break
  94. if x == WIN_WIDTH-1:
  95. break
  96.  
  97. # check if snake runs over itself
  98. if snake[0] in snake[1:]:
  99. break
  100.  
  101. # check if snake hits food
  102. if snake[0] == food:
  103. # eat the food
  104. score += 1
  105. food = ()
  106. while food == ():
  107. food = (randint(1,WIN_HEIGHT-2), randint(1,WIN_WIDTH-2))
  108. if food in snake:
  109. food = ()
  110. win.addch(food[0], food[1], "#")
  111. else:
  112. # move the snake
  113. last = snake.pop()
  114. win.addch(last[0], last[1], " ")
  115.  
  116. win.addch(snake[0][0], snake[0][1], '*')
  117.  
  118. curses.endwin()
  119. print(f"Hooray! Your final score is {score}.")
Add Comment
Please, Sign In to add comment