Raspberry Pi 按下按钮时游戏崩溃
Raspberry Pi game crashes out when button is pressed
我有这个 Python 程序,我正在 运行 上 Raspberry Pi:
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,什么都没有。一旦我纠正程序停止崩溃。不过问题似乎仍然存在。
我现在已将 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
我的面包板有问题。当我在一个新电路上创建电路时没有问题。
我有这个 Python 程序,我正在 运行 上 Raspberry Pi:
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,什么都没有。一旦我纠正程序停止崩溃。不过问题似乎仍然存在。
我现在已将 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
我的面包板有问题。当我在一个新电路上创建电路时没有问题。