import random
import time
from machine import Pin, i2c
import ssd1386
SCREEN_WIDTH = 128
SCREEN_HEIGHT = 64
datoH=ADC(Pin (35))
datoV=ADC(Pin (32))
boton=Pin(13, Pin.IN, Pin.PULL_UP)
i2c = I2C(0, scl=Pin(22), sda=Pin(23))
SNAKE_PIECE_SIZE = 3
MAX_SNAKE_LENGTH = 150
MAP_SIZE_X = 20
MAP_SIZE_Y = 20
START_SNAKE_SIZE = 5
SNAKE_MOVE_DELAY = 30
class State(object):
START = 0
RUNNING = 1
GAMEOVER = 2
@classmethod
def setter(cls, state):
if state = cls.START
return cls.START
elif state == cls.RUNNING
return cls.RUNNING
elif state == cls.GAMEOVER:
return cls.GAMEOVER
class Direction(object)
UP= 0
LEFT = 1
DOMN = 2
RIGHT = 3
@classmethod
def setter(cls, dirc):
if dirc == cls.UP:
return cls.Up
elif dirc -- cls.DOWN:
return cls.DOWN
elif dirc — cls.LEFT
return cls.LEFT
elif dirc -- cls.RIGHT
return cls.RIGHT
i2c = I2C(0)
screen = ssd1306.SSD1306_I2C(SCREEN_WIDTH, SCREEN_HEIGHT,I2C(0))
class Snake(object)
def _init_ (self):
self.snake = []
self.fruit = [] 7
self.snake_length = START_SNAKE_SIZE
self direction = Direction.RIGHT +
self.new direction = Direction. RIGHT
self.game_state = None
self display = screen
self.setup_game()
def setup_game(self)
self.game_state = State.START
direction = Direction. RIGHT
new_direction = Direction. RIGHT
self.reset_snake()
self.generate fruit()
self.display.fill(0)
self.draw_map()
self. show score()
self. show press_to_start()
self display.show()
def reset_snake(self)
self.snake = [] +2
self.snake_length = START_SNAKE_SIZE
for i in range(self.snake length):
self. snake. append( (MAP_SIZE_X // 2-i,MAP_SIZE_Y // 2))
def check fruit (self):
if self.snake[0][0] == self.fruit[0] and self.snake[0][1] == self.fruit[1]:
if self.snake length + 1 < MAX SNAKE LENGTH:
self.snake_length += 1
self. snake.insert(0, (self.fruit[0], selfFruit[1]))
self.generate_fruit()
def generate fruit(self):
while True:
self.fruit = [random.randint(1, MAP_SIZE X - 1), random.randint(1, MAP_SIZE Y - 1)]
fruit - tuple(self.fruit)
if fruit in self.snake:
continue
else:
print('fruit: ', self.fruit)
break
@staticmethod
def button press():
for pin in datoH.read(),datoV.read():
if pin.value() < 200 or pin.value() < 3800:
return True
return False
def read direction(self):
EEES, ARES AMAR"
for direction, pin in enumerate((UP_PIN, LEFT PIN, DOWN PIN, RIGHT_PIN))
1f pin.value() — e and not (direction —- (self.direction + 2) % 4):
self. new direction - Direction. setter(direction)
return
# print( datoH.read(), datoV.read(), boton.value())
# if datoH.read()<200:
# oled.text("derecha",72,28,0)
# oled.show()
# elif datoH.read()>3800:
# oled.text("izq",16,28,0)
# oled.show()
# elif datoV.read()>3800:
# oled.text("arriba",40,12,0)
# oled.show()
# elif datoV.read()<200:
# oled.text("abajo",40,44,0)
# oled.show()
# from machine import Pin,ADC, I2C
# from utime import sleep_ms
# from ssd1306 import SSD1306_I2C
# import framebuf
# ancho = 128
# alto = 64
# datoH=ADC(Pin (35))
# datoV=ADC(Pin (32))
# boton=Pin(13, Pin.IN, Pin.PULL_UP)
# i2c = I2C(0, scl=Pin(22), sda=Pin(23))
# oled = SSD1306_I2C(ancho, alto, i2c)
# oled.fill(1)
# pausa=50
# for i in range (15):
# oled.text("arquitectura",i*8,28,0)
# sleep_ms(pausa)
# oled.show()
# oled.text("arquitectura",i*8,28,1)
# def lineas():
# oled.fill(1)
# oled.line(0,0,128,64,0)
# oled.line(128,0,0,64,0)
# oled.show()
# lineas()
# #while True:
# #
# # print( datoH.read(), datoV.read(), boton.value())
# # if datoH.read()<200:
# # oled.text("derecha",72,28,0)
# # oled.show()
# # elif datoH.read()>3800:
# # oled.text("izq",16,28,0)
# # oled.show()
# # elif datoV.read()>3800:
# # oled.text("arriba",40,12,0)
# # oled.show()
# # elif datoV.read()<200:
# # oled.text("abajo",40,44,0)
# # oled.show()
# #
# #
# # else:
# # lineas()