我在pygame中创建了一个游戏,其中每个房间都是文件中的一个类。例如,如果您通过房间1中的门,程序将创建房间2类的实例并运行它。这可能不是这样做的最佳方式,但我想不出更好的事情。
但是。到目前为止,我一直在存储xml文件中有关房间的信息。 xml文件的顶部将存储有关房间的信息(如配置)
<bg_image>sprites\bg.png</bg_image>
<music>sounds\bg_music_2.mp3</music>
<music_start_time>1</music_start_time>
<player_x>4</player_x>
<player_y>4</player_y>
,第二部分将存储这样的对象。
<stairs>
<type>stairs</type>
<posx>352</posx>
<posy>300</posy>
<direction>n</direction>
<messageoninteract>There are sounds from the recorder (space)</messageoninteract>
<amount>1</amount>
</stairs>
<deskchair>
<type>compchair</type>
<posx>75</posx>
<posy>57</posy>
<direction>n</direction>
<messageoninteract>This chair does fit!</messageoninteract>
<amount>1</amount>
</deskchair>
<desk1>
<type>desk</type>
<posx>75</posx>
<posy>35</posy>
<direction>s</direction>
<messageoninteract>There's this new spoon throwing trend on youtube now.</messageoninteract>
<amount>1</amount>
</desk1>
我使用xml循环遍历xml文件,以根据相关信息进行dict和spawn对象。这非常有效。但是,现在我想更改以前房间中的对象。例如,我想在4号房间进行一次动作,使2号房间的桌子消失。对于我目前设置程序的方式,这真的很复杂和愚蠢。
我正在考虑使用configparser,但我不明白我将如何制作这些对象。并且重要的是它们是可更改的,如果它们嵌入在房间文件中或者甚至作为模块,它们不是(容易)。
那我该怎么做呢?
答案 0 :(得分:2)
只需将所有数据一次加载到内存中(使用类或简单的dicts),然后更改它们。输入时,请勿从磁盘再次装入每个房间。
您可以使用键(例如简单字符串)来引用其他房间。
这是一个非常简单的例子,可以给你一个想法:
rooms = {
'Secret Room': {
'bg_image': 'kitchen.png',
'music': 'some_music.ogg',
'on_enter': 'You found the secret room!!!',
'north': {
'room': 'Kitchen'
}
},
'Kitchen': {
'bg_image': 'kitchen.png',
'music': 'some_music.ogg',
'on_enter': 'This seems to be the kitchen.',
'north': {
'room': 'Living Room'
},
'south': {
'room': 'Secret Room',
'door': {
'state': 'locked'
}
}
},
'Living Room': {
'bg_image': 'living_room.png',
'music': 'some_other_music.ogg',
'on_enter': 'A nice looking living room.',
'south': {
'room': 'Kitchen'
},
'items': {
'old living room table': {
'type': 'desk',
'use': {
'message': 'You find a remote control and push the button!\nSuddenly, you hear a lock open.',
'action': 'Kitchen-south-door-state-open'
},
'discovered': False
}
}
}
}
DIRECTIONS = ('north', 'south', 'east', 'west')
running = True
current_room = 'Kitchen'
while running:
print 'You are in the ' + current_room
new_room = current_room
room = rooms[current_room]
room_directions = filter(lambda d: d in room, DIRECTIONS)
discovered_items = filter(lambda i: room['items'][i]['discovered'], room['items']) if 'items' in room else None
options = 'look / {0}'.format(' / '.join(room_directions))
if (discovered_items):
options += ' / use'
command = raw_input(options + ' : ')
if command == 'look':
# list all items in the room and mark them as discovered
if 'items' in room:
print 'You see: '
for item in room['items']:
print item
room['items'][item]['discovered'] = True
else:
print 'You find nothing interesting here.'
elif command in room_directions:
# changing the current room, if there's no locked door
if 'door' in room[command]:
if room[command]['door']['state'] == 'locked':
print 'The door is locked!'
continue
new_room = room[command]['room']
elif command.startswith('use '):
# using items by running the their action
item = command[4:]
if item in discovered_items:
print room['items'][item]['use']['message']
keys = room['items'][item]['use']['action'].split('-')
current = rooms
while keys:
key = keys.pop(0)
if len(keys) > 1:
current = current[key]
else:
current[key] = keys.pop()
else:
print 'You cannot use that.'
if new_room != current_room:
print rooms[new_room]['on_enter']
current_room = new_room
当然,除了这个简单的演示之外,您可能希望使用比简单的dicts更好的数据结构。使用方法get_discovered_items
的房间类比filter(lambda i: room['items'][i]['discovered'], room['items']) if 'items' in room else None
这样的线条更容易维护,但您会明白这一点。