我试图找到谁在我的游戏中获胜,然后,以及打印所有玩家的分数,说谁赢了。但是,我没有长时间使用Python而且我不知道该怎么做。以下是代码的相关部分:
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)
我想拥有它,以便当其中一个分数达到19或以上时,游戏结束并打印每个玩家的分数。这是有效的,但我也想要它,所以它说'玩家1(或者任何一个超过19的玩家)获胜!'但我不知道如何让节目首先显示哪一个得分达到19或更高。因此,如果玩家3或txtpad3分数达到20,我希望它说“玩家3获胜!”。任何帮助都会很棒。
答案 0 :(得分:1)
如果只有一名玩家的得分为19或更高,您只需在常规if
声明中测试得分即可。
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)
答案 1 :(得分:0)
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!')