上课时如何拿走生命值?

我正在用 python 开发一个游戏,一旦攻击功能发生,我无法弄清楚如何消除健康。我可以运行程序并且攻击功能运行良好,它显示 1 到 50 之间的随机整数,但实际上并没有从castlehealth= 100 中夺走生命值


在下面 print("You attacked for " + str(self.attack))我将下一行留空,因为我不知道要输入什么,我尝试了很多不同的东西,但无法从castlehealth.


这是我的代码:


import os

import time

from random import randint


class GameActions:

    def __init__(self):

        castlehealth = 100

        self.castlehealth = castlehealth

    def health(self):

        print("Castle health is: " + str(self.castlehealth))

        print()

    def attack(self):

        attack = randint(0, 50)

        self.attack = attack

        print("You attacked for " + str(self.attack))


def game():

    while True:

        game_actions = GameActions()

        print("What do you want to do?")

        print()

        print("attack, view hp")

        ans = input()

        if ans == "hp":

            game_actions.health()

        if ans == "attack":

            game_actions.attack()


慕容708150
浏览 153回答 2
2回答

婷婷同学_

您需要以下内容:self.castlehealth -= attack
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python