簡體   English   中英

如何將變量的值存儲在 class 中?

[英]How can I store the values of the variables in the class?

class Player():
    money = 0
    level = 0
    xp = 0
    xp_until_level = 20
    taxi_car = "Cabbie"
    busines = "No"
    busines_income = 1000
    upgrade_cost = 10000
    car_speed = 10
    car_level = 0
    drives_until_new_car = 20


p = Player()

我需要在進入游戲時保存和加載這些變量的值我嘗試使用picklе庫但沒有任何反應

如果您希望 Values 與實例 Player 相關聯,最簡單的方法如下:

class Player:
    def __init__(self):
        self.money = 0
        self.level = 0
        self.xp = 0
        self.xp_until_level = 20
        self.taxi_car = "Cabbie"
        self.busines = "No"
        self.busines_income = 1000
        self.upgrade_cost = 10000
        self.car_speed = 10
        self.car_level = 0
        self.drives_until_new_car = 20  

然后實例化播放器

p = Player() 

並通過以下方式訪問值:

p.money
p.level  

等等

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM