我需要制作这个游戏的麻烦。玩家,地精,坑和金子随机放在一个阵列中。玩家需要获得黄金而不会陷入坑中或被妖精吃掉。即时通讯仍处于初期阶段,但我甚至无法让玩家进入随机房间。这是我的代码
Object
我想找到播放器在阵列中的位置,并通过说"你在室内打印,"但我不知道该怎么做才能显示
答案 0 :(得分:0)
我假设你正在寻找玩家所在的位置。试试这个:
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
我还想让你知道,你不需要import random
多次,只有一次在顶部!你也有一些冗余,为了简单起见我会清理。