英文:
My Simple combat system isn't working properly
问题
以下是您的代码的翻译部分:
我尝试创建一个简单的战斗系统,但它不按照我的意愿运行。通常当我接近零时,我会卡在数字4。我希望它的作用是,如果 `d_damage` 等于0,则打印出胜利。我主要困扰于对手的伤害。我还没有处理用户的健康。但如果有人可以帮助我,我将不胜感激。
这是我的代码:
```python
import random
import os
health = ['100']
d_health = ['100']
damage = random.randint(0, 7)
def clear():
os.system("cls")
def health_damage():
sum = int(d_health[0]) - damage
if sum > 0:
d_health.remove(d_health[0])
d_health.append(str(sum))
begin_game()
if int(d_health[0]) <= 0:
clear()
print("你打败了龙!")
def begin_game():
clear()
print("龙的生命值:" + str(d_health[0]))
print()
print("你的生命值:" + str(health[0]))
x = input("你想做什么?")
if x.lower() == "1":
health_damage()
begin_game()
def before_game():
print("欢迎来到《新的开始:生存之战》")
x = input("你确定要战斗吗?")
if x.lower() == "是":
clear()
begin_game()
before_game()
希望这对您有所帮助。如果您有任何其他问题,请随时提出。
英文:
I tried to make a simple combat system but it isn't working how I want it to. Eventually when I get near zero im stuck at number 4 usually. And what I want it to do is that if d_damage equals 0 to print out that the person won. Im mostly struggling on the damage for the opponent. I havent worked on the users health. But if someone could help I would dearly appreciate it.
Here is my code:
import random
import os
health = [ '100' ]
d_health = [ '100' ]
damage = random.randint(0, 7)
def clear():
os.system("cls")
def health_damage():
sum = int(d_health[0]) - damage
if sum > 0:
d_health.remove(d_health[0])
d_health.append(str(sum))
begin_game()
if int(d_health[0]) <= 0:
clear()
print( "You defeated the dragon!" )
def begin_game():
clear()
print( "Dragon Health: " + str(d_health[0]) + "")
print()
print( "Your Health: " + str(health[0]) )
x = input( "What do you wish to do?" )
if x.lower() == "1":
health_damage()
begin_game()
def before_game():
print( "Welcome to The Beginning: Fight to Survive" )
x = input( "Are you sure you want to fight?" )
if x.lower() == "yes":
clear()
begin_game()
before_game()
答案1
得分: 1
只删除sum部分并直接删除if语句。它对任何事情都没有帮助。
应该改成这样:
d_health = 100
damage = random.int(1, 7)
def health_damage():
d_health -= damage
# 使用-=是d_health = d_health - damage的更好看的版本
if int(d_health) <= 0:
clear()
print("你打败了龙!")
while True:
if input(""):
quit()
else:
clear()
begin_game()
英文:
you only lower the health if health is above 0, so it's going to repeat forever for no reason
def health_damage():
sum = int(d_health[0]) - damage
if sum > 0:
d_health.remove(d_health[0])
d_health.append(str(sum))
begin_game()
if int(d_health[0]) <= 0:
clear()
print( "You defeated the dragon!" )
just remove the sum part and directly remove the if statment. it doesnt help with anything.
you should instead do this:
d_health = 100
damage = random.int(1, 7)
def health_damage():
d_health -= damage
# the -= is a better looking version of d_health = d_health - damage
if int(d_health) <= 0:
clear()
print( "You defeated the dragon!" )
while True:
if input(""):
quit()
else:
clear()
begin_game()
(btw a bit unrelated, but please learn object oriented programming. it sounds hard at first, but it makes everything way easier to work with)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论