Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. from microbit import *
  2.  
  3. left = (-1, 0)
  4. right = (+1, 0)
  5. up = (0, -1)
  6. down = (0, +1)
  7.  
  8. turnLeft = {
  9. up: left,
  10. down: right,
  11. left: down,
  12. right: up
  13. }
  14.  
  15. turnRight = {
  16. up: right,
  17. down: left,
  18. left: up,
  19. right: down
  20. }
  21.  
  22. snake_direction = down
  23. snake = [(1,0), (1,1), (1,2)]
  24. print(snake)
  25.  
  26. frames = 0
  27. last_a = False
  28. last_b = False
  29.  
  30. def move(direction):
  31. global snake
  32. snake = snake[1:]
  33. head = snake[-1]
  34. new_head = ((head[0]+direction[0])%5, (head[1]+direction[1])%5)
  35. snake.append(new_head)
  36. print(snake)
  37.  
  38. while True:
  39. frames += 1
  40.  
  41. now_a = button_a.is_pressed()
  42. now_b = button_b.is_pressed()
  43.  
  44. if now_a and not last_a:
  45. snake_direction = turnLeft[snake_direction]
  46. if now_b and not last_b:
  47. snake_direction = turnRight[snake_direction]
  48.  
  49. last_a = now_a
  50. last_b = now_b
  51.  
  52. if frames % 10 == 0:
  53. move(snake_direction)
  54.  
  55. display.clear()
  56. for chunk in snake:
  57. display.set_pixel(chunk[0], chunk[1], 5)
  58.  
  59. sleep(50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement