I am a python beginner and am trying to make a simple game like Conway's game of life.
I am starting with a single game piece, the "walker" (represented by a 1 on the board), and am trying to get it to bounce left and right across the game board (which is an 8x8 grid kind of... more like a 1x64 grid, but I'm working on that)
# making the game board with an array
board = [0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0]
# note that game board (array) looks like this
# 0 1 2 3 4 5 6 7
# 8 9 10 11 12 13 14 15
# 16 17 18 19 20 21 22 23
# 24 25 26 27 28 29 30 31
# 32 33 34 35 36 37 38 39
# 40 41 42 43 44 45 46 47
# 48 49 50 51 52 53 54 55
# 56 57 58 59 60 61 62 63
My plan is to store the walker's position in the variable walkerPos, which is initially set to be somewhere on the edge.
By default I want the walker to move right, but if the walker reaches an edge, I want it to detect what edge it's on and bounce toward the other direction.
sideTiles = [0,1,2,3,4,5,6,7,8,16,24,32,40,48,56,57,58,59,60,61,62,63,55,47,39,31,23,15]
cornerTiles = [0,7,56,63]
sideTileChoice = random.randint(0,27)
global walkerPos
walkerPos = sideTiles[sideTileChoice]
print('Initial walker Position: ' + str(walkerPos))
So, I have it choose a random position along the edge, and then I start the game by calling the function startGame(), which looks like this:
def startGame(turn, walkerPos, gameOver):
resetBoard() # makes all the places on the board 0
initPlace() # places the walker on a random side edge
print() # places a space in the console
displayBoard() # shows the array values line by line in a board fashion
# now that the game is setup, we can enter the game loop until we finish the game
while gameOver == 0:
turn += 1 # set the gamestate to a new turn
walkerMove(walkerPos, walkRight)
print()
displayBoard()
time.sleep(2)
print()
Like the game of life, the idea is that the game will play itself, so I have it go a turn at a time until it finishes. The code for the walker's movement is stored in the function walkerMove, which looks like this:
def walkerMove(walkerPos, walkRight):
if (walkerPos % 8 == 0):
print('walkRight=True')
print()
walkRight = True
if (walkerPos + 1) % 8 == 0:
print('walkRight=False')
print()
walkRight = False
if walkRight: # if we are on the left edge...
print(walkerPos)
board[walkerPos] = 0 # remove our last position from the board
walkerPos = walkerPos + 1 # change walkers position to the right
board[walkerPos] = walker # place walker at one position to the right of last
print('Walker moves right...')
else:
print(walkerPos)
board[walkerPos] = 0 # remove our last position from the board
walkerPos = walkerPos - 1 # change walkers position to the left
board[walkerPos] = walker # place walker at one position to the left of last
print('Walker moves left...')
I'm sure this is horrible code but my idea is to check if the walker has encountered an edge, a left one or a right one, and to change a variable that tells the walker what direction it should be moving in... Here's where my problem is. I had something like this working earlier by using walkerPos += 1, but at the time, the walkers position was being updated inside of startGame(), and I figured it would be better to have one function per game piece's moveset so I moved the code into a new function, walkerMove() and now the increments are all off.
The game starts properly:
Initial walker Position: 2
Welcome to 'It's Easier this Way'!
Game Board starts in the default state
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
Press 'SPACE' to begin.
Then the user presses space, triggering startGame()...
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
2
Walker moves right...
We see that the walker is indeed in the 2nd position, and the code correctly predicts that the walker must move right.
So here is the next iteration of the loop:
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
2
Walker moves right...
Notice that the walker DID move to the right like intended, but the walkerPos is still listed as 2, that's the number being printed right above 'Walker moves right...' But I am not sure how it ever got there, since as we can see, the walkerPos is still 2. Even weirder is the next iteration(s) of the loop (every iteration is the same from here on out, no matter the starting position of the walker)
0 0 0 1 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
2
Walker moves right...
This pattern happens every time. The game starts fine, the initial board placements are right. The game piece moves one space in whatever direction it's supposed to (even if it spawns on the right edge it will move left like intended, but only once before getting stuck), and the walkerPos variable seems like it's never changing even though the fact that the walker can move suggests otherwise.
I am sure I made a very silly mistake somewhere, but as a beginner, I am stuck trying to figure out what's going wrong. Can anyone lend a hand?