【问题标题】:Creating a text-based map for a text based RPG为基于文本的 RPG 创建基于文本的地图
【发布时间】:2016-05-09 09:30:51
【问题描述】:

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

我希望世界是无边界的,这样玩家就可以假设永远玩这个游戏。如果我要创建几个定义城镇的类,我如何将随机城镇对象拉到世界环境中的某个图块上供玩家互动?

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

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

感谢您的帮助

【问题讨论】:

  • 我建议你把这个问题分成几个小问题。我曾经在 JS 中做过一个非常古老的任务游戏(引擎)。您可以看到地图是如何构建的以及对象是如何放置在其中的。这不是您的最佳解决方案,但可能会提供一些想法。它在我的 github 帐户中。
  • 而且...我想说这不是真正与 Python 相关的问题,而更像是 OOP 和软件设计问题。
  • gamedev.stackexchange.com 是这个问题的更好的地方
  • 感谢大家的建议。非常感谢他们。
  • 你知道Rogue/Nethack吗?

标签: python python-2.7 design-patterns


【解决方案1】:

这个问题确实太宽泛了,所以我把它缩小了一点。为了专注于我解决的问题,我将首先陈述问题:'我如何制作一张地图——主要是从一个列表中——包含可以与玩家交互的某些属性,即敌方 AI 和战利品图块,并保存关于像城镇和地牢这样的地方,为了基于文本的 RPG 的目的?'

我这样解决了这个问题:

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' 将字符值从 index[0][0] 向前“移动”到列表的 index[1][0] - 上一行 - - 它返回值 3。这是通过将 Room 类的 userpos 变量映射到地图列表中的特定索引并通过 Player 函数移动跟踪任何更改来完成的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 2013-07-07
    • 1970-01-01
    • 2014-10-08
    • 2017-04-09
    • 1970-01-01
    • 2011-04-02
    相关资源
    最近更新 更多