Advertisement
Guest User

Untitled

a guest
Dec 15th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. """Snake-like game for the microbit."""
  2. from microbit import *
  3. from random import randint
  4. from math import floor
  5.  
  6. # constants (feel free to toy around with them)
  7. SCREEN_SIZE = 5
  8. SPEED = 250
  9. PLAYER_BRIGHTNESS = 5
  10.  
  11.  
  12. def render(player):
  13. """Creates a new Image containing the player.
  14.  
  15. Args:
  16. player (tuple): The players position
  17.  
  18. Returns:
  19. An Image object that can be drawn to the LED Matrix
  20.  
  21. """
  22.  
  23. # your code for exercise (a)
  24.  
  25. image = Image("00000:00000:00000:00000:00000")
  26. image.set_pixel(player[0], player[1], PLAYER_BRIGHTNESS)
  27. return image
  28.  
  29.  
  30. def update(player, direction):
  31. """Moves the player from its current position one step in the specified
  32. direction. Returns the players new position.
  33.  
  34. Args:
  35. player (tuple): The player position
  36. direction (str): Either 'N', 'S', 'W', or 'E' representing the four
  37. cardinal directions
  38.  
  39. Returns:
  40. The new player position (tuple)
  41.  
  42. """
  43. # your code for exercise (b)
  44.  
  45. # liste wird verwendet, da tupel nicht bearbeitet werden koennen
  46. pliste = list(player)
  47.  
  48. if direction == "N":
  49. if pliste[1] < 4:
  50. pliste[1] += 1
  51.  
  52. if direction == "E":
  53. if pliste[0] < 4:
  54. pliste[0] +=1
  55.  
  56. if direction == "S":
  57. if pliste[1] > 0:
  58. pliste[1] -= 1
  59.  
  60. if direction == "W":
  61. if pliste[0] > 0:
  62. pliste[0] -= 1
  63.  
  64. return tuple(pliste)
  65.  
  66.  
  67.  
  68. def process_input(direction):
  69. """Computes direction changes based on user input. A press of button `B`
  70. changes the direction 90 degrees clockwise, while button `A` changes it 90
  71. degrees anti clockwise.
  72.  
  73. Args:
  74. direction ('N', 'S', 'W', or 'E'): The current player direction
  75.  
  76. Returns:
  77. The new player direction ('N', 'S', 'W', or 'E')
  78.  
  79. """
  80. # your code for exercise (c)
  81.  
  82. # gegen den uhrzeigersinn
  83. if button_a.was_pressed() == True:
  84. if direction == 'N':
  85. return 'W'
  86. if direction == 'W':
  87. return 'S'
  88. if direction == 'S':
  89. return 'E'
  90. if direction == 'E':
  91. return 'N'
  92.  
  93.  
  94. # im uhrzeigersinn
  95. elif button_b.was_pressed() == True:
  96. if direction == 'N':
  97. return 'E'
  98. if direction == 'E':
  99. return 'S'
  100. if direction == 'S':
  101. return 'W'
  102. if direction == 'W':
  103. return 'N'
  104.  
  105. return direction
  106.  
  107. if __name__ == "__main__":
  108. # initialize player position and direction
  109. player = (2, 4)
  110. direction = 'N'
  111. # game loop
  112. while True:
  113. direction = process_input(direction)
  114. player = update(player, direction)
  115. display.show(render(player))
  116. sleep(SPEED)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement