#python #text #save #load
#python #текст #Сохранить #загрузить
Вопрос:
Я пытаюсь создать функцию сохранения загрузки для моей текстовой RPG, она сохранит и загрузит одну загрузку при открытии и сохранит при закрытии. Эта textRPG — мой первый реальный проект. прямо сейчас я просто пытаюсь получить файл для дампа в формате json.
код, который я использую, заключается в следующем
def saveload():
player_Stats = {
player.hp,
player.gold,
player.lvl,
player.current_location()
}
filename = 'save.json'
with open(filename, 'w') as f:
json.dump(player_Stats)
это весь сценарий проигрывателя. Я планирую использовать json.загрузить позже, чтобы загрузить данные в соответствующие переменные прямо сейчас в функции сохранения iv есть только пара статистики игроков для сохранения в целях тестирования. любая помощь приветствуется.
import items
import world
import enemies
import magic
class Player:
def __init__(self):
self.inventory = [items.Dagger(),items.Rock(),items.CrustyBread()]
self.spell_book = [magic.magic_missle(), magic.fire_ball()]
self.x = world.start_tile_location[0]
self.y = world.start_tile_location[1]
self.hp = 100
self.gold = 5
self.victory = False
self.exp = 199
self.player_lvl = 1
self.mana = 100
def is_alive(self):
return self.hp > 0
def move(self,dx,dy):
self.x = dx
self.y = dy
def move_north(self):
self.move(dx=0, dy=-1)
def move_south(self):
self.move(dx=0, dy=1)
def move_east(self):
self.move(dx=1, dy=0)
def move_west(self):
self.move(dx=-1, dy=0)
def player_stats(self):
print("Level: {}".format(self.player_lvl), "HP: {}".format(self.hp),"Mana:{}".format(self.mana),"Gold: {}".format(self.gold), "EXP: {}".format(self.exp))
def print_spellbook(self):
print('n Spell Book')
for magic in self.spell_book:
print('*' str(magic))
def print_inventory(self):
print("n Inventory:")
for item in self.inventory:
print('* ' str(item))
print("Gold: {}".format(self.gold))
'''
def check_lvl_up(self,enemy):
new_exp = self.exp enemy.exp
levels = [0,200,450,1012]
if True:
current_level = sum(1 for x in levels if x<= total_exp)
self.player_lvl = current_level
'''
def most_powerful_weapon(self):
max_damage = 0
best_weapon = None
for item in self.inventory:
try:
if item.damage > max_damage:
best_weapon = item
max_damage = item.damage
if self.player_lvl >= 2:
item.damage = item.damage 1
except AttributeError:
pass
return best_weapon
def attack(self):
spell = [magic_spell for magic_spell in self.spell_book if isinstance(magic_spell,magic.Spell)]
if not spell:
print("You have no spells to cast!")
user_input = input('What do you want to attack with? Melee or Magic: ')
if user_input == str('magic'):
for i, magic_spell in enumerate(spell, 1):
print("Choose a spell to cast: ")
print("{}. {}".format(i,magic_spell))
valid =False
while not valid:
choice = input("")
try:
if self.mana == 0:
print("You dont have enough mana")
else:
room = world.tile_at(self.x,self.y)
enemy = room.enemy
print("You use {} against {}!".format(magic_spell,enemy.name))
enemy.hp -= magic_spell.damage
self.mana = self.mana - magic_spell.mana
if not enemy.is_alive():
print("You killed {}!".format(enemy.name))
else:
print("{} HP is {}.".format(enemy.name,enemy.hp))
except(ValueError,IndexError):
print("Invalid choice, try again")
break
elif user_input == str('melee'):
best_weapon = self.most_powerful_weapon()
room = world.tile_at(self.x,self.y)
enemy = room.enemy
print("You use {} against {}!".format(best_weapon.name,enemy.name))
enemy.hp -= best_weapon.damage
if not enemy.is_alive():
print("You killed {}!".format(enemy.name))
else:
print("{} HP is {}.".format(enemy.name,enemy.hp))
def heal(self):
consumables = [item for item in self.inventory if isinstance(item,items.Consumables)]
if not consumables:
print("You dont have any items to heal you!")
return
for i, item in enumerate(consumables, 1):
print("Choose an item to use to heal: ")
print("{}. {}".format(i, item))
valid = False
while not valid:
choice = input("")
try:
to_eat = consumables[int(choice)-1]
self.hp = min(100,self.hp to_eat.healing_value)
self.inventory.remove(to_eat)
print("Current HP: {}".format(self.hp))
valid = True
except (ValueError,IndexError):
print("Invalid choice, try again")
def trade(self):
room = world.tile_at(self.x,self.y)
room.check_if_trade(self)
def current_locaton(self):
room = world.tile_at(self.x,self.y)
self.current_location = room
def saveload(self):
player_Stats = {
self.player.hp,
self.player.gold,
self.player.lvl,
self.player.current_location(),
}
filename = 'save.json'
with open(filename, 'w') as f:
json.dump(player_Stats)
Ответ №1:
Редактировать 19:42:15 22-09-2020
Вы не указываете на файл / поток, в котором хотите его сохранить.
with open(filename, 'w') as f:
json.dump(player_Stats, f)
или
with open(filename, 'w') as f:
f.write(json.dumps(player_Stats))
Дайте нам знать, если это поможет.
Комментарии:
1. внизу у меня есть функция с именем LoadSave () прямо сейчас я просто пытался заставить ее сохранить несколько параметров, просто чтобы посмотреть, создает ли она json.