【问题标题】:Finding which of 4 scores reach a certain amount first找出 4 个分数中哪一个先达到一定数量
【发布时间】:2014-04-02 17:58:28
【问题描述】:

我试图找出谁在我的游戏中获胜,然后,除了打印所有玩家的得分外,还说谁赢了。但是,我做 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 或 txtpad3score 达到 20,我希望它说“玩家 3 获胜!”。任何帮助都会很棒。

【问题讨论】:

  • 你只有一个赢家吗?你试过什么了? if 怎么样?
  • 只有一位获胜者。两个玩家的分数不可能同时上升。到目前为止,还没有尝试过任何接近成功的方法。
  • 啊修好了,你的“如果”帮助很大。

标签: python tkinter game-engine


【解决方案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)         

【讨论】:

    【解决方案2】:
        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!')
    

    【讨论】:

      猜你喜欢
      • 2016-12-20
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 2021-12-06
      • 2015-06-29
      • 1970-01-01
      相关资源
      最近更新 更多