здания атрибутов выдают ошибку при загрузке
Код: Выделить всё
TypeError: Building.__init__() missing 2 required positional arguments: 'name' and 'jobs'код классов-
Код: Выделить всё
class Kingdom:
def __init__(self, total_water=100_000, water_add=667) -> None:
self.people = []
self.unemployed_people = []
self.buildings = []
self.resources = []
self.total_water = total_water
self.water_added = water_add
def add_building(self, name, x, y):
"this function will return -1 if the kingdon does not have the required resouces"
for value in building_info[name]["cost"]:
for i, item in enumerate(self.resources):
new_item = item - value
if not isinstance(new_item, int):
self.resources[i] = new_item
break
else:
return -1
width, height = assets[name].get_size()
self.buildings.append(Building(x, y, width, height, name, building_info[name]["jobs"]))
def display(self, window, x_offset=0, y_offset=0, resource_display_y_offset=0):
for building in self.buildings:
building.display(window, x_offset, y_offset)
# items
for i, item in enumerate(self.resources):
blit_text(
window,
item.name + " " + str(item.count),
(
0,
i * text_size + text_size * 0.2 * i - 5 + resource_display_y_offset,
),
size=text_size,
).get_width()
try:
window.blit(
assets[item.name],
(
resource_div_rect.x - default_item_size * 2,
i * text_size + text_size * 0.2 * i + resource_display_y_offset,
),
)
except KeyError:
window.blit(
assets["Missing Item"],
(
resource_div_rect.x - default_item_size * 2,
i * text_size + text_size * 0.2 * i + resource_display_y_offset,
),
)
def tick(self):
self.total_water += self.water_added
for person in self.people:
remaining_resources = person.job.work(self.resources)
if not isinstance(remaining_resources, int):
for i, item in enumerate(remaining_resources):
if item.name == "water" and self.total_water < item.count:
remaining_resources.pop(i)
break
if item.name == "water":
self.total_water -= item.count
break
self.resources = remaining_resources
for i, item in enumerate(self.resources):
if item.name == "person":
for j in range(item.count):
self.unemployed_people.append(Person())
self.resources.pop(i)
def employ_all_people(self):
for i, person in enumerate(self.unemployed_people):
for building in self.buildings:
if building.jobs > 0:
self.unemployed_people.pop(i)
self.people.append(Person(building))
building.jobs -= 1
break
class Person:
def __init__(self, job=None) -> None:
self.job = job
class Item:
def __init__(self, name, count, tag=None) -> None:
self.name = name
self.count = count
self.tag = tag
def __add__(self, other):
"""This function will return -1 if it cannot add the values"""
if isinstance(other, int) or isinstance(other, float):
return Item(self.name, self.count + other, self.tag)
if not isinstance(other, Item):
return -1
if self.name == other.name or (
(self.tag == other.tag or self.tag == other.name or self.name == other.tag)
and self.tag is not None
):
return Item(self.name, self.count + other.count, self.tag)
return -1
def __sub__(self, other):
"""This function will return -1 if it cannot subtract the values"""
if (
isinstance(other, int) or isinstance(other, float)
) and self.count > other.count:
return Item(self.name, self.count - other, self.tag)
if not isinstance(other, Item):
return -1
if (
self.name == other.name
or (
(
self.tag == other.tag
or self.tag == other.name
or self.name == other.tag
)
and self.tag is not None
)
) and self.count > other.count:
return Item(self.name, self.count - other.count, self.tag)
return -1
def __repr__(self) -> str:
return (
"{name: "
+ str(self.name)
+ ", count: "
+ str(self.count)
+ ", tag: "
+ str(self.tag)
+ "}"
)
class Building(pg.Rect):
def __init__(self, x, y, width, height, name, jobs):
super().__init__(x, y, width, height)
self.name = name
self.jobs = jobs
self.manual = True
def display(self, window, x_offset, y_offset):
if self.x - x_offset < resource_div_rect.x:
return
if self.bottom - y_offset > div_rect.y:
return
window.blit(assets[self.name], (self.x - x_offset, self.y - y_offset))
def work(self, resources, called_as_click=False):
"this function will return -1 if there are insufficent resoureces"
if self.manual and not called_as_click:
return -1
for item in building_info[self.name]["in"]:
for i, value in enumerate(resources):
new_item = value - item
if not isinstance(new_item, int):
resources[i] = new_item
break
else:
return -1
for item in building_info[self.name]["out"]:
for i, value in enumerate(resources):
new_item = value + item
if not isinstance(new_item, int):
resources[i] = new_item
break
else:
resources.append(item)
return resources
Обратите внимание, что в нем нет кода с ошибками
Я пытался сделать аргументы имени и задания по умолчанию, и они загрузились, но теперь аргументы были заданы по умолчанию, поэтому я не мог изменить данные здания.
Я сохраняю данные, используя file = open и data = Pickle.load, он отлично работает с другими типами данных.
А также, если есть какой-то другой способ сохранить список класса королевства со всеми другими классами, которые являются частью запомните это, пожалуйста, предложите это.
Подробнее здесь: https://stackoverflow.com/questions/787 ... ts-in-init