每当添加这两行特定代码时,Python 都会跳过第一个 if 条件

如何解决每当添加这两行特定代码时,Python 都会跳过第一个 if 条件

查看第 54-59 行时,有两行代码表示:

hero_health -= ENEMY_damAGE_AMOUNT
enemy_health -= HERO_damAGE_AMOUNT[damage_choice]

每当我将这些代码添加到 if 语句时,程序就会完全跳过此 if 语句并始终输出到 else 语句。没有任何语法错误,但是每当我删除这些代码行时,如果条件很好,程序就会同时运行。此外,当添加它们时,代码将跳到 else 语句,并且出于某种原因只运行两个打印过程之一。我不确定导致这种情况的原因,并希望对我可能做错的事情提供一些反馈。谢谢!

import random
HERO_damAGE_AMOUNT = [15,16,17,18,19,20]
ENEMY_damAGE_AMOUNT = 15

hero_health = 100
enemy_health = 100
enemy_choice = 0
hero_choice = 0

#------------------------------------------------------------------------------------------------------------------------------------

moon_background = Image("https://i.imgur.com/HWFNLRm.jpg")
moon_background.set_size(472.25,480)
moon_background.set_position(0,0)
add(moon_background)

hero_image = Image("https://images-wixmp-ed30a86b8c4ca887773594c2.wixmp.com/f/a8baa77f-c488-4769-bb75-a8a228144b41/debw2vq-26be5f8e-e91e-4009-b8f8-a56114f7efbe.png?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1cm46YXBwOiIsImlzcyI6InVybjphcHA6Iiwib2JqIjpbW3sicGF0aCI6IlwvZlwvYThiYWE3N2YtYzQ4OC00NzY5LWJiNzUtYThhMjI4MTQ0YjQxXC9kZWJ3MnZxLTI2YmU1ZjhlLWU5MWUtNDAwOS1iOGY4LWE1NjExNGY3ZWZiZS5wbmcifV1dLCJhdWQiOlsidXJuOnNlcnZpY2U6ZmlsZS5kb3dubG9hZCJdfQ.2s7I8FsHnWciBzXtHubWMRVKaV4l4nyQumfc2v3PAs4")
hero_image.set_size(150,150)
hero_image.set_position(get_width()/32,get_height()/3)
add(hero_image)

enemy_image = Image("https://i5.walmartimages.com/asr/ee3dd5b3-301e-4728-8457-d1e9905a6318_1.e358b4a32891c23b850a910b35d444fd.png")
enemy_image.set_size(200,200)
enemy_image.set_position(get_width()/2,get_height()/3.65)
add(enemy_image)

block_image = Image("https://r7y4k4n7.stackpathcdn.com/3134-large_default/captain-america-shield-marvel-silver-coin-1-fiji-2019.jpg")
block_image.set_size(75,75)
block_image.set_position(get_width()/12,get_height()/1.27)
add(block_image)

attack_image = Image("https://img.icons8.com/emoji/452/crossed-swards.png")
attack_image.set_size(75,75)
attack_image.set_position(get_width()/1.3,get_height()/1.258)
add(attack_image)

#------------------------------------------------------------------------------------------------------------------------------------

def attack_and_block(x,y):
    if x <= 307.6923076923077 + 75 and x >= 307.6923076923077 and y <= 381.55802861685214 + 75 and y >= 381.55802861685214:
        enemy_choice = random.randint(1,2)
        hero_choice = 1
        if hero_choice == enemy_choice:
            damage_choice = random.randint(0,5)
            hero_health -= ENEMY_damAGE_AMOUNT
            enemy_health -= int(HERO_damAGE_AMOUNT[damage_choice])
            print("You both attack. You do " + str(HERO_damAGE_AMOUNT[damage_choice]) + " damage. The enemy does " + str(ENEMY_damAGE_AMOUNT) + " damage.")
            print("Your health is Now: " + str(hero_health) + ". The enemy's health is Now: " + str(enemy_health) + ".")
        else:
            print("You attacked but the enemy blocked it.")
            print("Your health is still: " + str(hero_health) + ". The enemy's health is still: " + str(enemy_health) + ".")
    elif x <= 33.333333333333335 + 75 and x >= 33.333333333333335 and y <= 369.2307692307692 + 75 and y >= 369.2307692307692:
        enemy_choice = random.randint(1,2)
        hero_choice = 2
        if hero_choice == enemy_choice:
            print("You both blocked,nothing happened.")
            print("Your health is still: " + str(hero_health) + ". The enemy's health is still: " + str(enemy_health) + ".")
        else:
            print("The enemy attacked,but you blocked it.")
            print("Your health is still: " + str(hero_health) + ". The enemy's health is still: " + str(enemy_health) + ".")

add_mouse_click_handler(attack_and_block)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?