'''
Voltaat Learn (http://learn.voltaat.com)
Link to the full tutorial:
Tutorial: Building a memory-strengthening game using a Raspberry Pi Pico board.
The function of this code is to build a memory-strengthening game that runs on the
Raspberry Pi Pico and challenges the user to remember and repeat sequences.
Note: You can use this sketch with any Raspberry Pi Pico.
'''
from machine import Pin, PWM
import utime
import random
# تعريف الثوابت
CHOICE_OFF = 0
CHOICE_NONE = 0
CHOICE_RED = 1
CHOICE_GREEN = 2
CHOICE_BLUE = 4
CHOICE_YELLOW = 8
# تعريف أرقام المنافذ
BUTTON_BLUE = 15
LED_BLUE = 16
BUTTON_RED = 14
LED_RED = 17
BUTTON_YELLOW = 13
LED_YELLOW = 18
BUTTON_GREEN = 12
LED_GREEN = 19
BUZZER1 = 9
BUZZER2 = 10
# معاملات اللعبة
ROUNDS_TO_WIN = 13
ENTRY_TIME_LIMIT = 3000
# متغيرات حالة اللعبة
game_board = [0] * 32
game_round = 0
# تهيئة المنافذ
button_red = Pin(BUTTON_RED, Pin.IN, Pin.PULL_UP)
button_green = Pin(BUTTON_GREEN, Pin.IN, Pin.PULL_UP)
button_blue = Pin(BUTTON_BLUE, Pin.IN, Pin.PULL_UP)
button_yellow = Pin(BUTTON_YELLOW, Pin.IN, Pin.PULL_UP)
led_red = Pin(LED_RED, Pin.OUT)
led_green = Pin(LED_GREEN, Pin.OUT)
led_blue = Pin(LED_BLUE, Pin.OUT)
led_yellow = Pin(LED_YELLOW, Pin.OUT)
buzzer1 = Pin(BUZZER1, Pin.OUT)
buzzer2 = Pin(BUZZER2, Pin.OUT)
def setup():
"""تهيئة اللوحة"""
# تمت التهيئة في تعريف الكائنات أعلاه
play_winner() # ترحيب بعد اكتمال التهيئة
def set_leds(leds):
"""تشغيل/إطفاء الأنوار حسب القيمة المعطاة"""
led_red.value(1 if (leds & CHOICE_RED) else 0)
led_green.value(1 if (leds & CHOICE_GREEN) else 0)
led_blue.value(1 if (leds & CHOICE_BLUE) else 0)
led_yellow.value(1 if (leds & CHOICE_YELLOW) else 0)
def check_button():
"""التحقق من الضغط على الأزرار"""
if button_red.value() == 0: # ملاحظة: 0 لأننا نستخدم PULL_UP
return CHOICE_RED
elif button_green.value() == 0:
return CHOICE_GREEN
elif button_blue.value() == 0:
return CHOICE_BLUE
elif button_yellow.value() == 0:
return CHOICE_YELLOW
return CHOICE_NONE
def buzz_sound(buzz_length_ms, buzz_delay_us):
"""تشغيل صوت البوزر"""
buzz_length_us = buzz_length_ms * 1000
while buzz_length_us > (buzz_delay_us * 2):
buzz_length_us -= buzz_delay_us * 2
buzzer1.value(0)
buzzer2.value(1)
utime.sleep_us(buzz_delay_us)
buzzer1.value(1)
buzzer2.value(0)
utime.sleep_us(buzz_delay_us)
def toner(which, buzz_length_ms):
"""تشغيل LED والصوت المرتبط"""
set_leds(which)
if which == CHOICE_RED:
buzz_sound(buzz_length_ms, 1136)
elif which == CHOICE_GREEN:
buzz_sound(buzz_length_ms, 568)
elif which == CHOICE_BLUE:
buzz_sound(buzz_length_ms, 851)
elif which == CHOICE_YELLOW:
buzz_sound(buzz_length_ms, 638)
set_leds(CHOICE_OFF)
def wait_for_button():
"""انتظار ضغط زر من المستخدم"""
start_time = utime.ticks_ms()
while utime.ticks_diff(utime.ticks_ms(), start_time) < ENTRY_TIME_LIMIT:
button = check_button()
if button != CHOICE_NONE:
toner(button, 150)
# انتظار حتى يترك المستخدم الزر
while check_button() != CHOICE_NONE:
pass
utime.sleep_ms(10) # للمساعدة في منع الاهتزاز
return button
return CHOICE_NONE
def add_to_moves():
"""إضافة حركة عشوائية جديدة إلى التسلسل"""
global game_round
new_button = random.randint(0, 3)
if new_button == 0:
new_button = CHOICE_RED
elif new_button == 1:
new_button = CHOICE_GREEN
elif new_button == 2:
new_button = CHOICE_BLUE
elif new_button == 3:
new_button = CHOICE_YELLOW
game_board[game_round] = new_button
game_round += 1
def play_moves():
"""تشغيل تسلسل الحركات الحالي"""
for current_move in range(game_round):
toner(game_board[current_move], 150)
utime.sleep_ms(150)
def play_memory():
"""اللعبة الرئيسية للذاكرة"""
global game_round
random.seed(utime.ticks_ms())
game_round = 0
while game_round < ROUNDS_TO_WIN:
add_to_moves()
play_moves()
for current_move in range(game_round):
choice = wait_for_button()
if choice == 0:
return False
if choice != game_board[current_move]:
return False
utime.sleep_ms(1000)
return True
def winner_sound():
"""صوت الفوز"""
for x in range(250, 70, -1):
for y in range(3):
buzzer2.value(1)
buzzer1.value(0)
utime.sleep_us(x)
buzzer2.value(0)
buzzer1.value(1)
utime.sleep_us(x)
def play_winner():
"""تشغيل عرض الفوز"""
set_leds(CHOICE_GREEN | CHOICE_BLUE)
winner_sound()
set_leds(CHOICE_RED | CHOICE_YELLOW)
winner_sound()
set_leds(CHOICE_GREEN | CHOICE_BLUE)
winner_sound()
set_leds(CHOICE_RED | CHOICE_YELLOW)
winner_sound()
def play_loser():
"""تشغيل عرض الخسارة"""
set_leds(CHOICE_RED | CHOICE_GREEN)
buzz_sound(255, 1500)
set_leds(CHOICE_BLUE | CHOICE_YELLOW)
buzz_sound(255, 1500)
set_leds(CHOICE_RED | CHOICE_GREEN)
buzz_sound(255, 1500)
set_leds(CHOICE_BLUE | CHOICE_YELLOW)
buzz_sound(255, 1500)
def attract_mode():
"""وضع الجذب - عرض أثناء انتظار ضغط الزر"""
while True:
set_leds(CHOICE_RED)
utime.sleep_ms(100)
if check_button() != CHOICE_NONE:
return
set_leds(CHOICE_BLUE)
utime.sleep_ms(100)
if check_button() != CHOICE_NONE:
return
set_leds(CHOICE_GREEN)
utime.sleep_ms(100)
if check_button() != CHOICE_NONE:
return
set_leds(CHOICE_YELLOW)
utime.sleep_ms(100)
if check_button() != CHOICE_NONE:
return
# الدالة الرئيسية
def main():
setup()
while True:
attract_mode()
# الإشارة لبداية اللعبة
set_leds(CHOICE_RED | CHOICE_GREEN | CHOICE_BLUE | CHOICE_YELLOW)
utime.sleep_ms(1000)
set_leds(CHOICE_OFF)
utime.sleep_ms(250)
# تشغيل اللعبة والتعامل مع النتيجة
if play_memory():
play_winner() # الفوز
else:
play_loser() # الخسارة
# تشغيل البرنامج
if __name__ == "__main__":
main()