1

New to programming, please bear with me.......

I have a rock/paper/scissors game that works perfectly. I know I probably could have done it with 10% of the code I actually used, but please use my code to explain to me, it's how I understand it at the moment. I want to add a button (GPIO03) somewhere in the code to quit the game completely. I have, for 2 days, tried everything. but.is_pressed, but.wait_for_press etc. I just can't get it working. Maybe there is something I don't know about yet or I'm just using it wrong. Could someone please add or change the code to exit the game completely when the button is pressed anywhere in the game? I'm at my wits end......

from gpiozero import LED, Buzzer, Button   
from time import sleep   
buz = Buzzer(2)   
but = Button(3)   

def rps():   
    ledg = LED(15)   
    ledr = LED(14)   
    ledw = LED(18)   

if p1 == 'r' and p2 == 'r':   
    print("Draw!!")   
    ledw.on()   
    sleep(3)   
    ledw.off()   
elif p1 == 'p' and p2 == 'p':    
    print("Draw!!")    
    ledw.on()   
    sleep(3)    
    ledw.off()    
elif p1 == 's' and p2 == 's':    
    print("Draw!!")    
    ledw.on()    
    sleep(3)    
    ledw.off()   
elif p1 == 'r' and p2 == 'p':    
    print("P2 wins!!")    
    ledr.on()     
    sleep(3)    
    ledr.off()    
elif p1 == 'r' and p2 == 's':    
    print("P1 wins!!")    
    ledg.on()    
    sleep(3)    
    ledg.off    
elif p1 == 'p' and p2 == 'r':    
    print("P1 wins!!")    
    ledg.on()    
    sleep(3)    
    ledg.off    
elif p1 == 'p' and p2 == 's':    
    print("P2 wins!!")    
    ledr.on()   
    sleep(3)   
    ledr.off()    
elif p1 == 's' and p2 == 'r':   
    print("P2 wins!!")   
    ledr.on()   
    sleep(3)   
    ledg.off   
elif p1 == 's' and p2 == 'p':   
    print("P1 wins!!")   
    ledg.on()   
    sleep(3)   
    ledr.off()         

while True:                  
    p1 = input("Choose r/p/s P1: (Type 'quit' to exit) ")            
    if p1 == 'r' or p1 == 'p' or p1 == 's':          
        pass      
    elif p1 == 'quit':   
        print("Bye!!")   
        break   
    else:     
        print("Invalid choice P1. Try again")    
        buz.on()   
        sleep(1)   
        buz.off()    
        continue   

    while True:   
        p2 = input("Choose r/p/s P2: (Type 'quit' to exit) ")   
        if p2 == 'r' or p2 == 'p' or p2 == 's':   
            pass   
        elif p2 == 'quit':   
            print("Chow!!")   
            return   
        else:   
            print("Invalid choice P2. Try again")   
            buz.on()   
            sleep(1)   
            buz.off()    
            continue   

        rps()   
        break   

Thanks

ben_nuttall
  • 859
  • 10
  • 20
Frans
  • 19
  • 3

1 Answers1

0

Your main game loop is the while loop. Instead of while True you could have while running and then set running to False when you want your game to end. You can then use gpiozero's btn.when_pressed callback feature to have a button set running to False at any point, separately from your game loop.

For example:

stop_btn = Button(20)

def end_game():
    global running
    running = False

btn.when_pressed = end_game

running = True
while running:
    i = input("Enter a number or type 'quit': ")
    if i == 'quit':
        running = False
        continue
    if i == '1':
        print("running")
print("finished")

However, you must realise that it will have to complete the loop iteration for the while condition to end, so in your version, if you're stuck at the input line you'll have to continue to the end of the iteration before it will stop.

ben_nuttall
  • 859
  • 10
  • 20
  • Hi Ben, thanks for the reply, much appreciated! Ok so there is no way to terminate the game while at the input line? Instead of entering 'r' for rock, for example, to press the button and have something print like 'cheerio', for example, and exiting the loop? I have been trying to insert an elif statement where if the input for p1 or p2 is the button that is pressed to break out of the loop or something (like my 'quit' options), but in 2 days haven't gotten it to work. So probably like you say, not possible. Will also play around with 'while running'. Thanks Ben – Frans Jan 24 '19 at 13:11
  • Oh I didn't see `Type quit to exit`. I would just do `if p1 == 'quit': running = False; continue` – ben_nuttall Jan 24 '19 at 13:32
  • I've updated my answer which I think properly does what you wanted – ben_nuttall Jan 24 '19 at 13:39
  • Thanks Ben, will check it out tomorrow when I'm at my pc and RPI again - I haven't done that yet! Holding thumbs..... :-) – Frans Jan 24 '19 at 17:55