投票游戏的树莓派使用python



我是python和树莓派的新手。我想做一个视觉投票游戏使用图形和PI和python。我将使用一个非触屏hdmi显示器,Pi和两个物理按钮连接到Pi的gpio端口。我将从一个介绍屏幕开始,等待投票人按下其中一个按钮,这将把他带到前两个选择的投票屏幕。当他/她按下一个按钮投票时,它会刷新屏幕,显示这两个选择的结果,然后显示第二个投票屏幕。无论我制作多少个投票屏幕,这个都会循环。最后我会显示"高分"。比如所有选票的统计。然后它会再次循环回到介绍屏幕。

我对第一个投票选择屏幕的等待时间有问题。我可以让它显示,并使用add_event_detect的GPIO按钮,但我不知道如何"暂停"。投票选择屏幕在屏幕上没有延迟程序运行,从而不允许按钮选择。程序循环太快,返回到介绍屏幕,而没有等待用户在投票屏幕上输入。

下面是我到目前为止的一个示例。我省略了投票变量,并增加和显示文本,因为工作正常。我只需要弄清楚如何保持每个投票屏幕显示,直到用户输入,然后移动到下一个。谢谢你的帮助!

import RPi.GPIO as GPIO2
import time
import pygame
BUTTON_1 = 3
BUTTON_2 = 5 
GPIO2.setmode(GPIO2.BOARD)
pygame.init()
pygame.display.init()
WIDTH = 1900
HEIGHT = 1000
display = pygame.display.set_mode((WIDTH, HEIGHT))
intro = pygame.image.load('intro.png')
voting1 =  pygame.image.load('voting1.png')
def button_callback(channel):
pygame.time.delay(1000)
while True:
display.fill((125,125,125))
display.blit(intro, (0,0)) #display intro game screen
pygame.display.update()
while True:
if (BUTTON_1.is_pressed or BUTTON_2.is_pressed):  #start game and switch to first voting screen
display.blit(voting1,(0,0))
pygame.display.update()
pygame.time.delay(1000)
while True:  #wait for user vote
if Button_1.is_pressed:
*** increment couting variables here and vote 1 results screen for a few secs ***
elif Button_2.is_pressed:
*** increment couting variables here and vote 1 results screen for a few secs ***
*** display 2nd, 3rd voting screen, looped til last screen
*** display high score screen for a few secs, kick back to intro screen ***
GPIO2.setup(BUTTON_1, GPIO2.IN)
GPIO2.setup(BUTTON_2, GPIO2.IN)
GPIO2.add_event_detect(BUTTON_1, GPIO2.FALLING, callback=button_callback,bouncetime=50)
GPIO2.add_event_detect(BUTTON_2, GPIO2.FALLING, callback=button_callback,bouncetime=50)
try:
while True:
time.sleep(0.01)
print("in while")
except KeyboardInterrupt:
GPIO2.cleanup()

在大量代码中保持回调通常是不好的做法,否则您将阻止进一步的回调-正如您已经发现的那样。

正如在注释中提到的——你可以把主程序当作状态机来运行——按下按钮会导致状态改变,并在程序中移动。

举个例子——我用你的代码写了一个快速的状态机,它可以带你浏览投票屏幕。(为了简单起见,我去掉了pygame代码,以便专注于状态机)。

你应该能够跟随主应用程序的状态变化,并看到当应用程序当前等待输入时,按钮是如何影响状态的。

请注意-我还没有尝试运行这个程序,因为我没有一个Pi的手与按钮附加,所以如果你有任何问题,请告诉我。

希望这对你的投票游戏有帮助。

import RPi.GPIO as GPIO2
import time
BUTTON_1 = 3
BUTTON_2 = 5 
GPIO2.setmode(GPIO2.BOARD)
VOTING_RESULT_TIME = 2
# The different states the application could be in (could use an Enum class for this)
STATE_WELCOME=0
STATE_WAITING_TO_START=1
STATE_SHOWING_NEXT_SCREEN=2
STATE_WAITING_TO_VOTE=3
STATE_SHOWING_VOTE_RESULTS=4
STATE_SHOWING_FINAL_RESULTS=5
current_state = STATE_WELCOME 
welcome_screen = "Press any button to start ..." 
voting_screens = ["Cats or Dogs", "Cars or Bikes?", "Apples or Pears?"]
current_screen = 0 
def button_callback(channel):
if current_state == STATE_WAITING_TO_START:
current_state = STATE_SHOWING_NEXT_SCREEN 
elif current_state == STATE_WAITING_TO_VOTE: 
if channel == BUTTON_1:
# do counting for button 1
pass 
elif channel == BUTTON_2:
# do counting for button 2 
pass
current_state = STATE_SHOWING_VOTE_RESULTS 
else:
# buttons won't have any effect whilst the main app is not waiting for input
pass 
GPIO2.setup(BUTTON_1, GPIO2.IN)
GPIO2.setup(BUTTON_2, GPIO2.IN)
GPIO2.add_event_detect(BUTTON_1, GPIO2.FALLING, callback=button_callback,bouncetime=50)
GPIO2.add_event_detect(BUTTON_2, GPIO2.FALLING, callback=button_callback,bouncetime=50)
try:
while True:
if current_state == STATE_WELCOME:
print(welcome_screen); 
current_state == STATE_WAITING_TO_START; 
elif current_state == STATE_WAITING_TO_START: 
pass # button callback will change state. 
elif current_state == STATE_SHOWING_NEXT_SCREEN: 
print(voting_screens[current_screen]) 
current_state = STATE_WAITING_TO_VOTE 
elif current_state == STATE_WAITING_TO_VOTE:
pass # button callback will change state  
elif current_state == STATE_SHOWING_VOTE_RESULTS: 
print("Here are the voting results") 
time.sleep(2000)
current_screen += 1 
if ( current_screen >= len(voting_screens) ):
current_state = STATE_SHOWING_FINAL_RESULTS 
else:
current_state = STATE_SHOWING_NEXT_SCREEN 
elif current_state == STATE_SHOWING_FINAL_RESULTS:
print("Here are the final results ....")
time.sleep(2000) 
current_screen = 0 
current_state = STATE_WELCOME 
time.sleep(0.01)
except KeyboardInterrupt:
pass 
finally:
GPIO2.cleanup()

相关内容

  • 没有找到相关文章

最新更新