I'm creating a checkers game using PyGame and I'm having problems with the screen updating after each player makes their move. Essentially, I'm using a matrix called "board" which is the underlying data structure for all positions on the screen. Every time someone makes a move, the board matrix is updated and shown on the screen using draw_board(). The screen should be updating after each iteration of the while loop, but for some reason it only changes after both players have made their moves (two iterations of the while loop). I've been trying to solve this for hours and can't figure it out. Here's my code. Sorry if it's unclear or there's way too much, I wasn't sure how else to capture the entire problem.
board = matrix of 0 (empty space), 1 (red piece), 2 (blue piece)
def draw_board(board):
'''
Updates the position of the pieces on the board after every move, by copying the board array
'''
draw_background() # we must make the board blank before drawing the pieces again, or else the old positions will remain drawn
screen = pygame.display.get_surface()
radius = 20
i = 0
for row in board:
j = 0
for space in row:
if space == 1:
pygame.draw.circle(screen, red, getPixels(j,i), radius)
elif space == 2:
pygame.draw.circle(screen, blue, getPixels(j,i), radius)
j+=1
i+=1
while True:
# human's turn
if game.turn == 'red':
awaiting_red = True
while awaiting_red:
for event in pygame.event.get():
if event.type == pygame.QUIT: # if window close button clicked then leave game loop
break
if event.type == pygame.MOUSEBUTTONDOWN and second_click == False: # click piece to move
# some code removed here to select a piece
second_click = True
elif event.type == pygame.MOUSEBUTTONDOWN and second_click == True: # click new position for piece
# some code removed here to select a new position
my_color = board[piece_selected.sprite.y_pos][piece_selected.sprite.x_pos]
board[piece_selected.sprite.y_pos][piece_selected.sprite.x_pos] = 0
board[space_selected.sprite.y_pos][space_selected.sprite.x_pos] = my_color
second_click = False
awaiting_red = False
# computer's turn
elif game.turn == 'blue':
move = game.get_move(board)
# updating the board matrix
for i in range(8): # x position
for j in range(8): # y position
new_space = move[0][j][i]
board[j][i] = new_space
# for some reason the screen doesn't update until after both players have made moves?? can't figure out how to fix this
draw_board(board)
pygame.display.update()
game.change_turn()
pygame.quit()
Any help is appreciated. Thank you!