Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from turtle import *
- from random import randrange
- from freegames import square, vector
- import time
- import board
- import busio
- import adafruit_mpr121
- i2c = busio.I2C(board.SCL, board.SDA)
- mpr121 = adafruit_mpr121.MPR121(i2c)
- food = vector(0, 0)
- snake = [vector(10, 0)]
- aim = vector(0, -10)
- def change(x, y):
- "Change snake direction."
- aim.x = x
- aim.y = y
- def inside(head):
- "Return True if head inside boundaries."
- return -200 < head.x < 190 and -200 < head.y < 190
- def move():
- "Move snake forward one segment."
- head = snake[-1].copy()
- head.move(aim)
- if not inside(head) or head in snake:
- square(head.x, head.y, 9, 'red')
- update()
- return
- snake.append(head)
- if head == food:
- print('Snake:', len(snake))
- food.x = randrange(-15, 15) * 10
- food.y = randrange(-15, 15) * 10
- else:
- snake.pop(0)
- clear()
- for body in snake:
- square(body.x, body.y, 9, 'black')
- square(food.x, food.y, 9, 'green')
- update()
- setup(420, 420, 370, 0)
- hideturtle()
- tracer(False)
- while True:
- # Loop through all 12 inputs (0-11).
- for i in range(12):
- # Call is_touched and pass it then number of the input. If it's touched
- # it will return True, otherwise it will return False.
- if mpr121[i].value:
- if i == 5:
- change(10, 0)
- elif i == 6:
- change(-10, 0)
- elif i == 0:
- change(0, 10)
- elif i == 9:
- change(0, -10)
- time.sleep(0.2)
- move()
- done()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement