我的简单战斗系统没有正常工作。

huangapple go评论81阅读模式
英文:

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 = [ &#39;100&#39; ]
d_health = [ &#39;100&#39; ]

damage = random.randint(0, 7)

def clear():
    os.system(&quot;cls&quot;)

def health_damage():
    sum = int(d_health[0]) - damage
    if sum &gt; 0:
        d_health.remove(d_health[0])
        d_health.append(str(sum))
        begin_game()
        
    if int(d_health[0]) &lt;= 0:
        clear()
        print( &quot;You defeated the dragon!&quot; )

def begin_game():
    clear()
    print( &quot;Dragon Health: &quot; + str(d_health[0]) + &quot;&quot;)
    print()
    print( &quot;Your Health: &quot; + str(health[0]) )
    x = input( &quot;What do you wish to do?&quot; )
    if x.lower() == &quot;1&quot;:
        health_damage()
        begin_game()

def before_game():
    print( &quot;Welcome to The Beginning: Fight to Survive&quot; )
    x = input( &quot;Are you sure you want to fight?&quot; )
    if x.lower() == &quot;yes&quot;:
        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 &gt; 0:
    d_health.remove(d_health[0])
    d_health.append(str(sum))
    begin_game()
    
if int(d_health[0]) &lt;= 0:
    clear()
    print( &quot;You defeated the dragon!&quot; )

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) &lt;= 0:
    clear()
    print( &quot;You defeated the dragon!&quot; )
    while True:
        if input(&quot;&quot;):
            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)

huangapple
  • 本文由 发表于 2023年2月19日 01:22:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定