Игра Raspberry Pi вылетает при нажатии кнопки - PullRequest
0 голосов
/ 21 апреля 2019

У меня есть программа на 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)

Это игра «камень, бумага, ножницы», в которую нужно играть по схеме, созданной на макете.Я построил схему на основе этой схемы:

Diagram of circuit for Rock, Paper, Scissors game

Это изображение фактической схемы, которую я построил:

Actual circuit for Rock, Paper, Scissors game

Когда я запускаю программу на своем Raspbery Pi, она запускается без проблем, но если я нажимаю кнопку, программа вылетает.Я вижу, что оператор print выводится на консоль до сбоя приложения.Например, Player = rock, Computer = paper.Это должно произойти после того, как светодиод включится, но светодиод так и не загорится.

Кто-нибудь может предложить возможные варианты расследования?

Обновление

ОК,кнопки должны были быть подключены к контактам 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 голосов
/ 15 мая 2019

Возникла проблема с моим макетом.Когда я создал схему на новой, проблем не было.

...