井字棋转 pygame
Tic Tac Toe Turn change with pygame
所以我将这段代码添加到游戏的主循环中,一切似乎都按预期进行,除了轮次改变,我不确定我已经被困在这个问题中 2 天了,无法弄清楚是什么问题这是完整脚本的 link 以防有人想检查我是否在函数本身做错了什么 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()
我不完全确定你的代码(我自己没有执行过)但我很确定我会覆盖
turn = "player1"
#will always slip into:
if turn == "player1":
将设置 "player1" 移动到代码中的其他位置,例如用于测试的全局变量。现在看来,即使你将它设置为"player2",它也会在下一个运行时被覆盖。
一点额外的建议,善意的建议:字符串不是很好的比较器。除非您喜欢可读性,否则请考虑使用布尔值或至少使用整数。稍后您将了解它们如何更通用且更不容易出错(例如简单的拼写错误或意外大写)。
例如,布尔值可以很容易地翻转
player = not player
(在 P1 的 True
和 P2 的 False
之间来回切换)。
整数可以像 player_id += 1
一样轻松递增(这对于 3 人或更多玩家的棋盘游戏开始变得方便)。
解决方案,如果有人在功能中看到这个,只需将 turn 变量移到 game_on 循环之外,这样它就不会在每个循环中被覆盖
run = True
while run:
draw_board_onetime(board_boxes, win)
make_board(board_boxes)
board = [" "] * 10
game_on = True
turn = 'player1'
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)
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()
所以我将这段代码添加到游戏的主循环中,一切似乎都按预期进行,除了轮次改变,我不确定我已经被困在这个问题中 2 天了,无法弄清楚是什么问题这是完整脚本的 link 以防有人想检查我是否在函数本身做错了什么 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()
我不完全确定你的代码(我自己没有执行过)但我很确定我会覆盖
turn = "player1"
#will always slip into:
if turn == "player1":
将设置 "player1" 移动到代码中的其他位置,例如用于测试的全局变量。现在看来,即使你将它设置为"player2",它也会在下一个运行时被覆盖。
一点额外的建议,善意的建议:字符串不是很好的比较器。除非您喜欢可读性,否则请考虑使用布尔值或至少使用整数。稍后您将了解它们如何更通用且更不容易出错(例如简单的拼写错误或意外大写)。
例如,布尔值可以很容易地翻转
player = not player
(在 P1 的 True
和 P2 的 False
之间来回切换)。
整数可以像 player_id += 1
一样轻松递增(这对于 3 人或更多玩家的棋盘游戏开始变得方便)。
解决方案,如果有人在功能中看到这个,只需将 turn 变量移到 game_on 循环之外,这样它就不会在每个循环中被覆盖
run = True
while run:
draw_board_onetime(board_boxes, win)
make_board(board_boxes)
board = [" "] * 10
game_on = True
turn = 'player1'
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)
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()