简体   繁体   English

Python 的 Turtle 模块 - 圆圈故障?

[英]Turtle module for Python - circles glitch?

I have been trying to create a naughts and crosses game using Python's Turtle module.我一直在尝试使用 Python 的 Turtle 模块创建一个零零碎碎的游戏。 The issue I keep running into is that the white circles used to cover up the numbers labelling each grid square (so that naughts and crosses can be written there instead) aren't drawn by the turtle in the same place each time the program runs - they are in a similar place, but have jumped slightly and so no longer cover up the letter below.我一直遇到的问题是,每次程序运行时,乌龟都不会在同一位置绘制用于覆盖标记每个网格方块的数字的白色圆圈(以便可以在那里写零和十字)-他们在一个相似的地方,但略有跳跃,因此不再掩盖下面的字母。 Here's the section of my code which creates the circles:这是我创建圆圈的代码部分:

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

Does anyone have any ideas to solve this?有没有人有任何想法来解决这个问题?

There's not sufficient code to give a definitive answer but my guess is the problem is in this line of the circle_drawer() function:没有足够的代码来给出明确的答案,但我的猜测是问题出在circle_drawer()函数的这一行中:

t.lt(90)

First it's suspicious as it's the only line that isn't commented!首先它是可疑的,因为它是唯一没有注释的行! Second, we really don't know how the turtle is oriented before or after this call as it depends on turns the turtle's made prior to this function call.其次,我们真的不知道海龟在这个调用之前或之后是如何定向的,因为这取决于海龟在这个函数调用之前的旋转。 My belief is you really wanted:我的信念是你真的想要:

t.setheading(0)

or some other angle other than 0 (zero).或 0(零)以外的其他角度。 This sets the turtle to a consistent known heading before doing your drawing and writing.在进行绘图和书写之前,这会将海龟设置为一致的已知标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM