from machine import Pin, I2C
from time import sleep
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
# LCD and button setup
I2C_ADDR = 0x27
I2C_NUM_ROWS = 2
I2C_NUM_COLS = 16
# Initialize I2C and LCD
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, I2C_NUM_ROWS, I2C_NUM_COLS)
# Button setup
button1 = Pin(19, Pin.IN, Pin.PULL_UP)
button2 = Pin(20, Pin.IN, Pin.PULL_UP)
button3 = Pin(21, Pin.IN, Pin.PULL_UP) # Removed unnecessary comment
# Questions and answers
questions = [
{"q": "Am I handsome?", "a": ["Yes", "No"], "correct": 0},
{"q": "How old am I?", "a": ["19", "Im still virgin"], "correct": 1}
]
# Game variables
current_question = 0
selected_option = 0
score = 0
# Display question on LCD
def display_question():
lcd.clear()
lcd.putstr(questions[current_question]["q"])
lcd.move_to(0, 1)
lcd.putstr("A:" + questions[current_question]["a"][0] + " B:" + questions[current_question]["a"][1])
# Check the answer and show result
def check_answer():
global score, selected_option # Added selected_option as global
if selected_option == questions[current_question]["correct"]:
score += 1
lcd.clear()
lcd.putstr("Correct!")
else:
lcd.clear()
lcd.putstr("Wrong!")
sleep(2)
# Move to the next question or end the game
def next_question():
global current_question
current_question += 1
if current_question < len(questions):
display_question()
else:
lcd.clear()
lcd.putstr(f"Game Over\nScore: {score}")
sleep(5)
reset_game()
# Reset game
def reset_game():
global current_question, score
current_question = 0
score = 0
lcd.clear()
lcd.putstr("Welcome!")
sleep(2)
display_question()
# Start the game
reset_game()
# Main loop
while True:
if button1.value() == 0: # Button 1 selects option A
selected_option = 0
check_answer()
next_question()
elif button2.value() == 0: # Button 2 selects option B
selected_option = 1
check_answer()
next_question()
elif button3.value() == 0: # Button 3 resets the game
reset_game()
sleep(0.2) # Increased sleep time slightly for better responsiveness