【问题标题】:Pygame score issue [closed]Pygame分数问题[关闭]
【发布时间】:2016-01-25 00:57:46
【问题描述】:
while not done:
    baddieAddCounter = 0
    baddies = []
    score += 1
    fact = ""

    #GIVES PLAYER A NEW FACT EVERYTIME THEY REACH THE TARGET SCORE
    if score < 200:
       fact == 'NOTHING'

    elif score > 200 and score <= 599:
       fact == 'SWAG'

    elif score > 600 and score <= 799:
       fact == 'brehh'


    drawText('Score: %s' % (score), font, screen, 10, 0)
    drawText('Fact: %s' % (fact), font, screen, 10, 40)
    pygame.display.update()

为什么这不起作用?在游戏中,一旦玩家得分达到目标得分(例如 >200),它应该将事实(例如 SWAG')更改为“if”语句中所说的内容,但由于“事实 =”而保持不变"'。

【问题讨论】:

  • 应该是fact = 'NOTHING'而不是fact == 'NOTHING'

标签: python python-2.7 pygame


【解决方案1】:

你没有分配给fact,你是在比较它。我假设你想要的是

if score < 200:
       fact = 'NOTHING'

elif score > 200 and score <= 599:
   fact = 'SWAG'

elif score > 600 and score <= 799:
   fact = 'brehh'

另外请注意,得分为 200 时,玩家将得到一个空白事实,因为第一个条件仅小于 200,而下一个条件仅大于 200。

【讨论】:

    【解决方案2】:

    当分数为 200 时,您的代码未设置 fact。您必须检查 score &gt;= 200 或使用

    if score < 200:
       fact == 'NOTHING'
    
    elif score <= 599:
       fact == 'SWAG'
    
    elif score <= 799:
       fact == 'brehh'
    

    顺便说一句:你可以在while之前设置face=''

    fact = ""
    
    baddieAddCounter = 0
    baddies = []
    
    while not done:
        score += 1
    
       if score < 200:
          fact == 'NOTHING'
    
       elif score <= 599:
          fact == 'SWAG'
    
       elif score <= 799:
          fact == 'brehh'
    
       drawText('Score: %s' % (score), font, screen, 10, 0)
       drawText('Fact: %s' % (fact), font, screen, 10, 40)
       pygame.display.update()
    

    编辑:

    @Margius 是对的。你使用==,但你需要=

    fact = ""
    
    baddieAddCounter = 0
    baddies = []
    
    while not done:
        score += 1
    
       if score < 200:
          fact = 'NOTHING'
    
       elif score <= 599:
          fact = 'SWAG'
    
       elif score <= 799:
          fact = 'brehh'
    
       drawText('Score: %s' % (score), font, screen, 10, 0)
       drawText('Fact: %s' % (fact), font, screen, 10, 40)
       pygame.display.update()
    

    【讨论】:

    • 我不得不说:直到@Margius 显示这个我才注意到'==' :)
    猜你喜欢
    • 1970-01-01
    • 2016-08-19
    • 2012-11-02
    • 2022-01-07
    • 2012-04-03
    • 2022-01-21
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多