[Python] Why can't I see the image equally spaced to the right with python turtle?

Discussão em 'Python' iniciado por Stack, Abril 7, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to create the Breakout game with python's turtle module, but for something I haven't yet identified I can't center the image correctly!

    from turtle import Turtle

    class Game():

    def __init__(self):
    self.t = Turtle()
    self.t.screen.title('Object-oriented turtle demo')
    self.t.screen.bgcolor("black")
    self.t.screen.setup(width=1000, height=900, startx=None, starty=None)
    self.t.hideturtle()
    self.t.penup()
    self.t.goto(0, 0)
    self.t.dot(10, "white") # ponto branco no centro da tela
    self.blocks = []
    self.t.screen.tracer(0)
    print(self.t.screen.window_width())


    def draw_block(self, x, y, width, height, colour):
    self.t.penup()
    self.t.goto(x, y)
    self.t.setheading(0)
    self.t.pendown()
    self.t.fillcolor(colour)
    self.t.begin_fill()

    for _ in range(2):
    self.t.forward(width)
    self.t.right(90)
    self.t.forward(height)
    self.t.right(90)

    self.t.end_fill()

    def draw_board(self):
    colours = ["red", "orangered", "dark orange", "orange", "yellow",
    "yellow green", "green", "turquoise", "blue", "dark blue"]

    block_width = 80
    block_height = 30
    block_spacing_x = 10 # Espaçamento entre blocos
    block_spacing_y = 40
    num_columns = 11
    num_rows = 10

    window_width = self.t.screen.window_width()
    total_width_blocks = num_columns * block_width
    total_spacing = (num_columns - 1) * block_spacing_x
    total_grid_width = total_width_blocks + total_spacing

    # Calcula o ponto inicial para centralizar a grade
    start_x = -total_grid_width / 2

    window_height = self.t.screen.window_height()
    start_y = (window_height / 2) - block_height - 20 # leve margem do topo

    for row in range(num_rows):
    for column in range(num_columns):

    x = start_x + column * (block_width + block_spacing_x)
    y = start_y - row * block_spacing_y
    self.draw_block(x, y, width=block_width, height=block_height, colour=colours[row])
    self.blocks.append({
    "x": x,
    "y": y,
    "width": block_width,
    "height": block_height,
    "color": colours[row],
    "active": True
    })
    print("Hello")
    self.draw_vertical_line()
    self.draw_horizontal_line()# Chama a função para desenhar a linha
    self.t.screen.update()
    #self.t.screen.mainloop()

    def draw_vertical_line(self):
    self.t.pencolor("white")
    self.t.penup()
    altura_janela = self.t.screen.window_height()

    # Desenha a linha da direita
    self.t.goto(self.t.screen.window_width() / 2, altura_janela / 2) # Topo da direita
    self.t.setheading(270) # Aponta para baixo
    self.t.pendown()
    self.t.forward(altura_janela)

    # Desenha a linha da esquerda (com um pequeno ajuste de +3 como tinha)
    self.t.penup()
    self.t.goto(-self.t.screen.window_width() / 2, altura_janela / 2) # Topo da esquerda
    self.t.setheading(270) # Aponta para baixo
    self.t.pendown()
    self.t.forward(altura_janela)

    def draw_horizontal_line(self):
    self.t.pencolor("white")
    self.t.penup()
    largura_janela = self.t.screen.window_width()
    margem = 20 # Margem dos lados
    comprimento_linha = largura_janela - 2 * margem
    centro_linha = 0 # O centro horizontal da janela é 0
    start_x = centro_linha - comprimento_linha / 2
    self.t.goto(start_x, 0)
    self.t.setheading(0)
    self.t.pendown()
    self.t.forward(comprimento_linha)


    # Executar
    turtle = Game()
    turtle.draw_board()
    #turtle.draw_vertical_line()
    print(turtle.blocks[-1])
    print(turtle.blocks[-1]["x"] + turtle.blocks[-1]["width"])
    print(turtle.t.screen.window_width() / 2)
    last_block = turtle.blocks[-1]
    right_edge = last_block["x"] + last_block["width"]
    half_screen = turtle.t.screen.window_width() / 2

    print("\n--- Validação ---")
    print(f"Último bloco termina em: {right_edge}")
    print(f"Metade da tela: {half_screen}")
    print(f"Diferença: {abs(right_edge - half_screen)}")
    turtle.t.screen.mainloop()


    enter image description here

    I've tried drawing vertical lines in -window_width/ 2 and in window_width/2, but the latter doesn't appear. I can only see it by dragging the window sideways with the mouse! Why is that?

    Continue reading...

Compartilhe esta Página