Mathieu Sauvé
Mathieu Sauvé

Reputation: 3

multiprocess not stopping to ask for input, returns an error

I created a function that will play a midi note using pygame.midi, I've also created a game called smack jack. I have both the game and the song playing at the same time using multiprocess but I run into an error where the program will not stop to ask for input and will continue the song creating this error

line 126, in loop_c

action = input('') EOFError: EOF when reading a line

please help, thanks.

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()

Upvotes: 0

Views: 27

Answers (1)

user14203455
user14203455

Reputation:

It's not a good idea to read user input from a sub process, so just run "loop_c" function in the main process, like this:

(This is from the end of your file)

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

Upvotes: 0

Related Questions