Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1.  
  2. from turtle import *
  3. from random import randrange
  4. from freegames import square, vector
  5.  
  6. import time
  7. import board
  8. import busio
  9. import adafruit_mpr121
  10.  
  11. i2c = busio.I2C(board.SCL, board.SDA)
  12. mpr121 = adafruit_mpr121.MPR121(i2c)
  13.  
  14. food = vector(0, 0)
  15. snake = [vector(10, 0)]
  16. aim = vector(0, -10)
  17.  
  18. def change(x, y):
  19. "Change snake direction."
  20. aim.x = x
  21. aim.y = y
  22.  
  23. def inside(head):
  24. "Return True if head inside boundaries."
  25. return -200 < head.x < 190 and -200 < head.y < 190
  26.  
  27. def move():
  28.  
  29. "Move snake forward one segment."
  30. head = snake[-1].copy()
  31. head.move(aim)
  32.  
  33. if not inside(head) or head in snake:
  34. square(head.x, head.y, 9, 'red')
  35. update()
  36. return
  37.  
  38. snake.append(head)
  39.  
  40. if head == food:
  41. print('Snake:', len(snake))
  42. food.x = randrange(-15, 15) * 10
  43. food.y = randrange(-15, 15) * 10
  44. else:
  45. snake.pop(0)
  46.  
  47. clear()
  48.  
  49. for body in snake:
  50. square(body.x, body.y, 9, 'black')
  51.  
  52. square(food.x, food.y, 9, 'green')
  53. update()
  54.  
  55.  
  56. setup(420, 420, 370, 0)
  57. hideturtle()
  58. tracer(False)
  59.  
  60.  
  61.  
  62. while True:
  63.  
  64. # Loop through all 12 inputs (0-11).
  65. for i in range(12):
  66. # Call is_touched and pass it then number of the input. If it's touched
  67. # it will return True, otherwise it will return False.
  68. if mpr121[i].value:
  69. if i == 5:
  70. change(10, 0)
  71. elif i == 6:
  72. change(-10, 0)
  73. elif i == 0:
  74. change(0, 10)
  75. elif i == 9:
  76. change(0, -10)
  77.  
  78. time.sleep(0.2)
  79. move()
  80.  
  81.  
  82.  
  83. done()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement