Python 如何在我的冒险游戏中分配玩家

Python 如何在我的冒险游戏中分配玩家,python,adventure,Python,Adventure,我需要做的游戏有问题。玩家、地精、矿坑和黄金随机放置在一个阵列中。玩家需要在不掉进坑里或被地精吃掉的情况下获得金牌。我仍处于起步阶段,但我甚至不能让球员进入随机房间。这是我的密码 array = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] print (array) rows=4 cols=4 objects = [[0 for x in range(cols)]for y in range(rows)] print (objects)

我需要做的游戏有问题。玩家、地精、矿坑和黄金随机放置在一个阵列中。玩家需要在不掉进坑里或被地精吃掉的情况下获得金牌。我仍处于起步阶段,但我甚至不能让球员进入随机房间。这是我的密码

array = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
print (array)
rows=4
cols=4
objects = [[0 for x in range(cols)]for y in range(rows)]
print (objects)



import random
player = "player"
row=random.randrange(0,3)
col=random.randrange(0,3)
if (objects[row][col]) == 0:
objects[row][col]= player




import random
goblin = "goblin"
flag = True
while (flag):
row=random.randrange(0,3)
col=random.randrange(0,3)
if (objects[row][col]) == 0:
    objects[row][col]= goblin
    flag = False

pit = "pit"
flag = True
while (flag):
row=random.randrange(0,3)
col=random.randrange(0,3)
if (objects[row][col]) == 0:
    objects[row][col]= pit
    flag = False

pit = "pit"
flag = True
while (flag):
row=random.randrange(0,3)
col=random.randrange(0,3)
if (objects[row][col]) == 0:
    objects[row][col]= pit
    flag = False


gold = "gold"
flag = True
while (flag):
row=random.randrange(0,3)
col=random.randrange(0,3)
if (objects[row][col]) == 0:
    objects[row][col]= gold
    flag = False
    print (objects)

objects[0][0]=1
objects[0][1]=2
objects[0][2]=3
objects[0][3]=4
objects[1][0]=5
objects[1][1]=6
objects[1][2]=7
objects[1][3]=8
objects[2][0]=9
objects[2][1]=10
objects[2][2]=11
objects[2][3]=12
objects[3][0]=13
objects[3][1]=14
objects[3][2]=15
objects[3][3]=16

print ("Welcome to the Adventure Game")
print ("You are in Room " , objects[player])

我想找到玩家在阵列中的位置,并通过说“你在房间里”打印出来,但我不知道该用什么来显示,我想你是在寻找玩家的位置。试试这个:

import random

array = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
print (array)
rows=4
cols=4
objects = [[0 for x in range(cols)]for y in range(rows)]
print (objects)


player = "player"
row=random.randrange(0,3)
col=random.randrange(0,3)
if (objects[row][col]) == 0:
    objects[row][col]= player



goblin = "goblin"
flag = True
while (flag):
    row=random.randrange(0,3)
    col=random.randrange(0,3)
    if (objects[row][col]) == 0:
        objects[row][col]= goblin
        flag = False

pit = "pit"
flag = True
while (flag):
    row=random.randrange(0,3)
    col=random.randrange(0,3)
    if (objects[row][col]) == 0:
        objects[row][col]= pit
        flag = False

pit = "pit"
flag = True
while (flag):
    row=random.randrange(0,3)
    col=random.randrange(0,3)
    if (objects[row][col]) == 0:
        objects[row][col]= pit
        flag = False


gold = "gold"
flag = True
while (flag):
    row=random.randrange(0,3)
    col=random.randrange(0,3)
    if (objects[row][col]) == 0:
        objects[row][col]= gold
        flag = False
        print (objects)


print ("Welcome to the Adventure Game")
#iterate through the 2D array and look for player
for room in objects:
    for thing in room:
        if thing == "player":
            #this will print out the room where the player is
            print thing + " is in room: "
            print room

我还想让您知道,您不必多次导入随机,只需在顶部导入一次!你也有一些冗余,为了简单起见我会清理一下。

我不是。。。完全确定你在问什么。好吧,我把玩家分配到一个随机房间,但我想打印一些东西,说明玩家在哪个房间。。。打印(“你在房间里”,我需要一些东西,但我不知道该放什么。我需要找到玩家在阵列中的位置。你应该使用编辑按钮,而不是使用注释,在问题中添加其他信息。这样可以更清晰地看到。房间是内部阵列吗?