我一直在尝试使用Python的海龟模块创建一个零和交叉游戏。我经常遇到的问题是,用来覆盖每个网格正方形的数字的白色圆圈(这样就可以在那里写上零和叉)不是每次程序运行时乌龟在同一个地方画的——它们在一个相似的地方,但有点跳跃,因此不再覆盖下面的字母。下面是我创建圆圈的代码部分:
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
有人有办法解决这个问题吗
没有足够的代码给出明确的答案,但我猜问题出在
circle_drawer()
函数的这一行:首先,它是可疑的,因为它是唯一一行没有评论的!第二,我们真的不知道海龟在调用之前或之后是如何定位的,因为它取决于海龟在调用函数之前所做的旋转。我相信你真的想要:
或者其他角度,而不是0(零)。这将在绘制和书写之前,将海龟设置为一致的已知标题
相关问题 更多 >
编程相关推荐