[英]How to take away health points when using a class?
I'm working on a game in python and I can't figure out how to take away health once attack function has taken place.我正在用 python 开发一个游戏,一旦攻击功能发生,我无法弄清楚如何消除健康。 I can run the program and the attack function works fine, it shows a random integer between 1 and 50 but doesn't actually take away health from castlehealth
= 100我可以运行程序并且攻击功能运行良好,它显示了 1 到 50 之间的随机整数,但实际上并没有从castlehealth
= 100 中夺走生命castlehealth
Underneath print("You attacked for " + str(self.attack))
I left the next line blank because I don't know what to type in, I tried a lot of different things and just can't get the attack to take away from castlehealth
.在print("You attacked for " + str(self.attack))
我将下一行留空,因为我不知道该输入什么,我尝试了很多不同的东西,但无法进行攻击远离castlehealth
。
Here is my code:这是我的代码:
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()
您需要以下内容:
self.castlehealth -= attack
Try something like self.castlehealth -= attack
.尝试像self.castlehealth -= attack
这样的东西。 I also fixed some potential indentation issues for you.我还为您修复了一些潜在的缩进问题。
Your full code sample could look this this:您的完整代码示例可能如下所示:
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))
self.castlehealth -= 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()
Explanation: self.castlehealth
is an instance variable of your GameActions
class.说明: self.castlehealth
是您的GameActions
类的实例变量。 The function GameActions.attack()
creates a new attack
variable as a random integer and then subtracts that value from the instance variable self.castlehealth
of the GameActions
class.函数GameActions.attack()
创建一个新的attack
变量作为一个随机整数,然后从GameActions
类的实例变量self.castlehealth
中减去该值。 Now self.castlehealth
will be the updated value.现在self.castlehealth
将是更新的值。 Consider also tracking the various attacks and resulting healths in a data structure, since every time you have a new attack
self.castlehealth
and self.attack
will change values and you will lose the ability to access the previous values.还要考虑在数据结构中跟踪各种攻击和由此产生的健康状况,因为每次有新的attack
self.castlehealth
和self.attack
都会改变值,并且您将无法访问以前的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.