首页 > 解决方案 > 多进程不停止请求输入,返回错误

问题描述

我创建了一个使用 pygame.midi 播放 midi 音符的函数,我还创建了一个名为 smack jack 的游戏。我使用多进程同时播放游戏和歌曲,但我遇到了一个错误,程序不会停止要求输入,并将继续创建此错误的歌曲

第 126 行,在 loop_c

action = input('') EOFError: 读取一行时出现 EOF

请帮忙,谢谢。

import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame.midi
import time
from multiprocessing import Process

C = 0
Ch = 1
D = 2
Dh = 3
E = 4
F = 5
Fh = 6
G = 7
Gh = 8
A = 9
Ah = 10
B = 11



def playnote(i, n , x):
    if n > -1:
        i+=(12*(n)+12)
    elif n == 0:
        i+=12
    pygame.midi.init()
    player = pygame.midi.Output(0)
    player.set_instrument(0)
    player.note_on(i, 127)
    time.sleep(x)
    pygame.midi.quit()

def loop_a():
    while True:
        playnote(B, 2, 0.5)
        playnote(Ch, 3, 0.5)
        playnote(D, 3, 0.5)
        playnote(E, 3, 0.5)
        playnote(Fh, 3, 0.5)
        playnote(D, 3, 0.5)
        playnote(Fh, 3, 0.5)
        playnote(F, 3, 0.5)
        playnote(Ch, 3, 0.5)
        playnote(F, 3, 0.5)
        playnote(E, 3, 0.5)
        playnote(C, 3, 0.5)
        playnote(E, 3, 0.5)
        #end of verse
        playnote(B, 2, 0.5)
        playnote(Ch, 3, 0.5)
        playnote(D, 3, 0.5)
        playnote(E, 3, 0.5)
        playnote(Fh, 3, 0.5)
        playnote(D, 3, 0.5)
        playnote(Fh, 3, 0.5)
        playnote(B, 3, 0.5)
        playnote(A, 3, 0.5)
        playnote(Fh, 3, 0.5)
        playnote(D, 3, 0.5)
        playnote(Fh, 3, 0.5)
        playnote(A, 3, 0.5)

def loop_b():
    while True:
        playnote(B, 1, 0.5)
        playnote(Fh, 1, 0.5)


def loop_c():
    '''
    YOU NEED TO INSTALL PYGAME

    IF YOU DON'T THE SOUND WONT WORK
    '''

    import random
    import time
    import os
    os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
    import pygame.midi

    # These are notes for the keyboard
    C = 0
    Ch = 1
    D = 2
    Dh = 3
    E = 4
    F = 5
    Fh = 6
    G = 7
    Gh = 8
    A = 9
    Ah = 10
    B = 11

    # this is a function to play the note, i = the midi note number (c = 0, D = 2, etc.), n is the octave, x is the not duration.
    def playnote(i, n, x):
        if n > -1:
            i += (12 * (n) + 12)
        elif n == 0:
            i += 12
        pygame.midi.init()
        player = pygame.midi.Output(0)
        player.set_instrument(0)
        player.note_on(i, 50)
        time.sleep(x)
        pygame.midi.quit()

    dealercount = 0
    dealertotal = 0
    hitcounter = 0
    cardtotal = 0
    card = random.randrange(1, 11)
    cardtotal += card
    print('Welcome to SmackJack!')
    print(" _____     _____")
    print("|9    |   |A .  |")
    print("|^ ^ ^|   | /.\ |")  # badass cards to set the mood
    print("|^ ^ ^|   |(_._)|")
    print("|^ ^ ^|   |  |  |")
    print("|____6|   |____V|")
    print('your current card is', card, 'you can hit or stay, what would you like to do?')
    playnote(C, 4, 1)
    playnote(G, 4, 0.5)
    action = input('')
    while (action != 'hit') and (action != 'stay'):
        print('you can hit or stay')
        action = input()
    aces = 0
    while action == 'hit':  # while loop to detect if player typed 'hit'
        while (action != 'hit') and (action != 'stay'):
            print('you can hit or stay')
            action = input()
        hitcounter += 1
        card = random.randrange(1, 11)
        cardtotal += card
        if card == 11:
            aces += 1
        while cardtotal == 21:
            print(cardtotal)
            print('you have 21! YOU WIN!')
            playnote(C, 4, 1)
            playnote(G, 4, 0.5)
            exit()
        while cardtotal >= 22 and aces > 0:
            aces -= 1
            cardtotal -= 10
        while cardtotal >= 22 and aces == 0:
            print('you have', cardtotal)
            print('you lose :(')
            playnote(G, 4, 1)
            playnote(C, 4, 0.5)
            exit()

            while hitcounter == 5:
                print('your current total is')
                print(cardtotal)
                print('you must stay')
                action = input()
        print('you got', card, 'your current total is')
        print(cardtotal)
        print('you can hit or stay, what would you like to do?')
        playnote(Fh, 4, 0.5)
        action = input()

    while action == 'stay':
        dealercard = random.randrange(1, 11)
        dealertotal += dealercard
        dealercount += 1
        print('the dealer draws', dealercard, 'their total is', dealertotal)
        time.sleep(0.25)
        if dealertotal > 21:
            print('your total', cardtotal, 'dealers total', dealertotal)
            print('you win :)')
            playnote(C, 4, 1)
            playnote(G, 4, 0.5)
            exit()
        elif dealercount == 5 and dealertotal > 21:
            print('your total', cardtotal, 'dealers total', dealertotal)
            print('you lose :(')
            playnote(G, 4, 1)
            playnote(C, 4, 0.5)
            exit()
            break

    if dealertotal > cardtotal:  # LOSE
        print('your total', cardtotal, 'dealers total', dealertotal)
        print('you lose :(')
        playnote(G, 4, 1)
        playnote(C, 4, 0.5)
        exit()

    elif cardtotal > dealertotal:  # WIN
        print('your total', cardtotal, 'dealers total', dealertotal)
        print('you win :)')
        playnote(C, 4, 1)
        playnote(G, 4, 0.5)
        exit()


if __name__ == '__main__':
    Process(target=loop_a).start()
    Process(target=loop_b).start()
    Process(target=loop_c).start()

标签: pythonmultithreadinginputmultiprocessing

解决方案


从子进程读取用户输入不是一个好主意,所以只需在主进程中运行“loop_c”函数,如下所示:

(这是您文件的末尾)

if __name__ == '__main__':
    Process(target=loop_a).start()
    Process(target=loop_b).start()
    loop_c() # Running this function in the main process

推荐阅读