0

我正在尝试创建一个程序,我可以在其中跟踪一个角色在 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="@"

我对列表相当陌生,因此我遇到了这个问题。最后一点,如果字符“@”移动到“|” (边界空间)然后他应该自动移动到下一个可用的空白空间“”。同样,如果跑步者到达终点,无论是否输入更多输入,他都不应该进一步移动。如果有任何不清楚的地方,请告诉我,我会尽快解决。感谢您的任何帮助。

4

2 回答 2

1

您遇到的这个问题与这些行有关:

currentDistance= track[2][location]
nextDistance= track[2][location+distanceTravelled]
currentDistance= " "
nextDistance="@"

他们不会做你想做的事,因为你只是将值分配给局部变量,而不是track列表的内容。如果你跳过局部变量,你应该有更好的结果:

track[2][location] = " "
track[2]location+distanceTravelled] = "@"
于 2012-11-22T21:49:06.543 回答
1

要将跑步者的旧位置设置为空白,您应该在 track[2] 中提供一个新值。给 currentDistance 一个新值是没有意义的。下一个距离也一样。所以:

track[2][location]=" "
if track[2][location+distanceTravelled]=="|":
   track[2][location+distanceTravelled+1]="@"
else:
   track[2][location+distanceTravelled]="@"

现在在 Track[2] 中都有新的值。但是,当距离旅行 + 其旧位置长于轨道 [2] 的实际长度时,您应该采取预防措施。然后它会给出错误。祝你好运

于 2012-11-22T21:50:59.140 回答