Модуль черепахи для Python - глюк с кругами?

Я пытался создать игру "naughts and crossses", используя модуль Python Turtle. Проблема, с которой я постоянно сталкиваюсь, заключается в том, что белые круги, используемые для прикрытия чисел, обозначающих каждый квадрат сетки (чтобы вместо этого могли быть написаны значки и крестики), черепаха не рисует в одном и том же месте каждый раз при запуске программы - они находятся в том же месте, но немного подпрыгнули и больше не закрывают письмо ниже. Вот часть моего кода, которая создает круги:

def position_1(naught_cross): #each function contains the code to write its respective number in the correct place on the grid
    if naught_cross != "1": #i.e. when a 'o' or 'x' is submitted as an argument
        circle_drawer(-125,140) #calls function to draw circle in square before text is typed so text can be cleared when naughts/crosses are inputted in the grid sqaures
    t.penup()
    t.setpos(-115,130) #sets the position of the turltle head in pixels
    font = ("Arial",30,"normal")
    t.write(naught_cross,font=(font)) #having the variable 'naught_cross' enables it to change between the grid number and a naught/cross
def position_2(naught_cross):
    if naught_cross != "2":
        circle_drawer(-15,140) #calls function to draw box in square before text is typed so text can be cleared when naughts/crosses are inputted in the grid sqaures
    t.penup()
    t.setpos(-5,130) #sets the position of the turltle head in pixels
    font = ("Arial",30,"normal")
    t.write(naught_cross,font=(font))
def position_3(naught_cross):
    if naught_cross != "3":
        circle_drawer(85,140) #calls function to draw box in square before text is typed so text can be cleared when naughts/crosses are inputted in the grid sqaures
    t.penup()
    t.setpos(95,130) #sets the position of the turltle head in pixels
    font = ("Arial",30,"normal")
    t.write(naught_cross,font=(font))
def position_4(naught_cross):
    if naught_cross != "4":
        circle_drawer(-125,40) #calls function to draw box in square before text is typed so text can be cleared when naughts/crosses are inputted in the grid sqaures
    t.penup()
    t.setpos(-115,30) #sets the position of the turltle head in pixels
    font = ("Arial",30,"normal")
    t.write(naught_cross,font=(font))
def position_5(naught_cross):
    if naught_cross != "5":
        circle_drawer(-15,40)
    t.penup()
    t.setpos(-5,30) #sets the position of the turltle head in pixels
    font = ("Arial",30,"normal")
    t.write(naught_cross,font=(font))
def position_6(naught_cross):
    if naught_cross != "6":
        circle_drawer(85,40)
    t.penup()
    t.setpos(95,30) #sets the position of the turltle head in pixels
    font = ("Arial",30,"normal")
    t.write(naught_cross,font=(font))
def position_7(naught_cross):
    if naught_cross != "7": 
        circle_drawer(-125,-60) #calls function to draw box in square before text is typed so text can be cleared when naughts/crosses are inputted in the grid sqaures
    t.penup()
    t.setpos(-115,-70) #sets the position of the turltle head in pixels
    font = ("Arial",30,"normal")
    t.write(naught_cross,font=(font))
def position_8(naught_cross):
    if naught_cross != "8":
        circle_drawer(-15,-60) #calls function to draw box in square before text is typed so text can be cleared when naughts/crosses are inputted in the grid sqaures
    t.penup()
    t.setpos(-5,-70) #sets the position of the turltle head in pixels
    font = ("Arial",30,"normal")
    t.write(naught_cross,font=(font))
def position_9(naught_cross):
    if naught_cross != "9":
        circle_drawer(85,-60) #calls function to draw box in square before text is typed so text can be cleared when naughts/crosses are inputted in the grid sqaures
    t.penup()
    t.setpos(95,-70) #sets the position of the turltle head in pixels
    font = ("Arial",30,"normal")
    t.write(naught_cross,font=(font))


def circle_drawer(x,y): #paramters are x,y coordinates (contain actual coords in arguments when called) if grid in which white box must be drawn
    t.ht() #hides turtle
    t.lt(90)
    t.setpos(x,y) #sets turtle in poosition according to values in arguments 
    t.fillcolor("white") #sets the colour as white
    t.begin_fill() #fills circle
    t.circle(30) #draws a circle of radius 30
    t.end_fill() #ends filling circle

Есть ли у кого-нибудь идеи, как это решить?

1 ответ

Недостаточно кода, чтобы дать окончательный ответ, но я предполагаю, что проблема в этой строке circle_drawer() функция:

t.lt(90)

Во-первых, это подозрительно, поскольку это единственная строка, которая не комментируется! Во-вторых, мы действительно не знаем, как черепаха ориентирована до или после этого вызова, поскольку это зависит от поворотов, сделанных черепахой до этого вызова функции. Я считаю, что вы действительно хотели:

t.setheading(0)

или любой другой угол, отличный от 0 (нуля). Это заставит черепаху двигаться по известному заголовку, прежде чем рисовать и писать.

Другие вопросы по тегам