Frage

I am trying to find who wins in my game, and then, as well as printing all the player's scores, say who won. However, I've not been doing Python long and I have no idea how to. Here is the relevant parts of the code:

w = 19 #When any of the scores reach more than 19, the game ends.
x = 19
y = 19
z = 19

while gamealive ==1:
    if ball.score1 > w or ball.score2 > x or ball.score3 > y or ball.score4 > z:
        gamealive=0 # stops the game trying to redraw everything
        canvas.delete(ALL) #clears the canvas
        canvas.create_text(350, 160, font=("Bauhaus 93",30), fill = 'Red', text='Game Over') #Puts Text on Canvas OGBP
        txtpad1score = canvas.create_text(440, 350, font=("Bauhaus 93",40), fill = 'white', text='0') #Puts scores on end canvas
        txtpad2score = canvas.create_text(440, 400, font=("Bauhaus 93",40), fill = 'white', text='0') 
        txtpad3score = canvas.create_text(440, 450, font=("Bauhaus 93",40), fill = 'white', text='0') 
        txtpad4score = canvas.create_text(440, 500, font=("Bauhaus 93",40), fill = 'white', text='0')

    canvas.itemconfig(txtpad1score,text = str(ball.score1))
    canvas.itemconfig(txtpad2score,text = str(ball.score2))
    canvas.itemconfig(txtpad3score,text = str(ball.score3))
    canvas.itemconfig(txtpad4score,text = str(ball.score4))

    tk.update_idletasks()
    tk.update()
    time.sleep(0.001)

I want to have it so that when one of the scores reaches 19 or above, the game ends and prints each player's scores. This works, but I also want it so that it says 'Player 1 (or whichever player got above 19 first) wins!' but I don't know how to get the program to show which one of the scores reached 19 or more first. So if player 3, or txtpad3score, reaches 20, I want it to say 'Player 3 wins!'. Any help would be great.

War es hilfreich?

Lösung

If only one player can have a score of 19 or more, you can just test the scores in a regular if statement.

winmesssage = canvas.create_text(440, 550, font=("Bauhaus 93",40), fill = 'white', text='0')

winner = ''
if score1 >= 19:
    winner = '1'
elif score2 >= 19:
    winner = '2'
elif score3 >= 19:
    winner = '3'
elif score4 >= 19:
    winner = '4'

message = 'Player {0} wins!'.format(winner)

canvas.itemconfig(winmessage,text = message)         

Andere Tipps

    if ball.score1 > w or ball.score2 > x or ball.score3 > y or ball.score4 > z:
        gamealive=0 # stops the game trying to redraw everything
        canvas.delete(ALL) # delete all 
        canvas.create_text(350, 160, font=("Bauhaus 93",30), fill = 'Red', text='Game Over') 
        txtpad1score = canvas.create_text(440, 350, font=("Bauhaus 93",40), fill = 'white', text='0') 
        txtpad2score = canvas.create_text(440, 400, font=("Bauhaus 93",40), fill = 'white', text='0') 
        txtpad3score = canvas.create_text(440, 450, font=("Bauhaus 93",40), fill = 'white', text='0') 
        txtpad4score = canvas.create_text(440, 500, font=("Bauhaus 93",40), fill = 'white', text='0') 
        canvas.create_text(260, 350, font = ('Bauhaus 93', 40), fill = 'white', text = 'Orange Score:')
        canvas.create_text(260, 400, font = ('Bauhaus 93', 40), fill = 'white', text = 'Green Score:')
        canvas.create_text(260, 450, font = ('Bauhaus 93', 40), fill = 'white', text = 'Blue Score:')
        canvas.create_text(260, 500, font = ('Bauhaus 93', 40), fill = 'white', text = 'Purple Score:')
        if ball.score1 > w:
            canvas.create_text(350, 600, font = ('Bauhaus 93', 40), fill = 'white', text = 'Orange wins!')
        if ball.score2 > x:
            canvas.create_text(350, 600, font = ('Bauhaus 93', 40), fill = 'white', text = 'Green wins!')
        if ball.score3 > y:
            canvas.create_text(350, 600, font = ('Bauhaus 93', 40), fill = 'white', text = 'Blue wins!')
        if ball.score4 > z:
            canvas.create_text(350, 600, font = ('Bauhaus 93', 40), fill = 'white', text = 'Purple wins!')
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top