If I understand the problem, you want the circle to appear in the cell between the words WHITE and BLACK:If I'm right, in principle your code is right, I just think you'll have to subtract 100 to centery in:pygame.draw.circle(DISPLAYSURF, tileColor, (centerx, centery -100), int(SPACESIZE / 2) - 4)
The problem is that the coordinates 1, 6 the board does not correspond to that box (in fact that cell does not exist on the board, so it is drawn out of it). That box has the coordinates. 5, 2 Really. In Pygame the pixel 0, 0 is located in the upper left corner of the screen. Better to see it with a reproducible example:import pygame
FPS = 10 # frames per second to update the screen
WINDOWWIDTH = 640 # width of the program's window, in pixels
WINDOWHEIGHT = 480 # height in pixels
SPACESIZE = 50 # width & height of each space on the board, in pixels
BOARDWIDTH = 11 # how many columns of spaces on the game board
BOARDHEIGHT = 3 # how many rows of spaces on the game board
XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH * SPACESIZE)) / 2)
YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * SPACESIZE)) / 2)
BLUE = ( 70, 130, 180)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
class Point:
def init(self, x, y):
self.x = x
self.y = y
class Size:
def init(self, width, height):
self.height = height
self.width = width
class Sailor(Point):
def init(self, x, y, player):
self.x = x
self.y = y
self.player = player
def translate_board_to_pixel_coord(x, y):
return (XMARGIN + x * SPACESIZE + int(SPACESIZE / 2),
YMARGIN + y * SPACESIZE + int(SPACESIZE / 2))
def main():
pygame.init()
screen_center = Point(WINDOWWIDTH // 2, WINDOWHEIGHT // 2)
board_size = Size(BOARDWIDTH * SPACESIZE, BOARDHEIGHT * SPACESIZE)
board_lt_vert = Point(screen_center.x - board_size.width // 2,
screen_center.y - board_size.height // 2)
board_rd_vert = Point(board_lt_vert.x + board_size.width,
board_lt_vert.y + board_size.height)
points = [(x, y) for x in range(board_lt_vert.x, board_rd_vert.x, SPACESIZE)
for y in range(board_lt_vert.y, board_rd_vert.y, SPACESIZE)]
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
clock = pygame.time.Clock()
pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 30)
done = False
while not done:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(BLUE)
# Dibujamos la retícula
for point in points:
pygame.draw.rect(screen, BLACK, (*point, SPACESIZE, SPACESIZE ), 2)
# Creamos una lista con las instancias de "sailors"
sailors = [Sailor(x, y, "white") for x in range(BOARDWIDTH)
for y in range(BOARDHEIGHT)]
for sailor in sailors:
x = sailor.x
y = sailor.y
centerx, centery = translate_board_to_pixel_coord(x, y)
if sailor.player == 'white' or sailor.player == 'black':
if sailor.player == 'white':
tileColor = WHITE
else:
tileColor = BLACK
pygame.draw.circle(screen, tileColor, (centerx, centery),
int(SPACESIZE / 2) - 4)
textsurface = myfont.render(f'{x}, {y}', False, (250, 0, 0))
screen.blit(textsurface,(centerx - textsurface.get_width() // 2,
centery - textsurface.get_height() // 2))
pygame.display.flip()
pygame.quit()
if name == "main":
main()
In the picture we can see the coordinates of the board Sailor You must have to draw in that box.If necessary, the function could be modified to accept another coordinate system and convert them to the one PyGame uses. According to your example (1, 6):1 - first row from below.6 - sixth column from the left (counting from 1)for this you can modify translate_board_to_pixel_coord(x, y) as follows:def translate_board_to_pixel_coord(x, y):
return (XMARGIN + (y - 1) * SPACESIZE + int(SPACESIZE / 2),
YMARGIN + (BOARDHEIGHT - x) * SPACESIZE + int(SPACESIZE / 2))
The complete example:import pygame
FPS = 10 # frames per second to update the screen
WINDOWWIDTH = 640 # width of the program's window, in pixels
WINDOWHEIGHT = 480 # height in pixels
SPACESIZE = 50 # width & height of each space on the board, in pixels
BOARDWIDTH = 11 # how many columns of spaces on the game board
BOARDHEIGHT = 3 # how many rows of spaces on the game board
XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH * SPACESIZE)) / 2)
YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * SPACESIZE)) / 2)
BLUE = ( 70, 130, 180)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
class Point:
def init(self, x, y):
self.x = x
self.y = y
class Size:
def init(self, width, height):
self.height = height
self.width = width
class Sailor(Point):
def init(self, x, y, player):
self.x = x
self.y = y
self.player = player
def translate_board_to_pixel_coord(x, y):
return (XMARGIN + (y - 1) * SPACESIZE + int(SPACESIZE / 2),
YMARGIN + (BOARDHEIGHT - x) * SPACESIZE + int(SPACESIZE / 2))
def main():
pygame.init()
screen_center = Point(WINDOWWIDTH // 2, WINDOWHEIGHT // 2)
board_size = Size(BOARDWIDTH * SPACESIZE, BOARDHEIGHT * SPACESIZE)
board_lt_vert = Point(screen_center.x - board_size.width // 2,
screen_center.y - board_size.height // 2)
board_rd_vert = Point(board_lt_vert.x + board_size.width,
board_lt_vert.y + board_size.height)
points = [(x, y) for x in range(board_lt_vert.x, board_rd_vert.x, SPACESIZE)
for y in range(board_lt_vert.y, board_rd_vert.y, SPACESIZE)]
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
clock = pygame.time.Clock()
pygame.font.init()
myfont = pygame.font.SysFont('Comic Sans MS', 30)
done = False
while not done:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(BLUE)
# Dibujamos la retícula
for point in points:
pygame.draw.rect(screen, BLACK, (*point, SPACESIZE, SPACESIZE ), 2)
sailors = [Sailor(x, y, "white") for x in range(1, BOARDHEIGHT + 1)
for y in range(1, BOARDWIDTH + 1)]
for sailor in sailors:
x = sailor.x
y = sailor.y
centerx, centery = translate_board_to_pixel_coord(x, y)
if sailor.player == 'white' or sailor.player == 'black':
if sailor.player == 'white':
tileColor = WHITE
else:
tileColor = BLACK
pygame.draw.circle(screen, tileColor, (centerx, centery),
int(SPACESIZE / 2) - 4)
textsurface = myfont.render(f'{x}, {y}', False, (250, 0, 0))
screen.blit(textsurface,(centerx - textsurface.get_width() // 2,
centery - textsurface.get_height() // 2))
pygame.display.flip()
pygame.quit()
if name == "main":
main()