SeesSound
SeesSound

Reputation: 505

python distance travelled lists

I am attempting to create a program where I can track the distance travelled by a character in a 1D running game. My code that displays the running world is below:

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 

Now the running character is represented by the "@" sign in track[2][1]. A user will input a number and depending on the number the runner will move to the right that many places and the track will be displayed again and the user will be re-asked again, until the runner makes it to the end.

My issue lies in creating a function that moves the runner forward into an empty space " ", while at the same time turning the old space the runner was in into an empty space " " and the new space into the runner "@". This is kind of the format of what I have tried:

def displayDistance(distanceTravelled,track):
    location= track[2].index("@")
    currentDistance= track[2][location]
    nextDistance= track[2][location+distanceTravelled]
    currentDistance= " "
    nextDistance="@"

I am fairly new with lists therefore I am having issues with this problem. Also on a last note, if the charachter "@" moves on to a "|" (border space) then he should automatically move to the next available blank space " ". As well, if the runner reaches the end, he shouldn't move any further regardless of if more input in entered. If anything isn't clear please let me know and I will fix it fast. Thanks for any and all help.

Upvotes: 0

Views: 552

Answers (2)

Niels
Niels

Reputation: 482

To set the old location of the runner to a white space you should give in track[2] a new value. Giving currentDistance a new value makes no sense. Same for nextdistance. So:

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

Now in Track[2] there are both given new values. You should however take precautions, when the distanceTravelled + its old location is longer than the actual length of track[2]. Then it wil give errors. Good luck

Upvotes: 1

Blckknght
Blckknght

Reputation: 104712

This issue you're having is with these lines:

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

They don't do what you want, because you're just assigning values to local variables, not to the contents of the track list. If you skip the local variables, you should have better results:

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

Upvotes: 1

Related Questions