Python 3 函数 - 列出其他函数的值

标签 python python-3.x

我正在开发一个非常简单的基于文本的冒险游戏。我已经能够完成玩家可以从一个房间移动到另一个房间的基础知识。为了增强游戏,我想要一个简单的战斗系统,但我在实现一个能够保持玩家健康分数的系统时遇到了困难。我提供了目前代码的示例并添加了注释。

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/

相关文章:

python - SQLAlchemy:声明式 ORM - 使用 GIST 和 TSRANGE 构建索引

python - 调整大小不会影响 PyQt5 中的布局

python - PyQt5 : Destroyed while thread is still running

python - 如何声明与数据类类型相同的python数据类成员字段

python - 从waveid加载wave

javascript - 无需单击表格即可键入您的信息

python - 基于 ajax 的数据加载后如何获取 Chrome webdriver 的最新内容

django - Django 中 REST API 的目录结构应该是什么?

python - 为什么 Python 3 中实例的 __dict__ 大小如此之小?

python - 如何在请求 HTTPS 调用中抑制有关缺少证书验证的警告?