我刚刚通过本教程完成了用Python编写一个Roguelike程序的工作,现在我非常独立地找出该去哪里和下一步该做什么。
我的困境是代码的混乱。我想把元素存储在某个地方,例如物品;生物;技能;任何可能有大量元素的地方,它们本身就有许多属性。
目前,所有这些代码都位于一个正在变得相当大的文件中,这是最基本的。当前,用于将项放在级别上的函数看起来非常类似:(在生成级别时调用此函数)。
def place_objects(room):
#Maximum number of items per room
max_items = from_dungeon_level([[1, 1], [2, 4]])
#Chance of each item (by default they have a chance of 0 at level 1, which then goes up)
item_chances = {}
item_chances['heal'] = 35
item_chances['lightning'] = from_dungeon_level([[25, 4]])
item_chances['fireball'] = from_dungeon_level([[25, 6]])
item_chances['confuse'] = from_dungeon_level([[10, 2]])
item_chances['sword'] = from_dungeon_level([[5, 4]])
item_chances['shield'] = from_dungeon_level([[15, 8]])
#Choose a random number of items
num_items = libtcod.random_get_int(0, 0, max_items)
for i in range(num_items):
#Choose random spot for this item
x = libtcod.random_get_int(0, room.x1+1, room.x2-1)
y = libtcod.random_get_int(0, room.y1+1, room.y2-1)
#Only place it if the tile is not blocked
if not is_blocked(x, y):
choice = random_choice(item_chances)
if choice == 'heal':
#Create a healing potion
item_component = Item(use_function=cast_heal)
item = Object(x, y, '~', 'Salve', libtcod.light_azure, item=item_component)
elif choice == 'lightning':
#Create a lightning bolt scroll
item_component = Item(use_function=cast_lightning)
item = Object(x, y, '#', 'Scroll of Lightning bolt', libtcod.light_yellow, item=item_component)
elif choice == 'fireball':
#Create a fireball scroll
item_component = Item(use_function=cast_fireball)
item = Object(x, y, '#', 'Scroll of Fireball', libtcod.light_yellow, item=item_component)
elif choice == 'confuse':
#Create a confuse scroll
item_component = Item(use_function=cast_confuse)
item = Object(x, y, '#', 'Scroll of Confusion', libtcod.light_yellow, item=item_component)
elif choice == 'sword':
#Create a sword
equipment_component = Equipment(slot='right hand', power_bonus=3)
item = Object(x, y, '/', 'Sword', libtcod.sky, equipment=equipment_component)
elif choice == 'shield':
#Create a shield
equipment_component = Equipment(slot='left hand', defense_bonus=1)
item = Object(x, y, '[', 'Shield', libtcod.sky, equipment=equipment_component)
objects.append(item)
item.send_to_back()由于我打算添加更多的项,这个函数将变得非常长,所有随后需要创建的函数都将被创建,以处理每个项所做的事情。我真的很想把它从main.py中拿出来存储到其他地方,以便更容易地使用,但我现在不知道如何做到这一点。
下面是我试着思考这个问题的尝试:
我可以有一个文件,其中包含一个item类,每个项都包含一些属性;(名称、类型、条件、魔法、图标、权重、颜色、描述、equip_slot、materal)。然后为项目在该文件中所做的工作存储函数?主文件如何知道何时调用另一个文件?
所有项目数据是否可以存储在一个外部文件中(比如XML或其他什么),并在需要时从其中读取?
这是我可以适用的东西,显然不仅仅是项目。当我真正想要的是一个主循环和一个更好的组织结构时,这对于没有一个非常臃肿的main.py来容纳游戏中所有的生物、物品和其他物体是非常有用的,它会让成千上万的代码行膨胀。
发布于 2013-11-24 09:40:18
如果您不需要人类可读的文件,请考虑使用Python的泡菜库;这可以序列化对象和函数,这些对象和函数可以被写入文件并从文件中读取。
在组织方面,您可以有一个objects文件夹,将这些类从您的主游戏循环中分离出来,例如:
game/
main.py
objects/
items.py
equipment.py
creatures.py为进一步改进,请使用继承来做以下工作:
class Equipment(Object):
class Sword(Equipment):
class Item(Object):
class Scroll(Item):然后,任何剑的所有设置都可以在初始化中定义(例如,哪只手在其中),并且只需要传递特定剑的变化(例如名称、技能奖励)。
发布于 2013-11-24 10:30:55
所有项目数据是否可以存储在一个外部文件中(比如XML或其他什么),并在需要时从其中读取?
您可以使用ConfigParser模块来完成这一任务。
可以将项的属性存储在单独的文件(普通文本文件)中。读取此文件可自动创建对象。
我将在这里使用文档中的示例作为指南:
import ConfigParser
config = ConfigParser.RawConfigParser()
config.add_section('Sword')
config.set('Sword', 'strength', '15')
config.set('Sword', 'weight', '5')
config.set('Sword', 'damage', '10')
# Writing our configuration file to 'example.cfg'
with open('example.cfg', 'wb') as configfile:
config.write(configfile)现在,要读取该文件:
import ConfigParser
from gameobjects import SwordClass
config = ConfigParser.RawConfigParser()
config.read('example.cfg')
config_map = {'Sword': SwordClass}
game_items = []
for i in config.sections():
if i in config_map:
# We have a class to generate
temp = config_map[i]()
for option,value in config.items(i):
# get all the options for this sword
# and set them
setattr(temp, option, value)
game_items.append(temp)https://stackoverflow.com/questions/20169712
复制相似问题