from machine import Pin, I2C
from utime import sleep
from pico_i2c_lcd import I2cLcd
from random import randint
sda = Pin(0)
scl = Pin(1)
i2c = I2C(0, sda=sda, scl=scl, freq=40000)
devices = i2c.scan()
i2c_addr = devices[0]
lcd = I2cLcd(i2c, i2c_addr, 2, 16)
TARGET = (5, 0) # (x, y) coordinates
MAX_MOVES = 20
ball_x = randint(0, 16 - 1)
ball_y = randint(0, 2 - 1)
jumps = 0
lcd.clear()
print(f"Start: ({ball_x},{ball_y})")
print(f"Target: {TARGET}")
sleep(2)
while jumps < MAX_MOVES:
jumps += 1
# Generate new random position
new_x = randint(0, 16 - 1)
new_y = randint(0, 2 - 1)
# Update display
lcd.clear()
print(f"Jump {jumps}: ({new_x},{new_y})")
print(f"Target: {TARGET}")
# Check win condition
if (new_x, new_y) == TARGET:
lcd.clear()
print(f"Success in {jumps}!")
sleep(5)
ball_x, ball_y = new_x, new_y
sleep(0.5)
# Lost case
lcd.clear()
lcd.putstr(f"Failed after")
lcd.putstr(f"{MAX_MOVES} jumps", 1)
print(-1)
sleep(5)
# while True:
# lcd.clear()
# lcd.move_to(x, y)
# lcd.putstr('o')
# sleep(0.3)
# if y == 0:
# y = 1
# elif y == 1:
# y = 0
# x += randint(-1, 1)
# if x >= 16:
# x -= 1
# elif x < 0:
# x += 1