我正在尝试创建一个程序,我可以在其中跟踪一个角色在 1D 跑步游戏中行进的距离。我显示正在运行的世界的代码如下:
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
现在正在运行的字符由 track[2][1] 中的“@”符号表示。用户将输入一个数字,根据该数字,跑步者将向右移动许多地方,并且将再次显示赛道并再次询问用户,直到跑步者完成。
我的问题在于创建一个将跑步者向前移动到空白空间“”的功能,同时将跑步者所在的旧空间变成空白空间“”,并将新空间变成跑步者“@”。这是我尝试过的一种格式:
def displayDistance(distanceTravelled,track):
location= track[2].index("@")
currentDistance= track[2][location]
nextDistance= track[2][location+distanceTravelled]
currentDistance= " "
nextDistance="@"
我对列表相当陌生,因此我遇到了这个问题。最后一点,如果字符“@”移动到“|” (边界空间)然后他应该自动移动到下一个可用的空白空间“”。同样,如果跑步者到达终点,无论是否输入更多输入,他都不应该进一步移动。如果有任何不清楚的地方,请告诉我,我会尽快解决。感谢您的任何帮助。