圆圈未绘制在正确的位置 pygame
Circle not being drawn at the correct position pygame
我的 tic tac toe 项目再次出现问题,当我单击一个正方形时,圆圈没有出现在它应该出现的位置,我尝试了多个不同的坐标但仍然无法弄清楚,所以如果你们中的任何人知道该怎么做告诉我,谢谢。
import pygame, sys
import numpy as np
pygame.init()
screen_color = (28, 170, 156)
line_color = (23, 140, 135)
line_width = 9
screen = pygame.display.set_mode((550, 450))
pygame.display.set_caption("Tic Tac Toe")
screen.fill(screen_color)
board = np.zeros((3, 3))
def draw_lines():
#1st horizontal
pygame.draw.line(screen, line_color, (0, 135), (550, 135), line_width)
#2nd horizontal
pygame.draw.line(screen, line_color, (0, 300), (550, 300), line_width)
#1st vertical
pygame.draw.line(screen, line_color, (175, 0), (175, 450), line_width)
#2nd vertical
pygame.draw.line(screen, line_color, (370, 0), (370, 450), line_width)
def draw_figures():
for row in range(3):
for col in range(3):
if board[row][col] == 1:
pygame.draw.circle(screen, 'cyan', (int(col * 550/3), int(row * 450/3)), 60, 10)
def mark_square(row, col, player):
board[row][col] = player
def available_square(row, col):
if board[row][col] == 0:
return True
else:
return False
def is_board_full():
for row in range(3):
for col in range(3):
if board[row][col] == 0:
return False
print(is_board_full())
print(board)
draw_lines()
player = 1
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mouseX = event.pos[0] #X coordinate
mouseY = event.pos[1] #Y coordinate
clicked_row = int(mouseY * 3 // 450)
clicked_col = int(mouseX * 3 // 550)
#print(clicked_col)
#print(clicked_row)
if available_square(clicked_row, clicked_col):
if player == 1:
mark_square(clicked_row, clicked_col, 1)
player = 2
elif player == 2:
mark_square(clicked_row, clicked_col, 2)
player = 1
draw_figures()
print(board)
pygame.display.update()
(如果还有其他错误也请告诉我)
如果获取坐标是反复试验或有特定方法,请告诉我,谢谢!
问题是圆以网格为中心,在正方形的中间。
您需要像这样向中心添加偏移量:
pygame.draw.circle(screen, 'cyan', (int(col * 550/3 + x_offset), int(row * 450/3 + y_offset)), 60, 10)
我还建议您创建一个名为 grid_width 的变量,这样您就不需要在程序中到处使用 550 或 450 之类的数字。
pygame.draw.circle
的第三个参数是圆心。在 (col + 0.5) 和 (row + 0.5) 处画圆:
pygame.draw.circle(screen, 'cyan', (int(col * 550/3), int(row * 450/3)), 60, 10)
pygame.draw.circle(screen, 'cyan', (int((col + 0.5) * 550/3), int((row+0.5) * 450/3)), 60, 10)
我的 tic tac toe 项目再次出现问题,当我单击一个正方形时,圆圈没有出现在它应该出现的位置,我尝试了多个不同的坐标但仍然无法弄清楚,所以如果你们中的任何人知道该怎么做告诉我,谢谢。
import pygame, sys
import numpy as np
pygame.init()
screen_color = (28, 170, 156)
line_color = (23, 140, 135)
line_width = 9
screen = pygame.display.set_mode((550, 450))
pygame.display.set_caption("Tic Tac Toe")
screen.fill(screen_color)
board = np.zeros((3, 3))
def draw_lines():
#1st horizontal
pygame.draw.line(screen, line_color, (0, 135), (550, 135), line_width)
#2nd horizontal
pygame.draw.line(screen, line_color, (0, 300), (550, 300), line_width)
#1st vertical
pygame.draw.line(screen, line_color, (175, 0), (175, 450), line_width)
#2nd vertical
pygame.draw.line(screen, line_color, (370, 0), (370, 450), line_width)
def draw_figures():
for row in range(3):
for col in range(3):
if board[row][col] == 1:
pygame.draw.circle(screen, 'cyan', (int(col * 550/3), int(row * 450/3)), 60, 10)
def mark_square(row, col, player):
board[row][col] = player
def available_square(row, col):
if board[row][col] == 0:
return True
else:
return False
def is_board_full():
for row in range(3):
for col in range(3):
if board[row][col] == 0:
return False
print(is_board_full())
print(board)
draw_lines()
player = 1
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mouseX = event.pos[0] #X coordinate
mouseY = event.pos[1] #Y coordinate
clicked_row = int(mouseY * 3 // 450)
clicked_col = int(mouseX * 3 // 550)
#print(clicked_col)
#print(clicked_row)
if available_square(clicked_row, clicked_col):
if player == 1:
mark_square(clicked_row, clicked_col, 1)
player = 2
elif player == 2:
mark_square(clicked_row, clicked_col, 2)
player = 1
draw_figures()
print(board)
pygame.display.update()
(如果还有其他错误也请告诉我) 如果获取坐标是反复试验或有特定方法,请告诉我,谢谢!
问题是圆以网格为中心,在正方形的中间。 您需要像这样向中心添加偏移量:
pygame.draw.circle(screen, 'cyan', (int(col * 550/3 + x_offset), int(row * 450/3 + y_offset)), 60, 10)
我还建议您创建一个名为 grid_width 的变量,这样您就不需要在程序中到处使用 550 或 450 之类的数字。
pygame.draw.circle
的第三个参数是圆心。在 (col + 0.5) 和 (row + 0.5) 处画圆:
pygame.draw.circle(screen, 'cyan', (int(col * 550/3), int(row * 450/3)), 60, 10)
pygame.draw.circle(screen, 'cyan', (int((col + 0.5) * 550/3), int((row+0.5) * 450/3)), 60, 10)