Skip to main content
 首页 » 编程设计

python之为基于文本的角色扮演游戏创建基于文本的 map

2025年05月04日28三少

目前,我正在创建一个基于文本的角色扮演游戏。我想知道如何创建一个有点交互式的 map ,定义特定的图 block 来保存某些世界信息,如敌人 AI 和战利品或城镇和地牢等地方,如何为这些地方创建基于文本的 map ,以及如何跟踪玩家的移动遍及世界。

我希望世界是无边界的,这样玩家就可以永远玩这个游戏。如果我要创建几个定义城镇的类,我如何才能将随机城镇对象拉到世界环境中的特定图 block 以供玩家与之交互?

我将如何构建我的玩家类、敌人和角色 AI 类以及房间类?

最后,我该如何着手创建一个基于文本的视觉 map 供玩家使用?

感谢您的帮助

请您参考如下方法:

这个问题肯定太宽泛了,所以我把范围缩小了一点。为了专注于我解决的问题,我将首先陈述问题:'我如何制作一张 map - 主要来自列表 - 包含可以与玩家互动的某些属性,即敌人 AI 和战利品瓷砖,并保存有关的信息像城镇和地牢这样的地方,是为了基于文本的角色扮演游戏吗?”

我是这样解决这个问题的:

class Player(object): 
    def __init__(self, name): 
        self.name = name 
 
    def movement(self): 
        while True: 
            print room.userpos 
            move = raw_input("[W], [A], [S], or [D]: ").lower() #Movement WASD. 
            while True: 
                if move == 'w':  #Have only coded the 'w' of the wasd for simplicity.  
                    x, y = (1, 0)   #x, y are column, row respectively.  This is done 
                    break           ##to apply changes to the player's position on the map. 
            a = room.column + x  #a and b represent the changes that take place to player's  
            b = room.row + y  ##placement index in the map, or 'tilemap' list. 
            room.userpos = tilemap[a][b] 
            if room.userpos == 3: 
                print "LOOT!"   #Representing what happens when a player comes across 
            return room.userpos ##a tile with the value 3 or 'loot'. 
            break                
 
class Room(object): 
    def __init__(self, column, row): 
        self.userpos = tilemap[column][row] 
        self.column = column    #Column/Row dictates player position in tilemap. 
        self.row = row 
 
floor = 0 
entry = 1 
exit = 2 
loot = 3                #Tile map w/ vairbale names. 
tilemap = [[0, 1, 0],   #[floor, entry, floor],   
           [3, 0, 0],   #[loot, floor, floor], 
           [0, 2, 0]]   #[floor, exit, floor] 
 
room = Room(0, 0)       #Player position for 'room' and 'Room' class -- too similar names 
user = Player('Bryce')  #for larger exercices, but I figure I could get away with it here. 
 
def main():     #Loads the rest of the program -- returns user position results. 
    inp = raw_input("Press any button to continue.: ")   
    if inp != '': 
        user.movement() 
        print room.userpos 
main()   

如果我要加载这个程序,并使用“w”将字符值从索引[0][0]向前“移动”到列表的索引[1][0]——向上一行—— - 它返回值 3。这是通过将 Room 类的 userpos 变量映射到 map 列表中的特定索引,并通过 Player 函数移动跟踪任何更改来完成的。