单击时符号未显示在 Pygame window 上(井字)
Sign not displaying on Pygame window when clicking (tictactoe)
我正在编写一个 tictactoe 游戏,我有一些类似于 this link 中的代码。
单击板时,它不会写任何东西(X 或 O)。多次尝试后,我似乎无法弄清楚问题出在哪里。如何解决?
import pygame
import sys
# ? 2019 TheFlyingKeyboard and released under MIT License
# theflyingkeyboard.net
def map_mouse_to_board(x, y):
if x < gameSize / 3:
column = 0
elif gameSize / 3 <= x < (gameSize / 3) * 2:
column = 1
else:
column = 2
if y < gameSize / 3:
row = 0
elif gameSize / 3 <= y < (gameSize / 3) * 2:
row = 1
else:
row = 2
return column, row
def draw_board(board):
myFont = pygame.font.SysFont('Tahoma', gameSize // 3)
for y in range(3):
for x in range(3):
if board[y][x] == xMark:
color = xColor
else:
color = oColor
text_surface = myFont.render(board[y][x], False, color)
screen.blit(text_surface, (y * (gameSize // 3) + margin + (gameSize // 18), x * (gameSize // 3) + margin))
def is_full(board):
return not any(None in sublist for sublist in board)
def get_winner(board):
# Diagonals
if ((board[0][0] == board[1][1] and board[1][1] == board[2][2]) \
or (board[0][2] == board[1][1] and board[1][1] == board[2][0])) and board[1][1] is not None:
return board[1][1]
for i in range(3):
if board[i][0] == board[i][1] and board[i][1] == board[i][2] and board[i][0] is not None: # Rows
return board[i][0]
if board[0][i] == board[1][i] and board[1][i] == board[2][i] and board[0][i] is not None: # Columns
return board[0][i]
return None
def draw_lines():
# Vertical lines
pygame.draw.line(screen, lineColor, (margin + gameSize // 3, margin),
(margin + gameSize // 3, screenSize - margin), lineSize)
pygame.draw.line(screen, lineColor, (margin + (gameSize // 3) * 2, margin),
(margin + (gameSize // 3) * 2, screenSize - margin), lineSize)
# Horizontal lines
pygame.draw.line(screen, lineColor, (margin, margin + gameSize // 3), (screenSize - margin, margin + gameSize // 3),
lineSize)
pygame.draw.line(screen, lineColor, (margin, margin + (gameSize // 3) * 2),
(screenSize - margin, margin + (gameSize // 3) * 2), lineSize)
screenSize = 1920
margin = 50
gameSize = 1920 - (2 * margin)
lineSize = 10
backgroundColor = (0, 0, 0)
lineColor = (255, 255, 255)
xColor = (200, 0, 0)
oColor = (0, 0, 200)
xMark = 'X'
oMark = 'o'
board = [[None, None, None], [None, None, None], [None, None, None]]
currentMove = 'X'
pygame.init()
screen = pygame.display.set_mode((screenSize, screenSize))
pygame.display.set_caption("Tic Tac Toe")
pygame.font.init()
myFont = pygame.font.SysFont('Tahoma', gameSize // 3)
screen.fill(backgroundColor)
canPlay = True
draw_lines()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
board = [[None, None, None], [None, None, None], [None, None, None]]
screen.fill(backgroundColor)
draw_lines()
canPlay = True
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.type is pygame.MOUSEBUTTONDOWN and canPlay:
(mouseX, mouseY) = pygame.mouse.get_pos()
(column, row) = map_mouse_to_board(mouseX, mouseY)
if board[column][row] is None:
board[column][row] = currentMove
if currentMove == xMark:
currentMove = oMark
else:
currentMove = xMark
draw_board(board)
winner = get_winner(board)
if winner is not None:
myFont = pygame.font.SysFont('Tahoma', screenSize // 5)
text_surface = myFont.render(str(winner) + " has won!", False, lineColor)
screen.blit(text_surface, (margin, screenSize // 2 - screenSize // 10))
canPlay = False
else:
if is_full(board):
myFont = pygame.font.SysFont('Tahoma', screenSize // 5)
text_surface = myFont.render("Draw!", False, lineColor)
screen.blit(text_surface, (screenSize // 10, screenSize // 2 - screenSize // 10))
pygame.display.update()
我参考一下Difference between ==
and is
operator in Python:
The Equality operator (==
) compares the values of both the operands and checks for value equality. Whereas the is
operator checks whether both the operands refer to the same object or not.
因此如果你想测试 event.type
是否等于 pygame.MOUSEBUTTONDOWN
,你必须使用 ==
而不是 is
:
if event.type is pygame.MOUSEBUTTONDOWN and canPlay:
if event.type == pygame.MOUSEBUTTONDOWN and canPlay:
另见 How is the 'is' keyword implemented in Python?
尽管在 if event.type is pygame.MOUSEBUTTONDOWN and canPlay:
中使用 is
而不是 ==
,但我对您的代码的唯一问题是 pygame window 是我的屏幕太大了。
我怀疑当您单击某个单元格时 Os and/or X 出现在屏幕之外。我通过将它们除以 3
来缩小常数值,它对我来说很好用:
发件人:
screenSize = 1920
margin = 50
gameSize = 1920 - (2 * margin)
lineSize = 10
至:
screenSize = 1920 // 3
margin = 50 // 3
gameSize = (1920 - (2 * margin)) // 3
lineSize = 10 // 3
输出:
我正在编写一个 tictactoe 游戏,我有一些类似于 this link 中的代码。
单击板时,它不会写任何东西(X 或 O)。多次尝试后,我似乎无法弄清楚问题出在哪里。如何解决?
import pygame
import sys
# ? 2019 TheFlyingKeyboard and released under MIT License
# theflyingkeyboard.net
def map_mouse_to_board(x, y):
if x < gameSize / 3:
column = 0
elif gameSize / 3 <= x < (gameSize / 3) * 2:
column = 1
else:
column = 2
if y < gameSize / 3:
row = 0
elif gameSize / 3 <= y < (gameSize / 3) * 2:
row = 1
else:
row = 2
return column, row
def draw_board(board):
myFont = pygame.font.SysFont('Tahoma', gameSize // 3)
for y in range(3):
for x in range(3):
if board[y][x] == xMark:
color = xColor
else:
color = oColor
text_surface = myFont.render(board[y][x], False, color)
screen.blit(text_surface, (y * (gameSize // 3) + margin + (gameSize // 18), x * (gameSize // 3) + margin))
def is_full(board):
return not any(None in sublist for sublist in board)
def get_winner(board):
# Diagonals
if ((board[0][0] == board[1][1] and board[1][1] == board[2][2]) \
or (board[0][2] == board[1][1] and board[1][1] == board[2][0])) and board[1][1] is not None:
return board[1][1]
for i in range(3):
if board[i][0] == board[i][1] and board[i][1] == board[i][2] and board[i][0] is not None: # Rows
return board[i][0]
if board[0][i] == board[1][i] and board[1][i] == board[2][i] and board[0][i] is not None: # Columns
return board[0][i]
return None
def draw_lines():
# Vertical lines
pygame.draw.line(screen, lineColor, (margin + gameSize // 3, margin),
(margin + gameSize // 3, screenSize - margin), lineSize)
pygame.draw.line(screen, lineColor, (margin + (gameSize // 3) * 2, margin),
(margin + (gameSize // 3) * 2, screenSize - margin), lineSize)
# Horizontal lines
pygame.draw.line(screen, lineColor, (margin, margin + gameSize // 3), (screenSize - margin, margin + gameSize // 3),
lineSize)
pygame.draw.line(screen, lineColor, (margin, margin + (gameSize // 3) * 2),
(screenSize - margin, margin + (gameSize // 3) * 2), lineSize)
screenSize = 1920
margin = 50
gameSize = 1920 - (2 * margin)
lineSize = 10
backgroundColor = (0, 0, 0)
lineColor = (255, 255, 255)
xColor = (200, 0, 0)
oColor = (0, 0, 200)
xMark = 'X'
oMark = 'o'
board = [[None, None, None], [None, None, None], [None, None, None]]
currentMove = 'X'
pygame.init()
screen = pygame.display.set_mode((screenSize, screenSize))
pygame.display.set_caption("Tic Tac Toe")
pygame.font.init()
myFont = pygame.font.SysFont('Tahoma', gameSize // 3)
screen.fill(backgroundColor)
canPlay = True
draw_lines()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_r:
board = [[None, None, None], [None, None, None], [None, None, None]]
screen.fill(backgroundColor)
draw_lines()
canPlay = True
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.type is pygame.MOUSEBUTTONDOWN and canPlay:
(mouseX, mouseY) = pygame.mouse.get_pos()
(column, row) = map_mouse_to_board(mouseX, mouseY)
if board[column][row] is None:
board[column][row] = currentMove
if currentMove == xMark:
currentMove = oMark
else:
currentMove = xMark
draw_board(board)
winner = get_winner(board)
if winner is not None:
myFont = pygame.font.SysFont('Tahoma', screenSize // 5)
text_surface = myFont.render(str(winner) + " has won!", False, lineColor)
screen.blit(text_surface, (margin, screenSize // 2 - screenSize // 10))
canPlay = False
else:
if is_full(board):
myFont = pygame.font.SysFont('Tahoma', screenSize // 5)
text_surface = myFont.render("Draw!", False, lineColor)
screen.blit(text_surface, (screenSize // 10, screenSize // 2 - screenSize // 10))
pygame.display.update()
我参考一下Difference between ==
and is
operator in Python:
The Equality operator (
==
) compares the values of both the operands and checks for value equality. Whereas theis
operator checks whether both the operands refer to the same object or not.
因此如果你想测试 event.type
是否等于 pygame.MOUSEBUTTONDOWN
,你必须使用 ==
而不是 is
:
if event.type is pygame.MOUSEBUTTONDOWN and canPlay:
if event.type == pygame.MOUSEBUTTONDOWN and canPlay:
另见 How is the 'is' keyword implemented in Python?
尽管在 if event.type is pygame.MOUSEBUTTONDOWN and canPlay:
中使用 is
而不是 ==
,但我对您的代码的唯一问题是 pygame window 是我的屏幕太大了。
我怀疑当您单击某个单元格时 Os and/or X 出现在屏幕之外。我通过将它们除以 3
来缩小常数值,它对我来说很好用:
发件人:
screenSize = 1920
margin = 50
gameSize = 1920 - (2 * margin)
lineSize = 10
至:
screenSize = 1920 // 3
margin = 50 // 3
gameSize = (1920 - (2 * margin)) // 3
lineSize = 10 // 3
输出: