Guest User

Snake game in python

a guest
Oct 23rd, 2019
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. from microbit import *
  2. from random import randint
  3.  
  4. def random_point():
  5. """ This function returns a random point
  6. on the playing board
  7. """
  8. return [randint(0, 4), randint(0, 4)]
  9.  
  10.  
  11. class Snake:
  12. """ This class contains the functions that operate
  13. on our game as well as the state of the game.
  14. It's a handy way to link the two.
  15. """
  16.  
  17. def __init__(self):
  18. """ Special function that runs when you create
  19. a "Snake", ie. when you run
  20. game = Snake()
  21. init stands for "Initialisation"
  22. """
  23. ## UNCOMMENT AND FILL IN THE # LINES BELOW WITH START VALUES
  24. ## current direction is a string with up, down, left or right
  25. self.current_direction = "up"
  26. ## snake is a list of the pixels that the snake is at
  27. self.snake = [[2, 2]]
  28. ## food is the co-ords of the current food
  29. self.food = random_point()
  30. ## whether or not to end the game, used after update
  31. self.end = False
  32. pass
  33.  
  34. def handle_input(self):
  35. """ We'll use this function to take input from the
  36. user to control which direction the snake is going
  37. in.
  38. """
  39. x = accelerometer.get_x()
  40. y = accelerometer.get_y()
  41. # abs is the absolute function eg -1 => 1, 1 => 1
  42. if abs(x) > abs(y):
  43. if x < 0:
  44. self.current_direction = "left"
  45. else:
  46. self.current_direction = "right"
  47. else:
  48. if y < 0:
  49. self.current_direction = "up"
  50. else:
  51. self.current_direction = "down"
  52.  
  53. def update(self):
  54. """ This function will update the game state
  55. based on the direction the snake is going.
  56. """
  57. # The line below makes a copy of the head of the snake
  58. # you will be working with that copy in this function
  59. new_head = list(self.snake[-1])
  60. if self.current_direction == "up":
  61. new_head[1] -= 1
  62. elif self.current_direction == "down":
  63. new_head[1] += 1
  64. elif self.current_direction == "left":
  65. new_head[0] -= 1
  66. elif self.current_direction == "right":
  67. new_head[0] += 1
  68. if new_head[0] < 0:
  69. new_head[0] = 4
  70. elif new_head[0] > 4:
  71. new_head[0] = 0
  72. if new_head[1] < 0:
  73. new_head[1] = 4
  74. elif new_head[1] > 4:
  75. new_head[1] = 0
  76. if new_head in self.snake:
  77. self.end = True
  78.  
  79. self.snake.append(new_head)
  80. if new_head == self.food:
  81. self.food = random_point()
  82. while self.food in self.snake:
  83. self.food = random_point()
  84. else:
  85. self.snake = self.snake[1:]
  86. def draw(self):
  87. """ This makes the game appear on the LEDs. """
  88. display.clear()
  89. if self.food[0] >= 0 and self.food[0] <= 4 and self.food[1] >= 0 and self.food[1] <= 4:
  90. display.set_pixel(self.food[0], self.food[1], 5)
  91. for part in self.snake:
  92. if part[0] >= 0 and part[0] <= 4 and part[1] >= 0 and part[1] <= 4:
  93. display.set_pixel(part[0], part[1], 9)
  94.  
  95. # game is an "instance" of Snake
  96. game = Snake()
  97.  
  98. # this is called our "game loop" and is where everything
  99. # happens
  100. while not game.end:
  101. game.handle_input()
  102. game.update()
  103. game.draw()
  104. # this makes our micro:bit do nothing for 500ms
  105. sleep(500)
  106.  
  107. display.show("Game over")
Advertisement
Add Comment
Please, Sign In to add comment