pygame音乐播放时的用户输入

时间:2019-01-12 16:17:19

标签: python raspberry-pi pygame

我最近拿起Raspberry PI并有几个小时的空闲时间,所以我决定创建一个有趣的闹钟。我已经写了一些样板代码,只是为了模拟基本流程,但是我在努力如何同时发出警报音,同时还要听取用户输入(打no按钮)方面感到困惑。

import RPi.GPIO as GPIO
import time
import datetime
import pygame


class Alarm:
    def __init__(self, dayOfWeek, timeOfDay):
               self.dayOfWeek = dayOfWeek
               self.timeOfDay = timeOfDay
    def CheckAlarmState(self):
               for day in self.dayOfWeek:
                   if(day == datetime.datetime.today().weekday()):
                       pygame.mixer.music.play()
                       GPIO.output(18, GPIO.HIGH)
                       time.sleep(1)
                       GPIO.output(18, GPIO.LOW)


pygame.mixer.init()
pygame.mixer.music.load("myFile.wav")
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT)
dayOfWeek = [0,1,2,3,4,5]
alarm = Alarm(dayOfWeek, 5)
alarm.CheckAlarmState()

0 个答案:

没有答案