按下按钮会使Raspberry Pi游戏崩溃

时间:2019-04-21 12:19:35

标签: 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 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)

这是一种石头,纸,剪刀的游戏,将在面包板上创建的电路上进行游戏。我根据此图构建了电路:

Diagram of circuit for Rock, Paper, Scissors game

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

Actual circuit for Rock, Paper, Scissors game

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

有人可以建议调查的可能途径吗?

更新

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

我现在已经将print "Quit game"添加到了quit函数中,并且注意到无论我按哪个按钮,它似乎都被调用了:

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

1 个答案:

答案 0 :(得分:0)

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