# https://wokwi.com/projects/383873092592043009
from machine import Pin, I2C, ADC
import ssd1306
import time
SCL = 17
SDA = 16
# 初始化OLED顯示器
i2c = I2C(0, scl=Pin(SCL), sda=Pin(SDA), freq=400000)
display = ssd1306.SSD1306_I2C(128, 64, i2c)
# 初始化搖桿模塊
joy_x = ADC(Pin(26))
joy_y = ADC(Pin(27))
joy_button = Pin(21, Pin.IN, Pin.PULL_UP)
# 遊戲選單
games = ["Tetris", "Breakout", "Greedy Snake"]
index = 0
def menu():
global index
display.fill(0)
display.text("Select Game", 0, 0)
for i, game in enumerate(games):
if i == index:
display.text("> " + game, 0, 10 + 10*i)
else:
display.text(game, 0, 10 + 10*i)
display.show()
while True:
if joy_y.read_u16() < 500:
index -= 1
if index < 0:
index = len(games) - 1
menu()
if joy_y.read_u16() > 60000:
index += 1
if index >= len(games):
index = 0
menu()
if joy_button.value() == 0:
return index
# 俄羅斯方塊遊戲
def tetris():
display.fill(0)
# 在這裡加入俄羅斯方塊遊戲的邏輯和畫面繪製
display.text("Tetris Game", 0, 24)
display.show()
time.sleep(2)
# 打磚塊遊戲
def breakout():
display.fill(0)
# 在這裡加入打磚塊遊戲的邏輯和畫面繪製
display.text("Breakout Game", 0, 24)
display.show()
time.sleep(2)
# 貪食蛇遊戲
def greedy_snake():
display.fill(0)
# 在這裡加入貪食蛇遊戲的邏輯和畫面繪製
display.text("Greedy Snake Game", 0, 24)
display.show()
time.sleep(2)
while True:
game = menu()
if game == 0:
tetris()
elif game == 1:
breakout()
elif game == 2:
greedy_snake()