我正在为这个python程序而苦苦挣扎。目标很简单,一个有地图和玩家的迷你游戏,你用zqsd键移动玩家,然后在地图中移动。问题是我在早期的任务中迷失了方向,我需要编写如下代码:-Make通过修改update_p函数并在其中添加map参数来确保玩家不会脱离地图。
不知道怎么做,任何帮助都是非常好的。
import random
import numpy as np
import sys
#Carte
m=[[0,0,0,1,1],[0,0,0,0,1],[1,1,0,0,0],[0,0,0,0,0]]
m2=m
m3=[]
d={0 : ' ', 1 : '#' }
def display_map(m,d):
for i in range(len(m)):
for j in range(len(m[i])):
if m[i][j]==0:
m[i][j]=d[0]
elif m[i][j]==1:
m[i][j]=d[1]
for h in range(len(m)):
print(*m[h], sep="")
return ""
#Personnage
depart=(0,0)
def create_perso(x,y):
p={ "char" : "o", "x" : x , "y" : y }
return p
p=create_perso(0,0)
def display_map_and_char(m,d,p):
for i in range(len(m)):
for j in range(len(m[i])):
if m[i][j]==0:
m[i][j]=d[0]
elif m[i][j]==1:
m[i][j]=d[1]
m2=m
for i in range(len(m2)):
for j in range(len(m2[i])):
if m2[i][j]=='o':
m2[i][j]=" "
list=[]
for k in p.values():
list+=[k]
if list[0]=='o':
pos=p
if list[1]<=(len(m)) and list[2]<=len(m):
m2[list[1]][list[2]]=list[0]
m3=np.concatenate((m,m2))
for h in range(len(m)):
print(*m3[h], sep="")
return ""
letter=input()
def update_p(letter,p,m):
if m[p["x"]][p["y"]]:
if letter=="d":
p["y"]=p["y"]+1
if letter=="z":
p["x"]=p["x"]-1
if letter=="q":
p["y"]=p["y"]-1
if letter=="s":
p["x"]=p["x"]+1
return ""
while True:
letter=input()
print(update_p(letter,p,m))
print(display_map_and_char(m,d,p))发布于 2020-11-23 02:06:05
我认为从你那里得到的信息还不足以做出神的回答(0和1之类的东西是什么意思)。但是,如果您只想为地图添加边界检查,则必须检查0 < x < map_width和0 < y < map_height是否如下所示:
def update_p(letter,p,m):
if m[p["x"]][p["y"]]:
if letter=="d":
if p["y"]+1 <= len(m):
p["y"]=p["y"]+1
if letter=="z":
if p["x"]-1 >= 0:
p["x"]=p["x"]-1
if letter=="q":
p["y"]=p["y"]-1 # up to you
if letter=="s":
p["x"]=p["x"]+1 # up to you
return ""这是一个非常基本的问题,也许你可以选择一个更简单的教程
发布于 2020-11-23 02:10:55
在update_p中,您将根据按下的键在当前x或y位置添加或删除1。你要事先检查新的位置是否在地图之外。
def update_p(letter, p, m):
map_size = len(m[0]) # Your map is square so we don't need anything fancier than that.
if letter == "d" and 0 <= p["y"] + 1 <= map_size:
p["y"] = p["y"] + 1
elif letter == "z" and 0 <= p["x"] - 1 <= map_size:
p["x"] = p["x"] - 1
elif letter == "q" and 0 <= p["y"] - 1 <= map_size:
p["y"] = p["y"] - 1
elif letter == "s" and 0 <= p["x"] + 1 <= map_size:
p["x"] = p["x"] + 1
return ""与你的问题无关,我不明白为什么你将玩家表示为一个字典,而不是使用一个自定义类。
def create_perso(x,y):
p={ "char" : "o", "x" : x , "y" : y }
return p
p=create_perso(0,0)应该是
class Personnage:
def __init__(self, x, y):
self.char = "o"
self.x = x
self.y = y
p = Personnage(0, 0)这意味着这是
list=[]
for k in p.values():
list+=[k]
if list[0]=='o':
pos=p
if list[1]<=(len(m)) and list[2]<=len(m):
m2[list[1]][list[2]]=list[0]必须修改为:
if p.x <= len(m) and p.y <= len(m):
m2[p.x][p.y] = p.char然后我们可以回到update_p,让它更紧凑:
def update_p(letter, p, m):
map_size = len(m[0]) # Your map is square so we don't need anything fancier than that.
if letter == "d" and 0 <= p.y + 1 <= map_size:
p.y += 1
elif letter == "z" and 0 <= p.x - 1 <= map_size:
p.x -= 1
elif letter == "q" and 0 <= p.y - 1 <= map_size:
p.y -= 1
elif letter == "s" and 0 <= p.x + 1 <= map_size:
p.x += 1
return ""最后,您还可以添加另一个检查,以查看地图上的下一个位置是否已经被墙占据,并防止角色移动到那里。
https://stackoverflow.com/questions/64957483
复制相似问题