我正在开发一个非常简单的基于文本的冒险游戏。我已经能够完成玩家可以从一个房间移动到另一个房间的基础知识。为了增强游戏,我想要一个简单的战斗系统,但我在实现一个能够保持玩家健康分数的系统时遇到了困难。我提供了目前代码的示例并添加了注释。
def update_score(x): #after the player has a combat round the variable 'a'is updated with remianing hit points
a = []
a.append(x)
def hit_points(): #when the player is in combat and takes a hit, 2 points are deducted and it is passed to the updated score function
y -= 2
updated_score(y)
def Continue():
#how can i then reference the updated score in another function. If the player goes into another battle, the remaining battle points will have to be used and deducted from
我才刚刚开始掌握函数,想知道是否可以将更新后的值从updated_score函数传递到其他函数,或者何时再次调用生命值函数。
我试图避免使用全局变量。
非常感谢任何帮助
最佳答案
尝试使用类
class Player:
def __init__(self):
self.hit_points = 100
def take_hit(self):
self.hit_points -= 2
p = Player()
print(p.hit_points)
>>> 100
p.take_hit()
print(p.hit_points)
>>> 98
关于Python 3 函数 - 列出其他函数的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51140546/