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="@"
私はリストにかなり慣れていないため、この問題に問題があります。また、最後の注意として、文字「@」が「|」に移動した場合 (border space) の場合、彼は自動的に次の使用可能な空白スペース " " に移動する必要があります。同様に、ランナーが最後に到達した場合、入力がさらに入力されたかどうかに関係なく、ランナーはそれ以上動くべきではありません。何か不明な点があればお知らせください。すぐに修正します。助けてくれてありがとう。