Python 当按下按钮时,树莓圆周率游戏崩溃

Python 当按下按钮时,树莓圆周率游戏崩溃,python,raspberry-pi,gpio,Python,Raspberry Pi,Gpio,我有一个在Raspberry Pi上运行的Python程序: import RPi.GPIO as GPIO import time from random import randint # Setup GPIO pins # Set the BCM mode GPIO.setmode(GPIO.BCM) # Outputs GPIO.setup(4, GPIO.OUT) GPIO.setup(17, GPIO.OUT) GPIO.setup(27, GPIO.OUT) # Ensure

我有一个在Raspberry Pi上运行的Python程序:

import RPi.GPIO as GPIO
import time
from random import randint

# Setup GPIO pins
# Set the BCM mode
GPIO.setmode(GPIO.BCM)

# Outputs
GPIO.setup(4, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)

# Ensure all LED's are off
GPIO.output(4, GPIO.LOW)
GPIO.output(17, GPIO.LOW)
GPIO.output(27, GPIO.LOW)

# Inputs
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

global player
player = 0

# Setup the callback functions
def rock(channel):
    global player
    player = 1 # magic number 1 = rock, pin 12

def paper(channel):
    global player
    player = 2 # magic number 2 = paper, pin 16

def scissors(channel):
      global player
      player = 3 # magic number 3 = scissors, pin 21

def quit_game(channel):
    GPIO.cleanup()
    exit() # pin 20, immediate exit of game

# Add event detection and callback assignments
GPIO.add_event_detect(12, GPIO.RISING, callback=rock)
GPIO.add_event_detect(16, GPIO.RISING, callback=paper)
GPIO.add_event_detect(21, GPIO.RISING, callback=scissors)
GPIO.add_event_detect(20, GPIO.RISING, callback=quit_game)

# Computer random pick
computer = randint(1, 3)

while True:

    if player == computer:
        # This is a tie condition
        GPIO.output(27, GPIO.HIGH)
        time.sleep(5)
        GPIO.output(27, GPIO.LOW)
        player = 0
    elif player == 1:
        if computer == 2:
            # Player loses, paper covers rock
            GPIO.output(17, GPIO.HIGH)
            print "Player = rock, Computer = paper\n"
            time.sleep(5)
            GPIO.output(17, GPIO.LOW)
            player = 0
        else:
            # Player wins, rock dulls paper
            GPIO.output(4, GPIO.HIGH)
            print "Player = rock, Computer = scissors\n"
            time.sleep(5)
            GPIO.output(4, GPIO.LOW)
            player = 0
    elif player == 2:
        if computer == 3:
            # Player loses, scissors cut paper
            GPIO.output(17, GPIO.HIGH)
            print "Player = paper, Computer = scissors\n"
            time.sleep(5)
            GPIO.output(17, GPIO.LOW)
            player = 0
        else:
            # Player wins, paper covers rock
            GPIO.output(4, GPIO.HIGH)
            print "Player = paper, Computer = rock\n"
            time.sleep(5)
            GPIO.output(4, GPIO.LOW)
            player = 0
    elif player == 3:
        if computer == 1:
            # Player loses, rock dulls scissors
            GPIO.output(17, GPIO.HIGH)
            print "Player = scissors, Computer  = rock\n"
            time.sleep(5)
            GPIO.output(17, GPIO.LOW)
            player = 0
        else:
            # Player wins, scissors cut paper
            GPIO.output(4, GPIO.HIGH)
            print "Player = scissors, Computer = paper\n"
            time.sleep(5)
            GPIO.output(4, GPIO.LOW)
            player = 0

    # Another random pick for the computer
    computer = randint(1, 3)
这是一个石头,布,剪刀的游戏,是在一个电路板上创建的。我根据此图构建了电路:

这是我构建的实际电路的图像:

当我在Raspbery Pi上运行程序时,它会毫无问题地启动,但如果我按下一个按钮,程序就会崩溃。我可以看到,在应用程序崩溃之前,print语句被打印到控制台。例如,
Player=rock,Computer=paper
。这应该在LED打开后发生,但LED从不亮起

有人能提出可能的调查方案吗

更新

好的,按钮应该连接到引脚12、16、20和21,但实际上连接到GND、20、21,什么都没有。一旦我纠正了程序停止崩溃的错误。不过,这个问题似乎仍然存在

我现在在我的
Quit
函数中添加了
print“Quit game”
,并注意到无论我按哪个按钮,它似乎都会被调用:

def quit(channel):
    print "Quit game"
    GPIO.cleanup()
    exit() # pin 20, immediate exit of game

$ python rock_paper_scissors/prs_with_LEDs_and_switches.py 
Player = scissors, Computer = rock
Quit game

我的实验板有问题。当我在新线路上创建线路时,没有问题。

您能定义“崩溃”吗?你得到了Python的回溯吗?分割错误?还有别的吗?请在您的问题中包含任何特定的错误消息。不要获取任何错误或错误。这就是我得到的全部:
pi@raspberrypi:~/AI$python rock\u paper\u剪刀/prs\u带指示灯和开关。py播放器=剪刀,计算机=布