I'm new at python and raspberry pi. I was looking to make a visual voting game using graphics and the PI and python. I will be using a non touch screen hdmi in display, the Pi, and two physical buttons hooked up to the Pi's gpio ports. i was going to start with an introductory screen, and wait for a voter to push one of the buttons, which would then bring him to the first 2 choice voting screen. When he/she pushes a button to vote, it would refresh the screen to the results for those 2 choices for a few seconds, then bring up the 2nd voting screen. This would loop for however many voting screens i make. At the end I would display a "high score" like tally of all the votes. And then it would loop back to the introductory screen again.
I am having issues with the wait time for the first voting selection screen. I can get it to display, and am using the add_event_detect for the GPIO buttons, but i don't know how to "pause" the voting selection screen on screen without delaying the program from running, thus not allowing the button selection to be made. The program loops too fast and goes back to the intro screen and does not wait for user input on the voting screen.
Below is a sample of what i have so far. I have omitted the voting variables and incrementing and displaying the text as that works ok. I just need to figure out how to keep each voting screen displayed until user input and then move onto the next. Mahalo for the help!
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()