Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. from enum import Enum
  2. import random
  3.  
  4.  
  5. class Move(Enum):
  6. # Direction where the player can go
  7. Still = 0, 0
  8. Up = 0, -1
  9. Down = 0, 1
  10. Right = 1, 0
  11. Left = -1, 0
  12.  
  13.  
  14. class Game:
  15. def __init__(self, width, height):
  16. # Definition of basic direction (right side here)
  17. self.heading = Move.Right
  18.  
  19. # Definition of the width and height of game board
  20. self.width = width
  21. self.height = height
  22. self.size = width * height
  23.  
  24. # We place the snake in the middle of the game board
  25. self.snake = [
  26. (int(width / 2), int(height / 2)),
  27. (int(width / 2) - 1, int(height / 2))
  28. ]
  29.  
  30. # We place the food in the first quarter of the game board
  31. self.food = (int(width / 4), int(height / 4))
  32.  
  33. # Indication that the game is launch
  34. self.live = True
  35.  
  36. def chose_next_pos(self, move):
  37. # When the snake is going to a direction, we defined the position of snake head
  38. # We take the new position
  39. if move != Move.Still:
  40. self.heading = move
  41.  
  42. # Now we have the position of snake head
  43. pos_x, pos_y = self.snake[0]
  44. # We take the position of different axis :
  45. head_x, head_y = self.heading.value
  46.  
  47. # Calcul of the next position :
  48. pos_x = pos_x + head_x
  49. pos_y = pos_y + head_y
  50.  
  51. # And return it :
  52. return (pos_x, pos_y)
  53.  
  54. def run_one_frame(self, move=Move.Still):
  55. # This function is going to refresh the game
  56.  
  57. # If the game is over we didn't do anything
  58. if self.live is False:
  59. print("Game is already over...")
  60. return
  61.  
  62. # Here we are going to add some code for the game
  63. next_pos = self.chose_next_pos(move)
  64. # Put the new position of the snake
  65. # self.snake.insert(0, next_pos)
  66. pos_x, pos_y = next_pos
  67. if self.is_blocked_by_wall(pos_x, pos_y) is True:
  68. return
  69. if self.is_apple_here(pos_x, pos_y):
  70. print('Ate an apple!')
  71. self.spawn_new_food()
  72. if len(self.snake) is self.size:
  73. print('You won!')
  74. else:
  75. # Remove a block, at the tail of the snake, to make it go forward.
  76. self.snake.pop()
  77. if self.is_in_snake(pos_x, pos_y) is True:
  78. # Stop the game
  79. self.live = False
  80. else:
  81. # Add the head of the snake
  82. self.snake.insert(0, (pos_x, pos_y))
  83.  
  84. def is_blocked_by_wall(self, pos_x, pos_y):
  85. if pos_x < 0 or pos_x >= self.width or pos_y < 0 or pos_y >= self.height:
  86. print('I am blocked...')
  87. return(True)
  88. return(False)
  89.  
  90. def is_apple_here(self, pos_x, pos_y):
  91. food_x, food_y = self.food
  92. if pos_x is food_x and pos_y is food_y:
  93. return True
  94. return False
  95.  
  96. def spawn_new_food(self):
  97. pos_x = random.randint(0, self.width - 1)
  98. pos_y = random.randint(0, self.height - 1)
  99.  
  100. while self.is_in_snake(pos_x, pos_y) is True:
  101. pos_x = random.randint(0, self.width - 1)
  102. pos_y = random.randint(0, self.height - 1)
  103. self.food = (pos_x, pos_y)
  104.  
  105. def is_in_snake(self, pos_x, pos_y):
  106. for node in self.snake:
  107. node_x, node_y = node
  108. if node_x is pos_x and node_y is pos_y:
  109. return True
  110. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement