So I have this code to the main loop for the game , everything seems to go as intended except turn change, i am not sure what is the problem i have been stuck in this for 2 days and can't figure it out here is the link to the full script in case anyone want to check if i did something wrong in the functions itself https://github.com/AmrBinBashir/Tic-Tac-Toe-Pygame:
run = True
while run:
draw_board_onetime(board_boxes, win)
make_board(board_boxes)
board = [" "] * 10
game_on = True
while game_on:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEMOTION:
pos = pygame.mouse.get_pos()
for box in board_boxes:
if box.is_hover(pos):
box.hovered = True
box.draw(win)
else:
box.hovered = False
box.draw(win)
turn = "player1"
if turn == "player1":
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
for i in range(1, 10):
if board_boxes[i].is_hover(pos):
board_boxes[i].text = x_marker
board[i] = x_marker
print(board)
if check_win(board, x_marker):
game_on = False # WIN
else:
if full_board_check(board):
game_on = False # TIE
else:
turn = "player2"
else:
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
for i in range(1, 10):
if board_boxes[i].is_hover(pos):
board_boxes[i].text = o_marker
board[i] = o_marker
print(board)
if check_win(board, o_marker):
game_on = False # WIN
else:
if full_board_check(board):
game_on = False # TIE
else:
turn = "player1"
pygame.display.update()
sys.exit()
sys.exit()
I am not entirely sure about your code (haven't executed it myself) but I'm pretty sure overriding
Move setting "player1" somewhere else in your code, e.g. a global variable for testing. Right now it seems that even if you set it to "player2" it will be overwritten anyway in the next run.
Little extra bit, kind advice: Strings don't make good comparators. Unless you enjoy the readability, think about using booleans or at least integers for that matter. You will learn later on how they are much more versatile and less prone to errors (such as simple typos or accidental capitalisation).
Booleans for example can easily be flipped like
player = not player
(switching back and forth betweenTrue
for P1 andFalse
for P2).Integers can easily be incremented like
player_id += 1
(which starts becoming handy for a board game with 3 players or more).