我正在尝试创建一个程序,用于跟踪角色在一维游戏中的行进距离。我显示正在运行的世界的代码如下:
def display (track):
r = 0
c = 0
print("\nTRACK")
for r in range (0, (4), 1):
for c in range (0, (41), 1):
sys.stdout.write(track[r][c])
print()
print()
def initialize ():
r = 0
c = 0
track = []
#Creates each row and column. A "for" loop initiates which creates and appends an empty list to the list "track". Then, taking the current row into consideration, the respective number of columns are created via the inner "for loop and a space is appended to the end of the current row. The loop re-initiates and the process is repeated for all 4 required rows. This results in 4 rows and 41 coloumns.
for r in range (0, (4), 1):
#appends an empty list to track
track.append([])
for c in range (0, (41), 1):
#appends a space to the current row
track[r].append(" ")
# the actual rows and columns are created below.
# 0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y
track [0] = [" ","0"," ","1"," ","2"," ","3"," ","4"," ","5"," ","6"," ","7"," ","8"," ","9"," ","A"," ","B"," ","C"," ","D"," ","E"," ","F"," ","G"," ","H"," ","I"," ","J"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "]
track [1] = [" ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," "]
track [2] = ["|","@","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"," ","|"]
track [3] = [" ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," ","-"," "]
return track
现在,正在运行的角色由曲目[2] [1]中的“@”符号表示。用户将输入一个号码,并根据号码向右移动,许多地方和轨道将再次显示,用户将再次被重新询问,直到跑步者到达终点。
我的问题在于创建一个将跑步者向前移动到一个空的空间“”的功能,同时将跑步者所在的旧空间变成一个空的空间“”和新的空间进入跑步者“@ ”。这是我尝试的格式:
def displayDistance(distanceTravelled,track):
location= track[2].index("@")
currentDistance= track[2][location]
nextDistance= track[2][location+distanceTravelled]
currentDistance= " "
nextDistance="@"
我对列表相当新,因此我遇到了这个问题。还有最后一点,如果charachter“@”移动到“|” (边界空间)然后他应该自动移动到下一个可用的空白区域“”。同样,如果跑步者到达终点,无论是否输入更多输入,他都不应该进一步移动。如果有什么不清楚请告诉我,我会尽快解决。感谢您的帮助。
答案 0 :(得分:1)
您遇到的问题在于以下几行:
currentDistance= track[2][location]
nextDistance= track[2][location+distanceTravelled]
currentDistance= " "
nextDistance="@"
他们没有做你想做的事情,因为你只是将值分配给局部变量,而不是track
列表的内容。如果跳过局部变量,则应该有更好的结果:
track[2][location] = " "
track[2]location+distanceTravelled] = "@"
答案 1 :(得分:1)
要将跑步者的旧位置设置为空白区域,您应该在track [2]中为新值指定。给currentdistance一个新值是没有意义的。对于nextdistance也是如此。所以:
track[2][location]=" "
if track[2][location+distanceTravelled]=="|":
track[2][location+distanceTravelled+1]="@"
else:
track[2][location+distanceTravelled]="@"
现在在Track [2]中,给出了两个新值。但是,当distanceTravelled +其旧位置长于轨道的实际长度时,您应该采取预防措施[2]。然后它会给出错误。祝你好运