gpt4 book ai didi

python - 打印 RPG 游戏的视觉 map

转载 作者:太空宇宙 更新时间:2023-11-03 18:01:01 24 4
gpt4 key购买 nike

大家好,大家好,

所以我对 Python 还比较陌生(现在已经学习了大约 2 周),为了加强我的学习,我一直在努力构建我自己的经典地下城/冒险游戏的版本。游戏有一名玩家、1 个怪物(将来我希望包括 5 或 6 个)、一把 key 和一扇门。玩家必须首先在地牢周围搜索,找到 key ,然后找到门。此外,玩家还可以拿起武器攻击/躲避怪物。

如果玩家跑进一个有怪物的房间,那么这会“唤醒怪物”,它会开始追逐玩家,每回合都靠近玩家。

我通过打印基于包含 100 个元组的列表(对于 10 x 10 x-y 坐标网格)的 map ,在 shell 中构建了游戏的可视化表示。如果任何游戏元素的位置等于特定的 x, y 坐标,则它会打印出一个代表性数字(例如,如果玩家位于位置 1,1,它将在位置 1, 1 中打印出“X”而不是“_”,或者如果它是怪物,它将是“!”)。

请参阅下面的当前 map 示例。这些方 block 目前很小(1 个字符 x 1 个字符),我尝试将其扩展为 3 个字符 x 3 个字符(参见示例 2),并在中心打印出游戏元素值(如果有)。

我遇到的问题是弄清楚如何构建这些更大的正方形,我很困惑。我已经用越来越复杂/疯狂嵌套的 for 循环和 if 语句闲逛了一个多小时。想暂时解决这个问题,看看过去是否有人尝试过这样做/有任何方便的提示或建议。

预先感谢您的帮助! :)

乔伊

第一个脚本

def draw_map(p_loc, m_loc, prior_moves):
left_side_indices = list(range(0, 100))
del_multiple = 9

#This for loop deletes every 9th element (ie. 9, 19, 29...99) and assigns the
#existing numbers from 0 to 98 to left_side_indices. The reason why I did this was
#because the cells that are on the right side of the grid must be printed differently
#than all of the other cells.
for i in range(10):
del left_side_indices[del_multiple]
del_multiple = del_multiple + 9

non_right_wall_grid = left_side_indices

print('\n\n\n\n' + ' _' * 10)
tile = '|{}'


#unpacking the player location to x and y
x, y = p_loc
p_loc = (x, y)
#unpacking the monster location to a and b (x and y coordinates respectively)
a, b = m_loc
m_loc = (a, b)

#loops through the grid list which is a list of tuples (x, y)
for idx, cell in enumerate(grid):
if idx in non_right_wall_grid:
if cell == player_loc:
print(tile.format('X'), end="")
elif cell == monster_loc and player.spotted_monster == True:
print(tile.format('!'), end="")
elif cell == door_loc and player.spotted_door == True:
print(tile.format('O'), end="")
elif cell in prior_moves:
print(tile.format('.'), end="")
else:
print(tile.format('_'), end="")
else:
if cell == player_loc:
print(tile.format('X|'))
elif cell == monster_loc and player.spotted_monster == True:
print(tile.format('!|'))
elif cell == door_loc and player.spotted_door == True:
print(tile.format('O'))
elif cell in prior_moves:
print(tile.format('.|'))
else:
print(tile.format('_|'))

print('\n\n\n\n')

第一个脚本 - 输出

 _ _ _ _ _ _ _ _ _ _
|_|_|_|_|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|
|_|_|.|.|.|.|_|_|_|_|
|_|_|_|.|_|.|_|_|_|_|
|_|_|_|.|.|O|X|_|_|_|
|_|_|_|.|_|_|_|_|_|_|
|_|_|_|.|_|_|_|_|_|_|
|_|_|_|.|_|_|_|_|_|_|
|_|_|.|.|_|_|_|_|_|_|
|_|_|_|_|_|_|_|_|_|_|

You are currently in room (7, 5).
You currently do not have the key.
You are currently able to go ['LEFT', 'RIGHT', 'UP', 'DOWN'], type the appropriate word to check out that room.
Type QUIT to stop playing DUNGEON or HELP for help.

Player: (7, 5)
Monster: (2, 7)
Door: (6, 5)
Key: (9, 1)
Moves After Spotting: 0

下面的第二个脚本(这是我最终努力构建的)

第二个脚本

print('\n\n ___' + '___' * 18 + '__ ')

for _ in range(10):
print('| ' * 10 + '|')
print('| {} '.format('X') * 10 + '|')
print('|_____' * 10 + '|')

print('\n\n')

第二个脚本 - 输出

 ___________________________________________________________ 
| | | | | | | | | | |
| X | X | X | X | X | X | X | X | X | X |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
| | | | | | | | | | |
| X | X | X | X | X | X | X | X | X | X |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
| | | | | | | | | | |
| X | X | X | X | X | X | X | X | X | X |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
| | | | | | | | | | |
| X | X | X | X | X | X | X | X | X | X |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
| | | | | | | | | | |
| X | X | X | X | X | X | X | X | X | X |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
| | | | | | | | | | |
| X | X | X | X | X | X | X | X | X | X |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
| | | | | | | | | | |
| X | X | X | X | X | X | X | X | X | X |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
| | | | | | | | | | |
| X | X | X | X | X | X | X | X | X | X |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
| | | | | | | | | | |
| X | X | X | X | X | X | X | X | X | X |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|
| | | | | | | | | | |
| X | X | X | X | X | X | X | X | X | X |
|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|

解决方案

def draw_map():
left_side_indices = list(range(0, 100))
del_multiple = 9

#This for loop deletes every 9th element (ie. 9, 19, 29...99) and assigns the existing numbers from 0 to 98 to left_side_indices
for i in range(10):
del left_side_indices[del_multiple]
del_multiple = del_multiple + 9

non_right_wall_grid = left_side_indices


print('\n\n ___' + '___' * 18 + '__ ')

for row in range(10):
print('| ' * 10 + '|')
for column in range(10):
if column in non_right_wall_grid:
print('| {} '.format(tile_contents(player_loc, monster_loc, prior_moves, row, column)), end='')
else:
print('| {} |'.format(tile_contents(player_loc, monster_loc, prior_moves, row, column)))
print('|_____' * 10 + '|')
print('\n\n')


def tile_contents(p_loc, m_loc, prior_moves, row, column):
x, y = p_loc
p_loc = (x, y)
a, b = m_loc
m_loc = (a, b)
coordinate = (column, row)

if coordinate == p_loc:
return('X')
elif coordinate == m_loc and player.spotted_monster == True:
return('!')
elif coordinate == door_loc and player.spotted_door == True:
return('O')
elif coordinate in prior_moves:
return('.')
else:
return('_')

最佳答案

像这样的东西怎么样:

def print_map():     
print('\n\n ___' + '___' * 18 + '__ ')
for row in range(10):
print('| ' * 10 + '|')
for column in range(10):
print('| {} '.format(tile_contents(row, column)), end='')
print('|')
print('|_____' * 10 + '|')
print('\n\n')


def tile_contents(row,column):
# Put here all conditionals based on row, column positions of player, monsters
# Return the character to be displayed in the middle of the cell

return "X"

print_map()

关于python - 打印 RPG 游戏的视觉 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27672441/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com