import machine
import time
import random
from ili9341 import ILI9341, color565
# Initialize display
spi = machine.SPI(1, baudrate=32000000, sck=machine.Pin(10), mosi=machine.Pin(11))
display = ILI9341(spi, cs=machine.Pin(17), dc=machine.Pin(16), rst=machine.Pin(18))
# Variables for the game
basket_width = 30
basket_y = 200
ball_x, ball_y = random.randint(5, display.width - 5), 0
ball_size = 5
score = 0
# Set up potentiometer
pot = machine.ADC(26)
def draw_ball(x, y, color):
display.fill_circle(x, y, ball_size, color)
def draw_basket(x, color):
display.fill_rect(x, basket_y, basket_width, 10, color)
def display_score():
display.fill_rect(0, 0, display.width, 20, color565(0, 0, 0))
display.text(f"Score: {score}", 10, 10, color565(255, 255, 255))
while True:
# Clear previous ball
draw_ball(ball_x, ball_y, color565(0, 0, 0))
# Move the ball
ball_y += 2
if ball_y >= display.height:
# Check if the ball is caught in the basket
if basket_x <= ball_x <= basket_x + basket_width:
score += 1
# Reset ball
ball_x = random.randint(5, display.width - 5)
ball_y = 0
# Draw new ball
draw_ball(ball_x, ball_y, color565(255, 0, 0))
# Read potentiometer and update basket position
pot_value = pot.read_u16() >> 8
basket_x = int((pot_value / 255) * (display.width - basket_width))
# Clear previous basket and draw new position
draw_basket(basket_x, color565(0, 255, 0))
# Display score
display_score()
time.sleep(0.05)