只有我的tic-tac-toe程序的圆圈移动

时间:2015-10-09 13:53:15

标签: python

以下代码是tic-tac-toe Python游戏的开始。我制作了两只乌龟,一只用于十字架,另一只用于圆圈,但即使满足条件,十字龟似乎也没有移动。

import turtle 

clickCount=0
turtle.setup(750,750)
wn=turtle.Screen()
grid=turtle.Turtle()
grid.speed(15)
grid.pensize(10)
grid.penup()
grid.goto(-300,-300)
grid.pendown()
grid.goto(-300,300)
grid.goto(300,300)
grid.goto(300,-300)
grid.goto(-300,-300)
grid.goto(-300,-100)
grid.goto(300,-100)
grid.goto(300,100)
grid.goto(-300,100)   
grid.penup()
grid.goto(-100,300)
grid.pendown()
grid.goto(-100,-300)
grid.goto(100,-300)
grid.goto(100,300)


cross=turtle.Turtle()
cross.penup()
circle=turtle.Turtle()
circle.shape("circle")
circle.penup()


def clickthingy(x,y): 
    clickCount=0

    if x>-300 and x<-100:
        if y>100 and y<300:
            if clickCount%2==0:
                circle.goto(-200,200)
                clickCount=clickCount+1
            else:
                cross.goto(-200,200)
                clickCount=clickCount+1
        if y<100 and y>-100:
            if clickCount%2==0:
                circle.goto(-200,0)
                clickCount=clickCount+1
            else:
                cross.goto(-200,0)
                clickCount=clickCount+1
        if y<-100 and y>-300:
            if clickCount%2==0:
                circle.goto(-200,-200)
                clickCount=clickCount+1
            else:
                cross.goto(-200,-200)
                clickCount=clickCount+1         

   if x>-100 and x<100:
    if y>100 and y<300:
        if clickCount%2==0:
            circle.goto(0,200)
            clickCount=clickCount+1
        else:
            cross.goto(0,200)
            clickCount=clickCount+1
    if y<100 and y>-100:
        if clickCount%2==0:
            circle.goto(0,0)
            clickCount=clickCount+1
        else:
            cross.goto(0,0)
            clickCount=clickCount+1
    if y<-100 and y>-300:
        if clickCount%2==0:
            circle.goto(0,-200)
            clickCount=clickCount+1
        else:
            cross.goto(0,-200)
            clickCount=clickCount+1   

    if x>100 and x<300:
        if y>100 and y<300:
            if clickCount%2==0:
                circle.goto(200,200)
                clickCount=clickCount+1
            else:
                cross.goto(200,200)
                clickCount=clickCount+1
        if y<100 and y>-100:
            if clickCount%2==0:
                circle.goto(200,0)
                clickCount=clickCount+1
            else:
                cross.goto(200,0)
                clickCount=clickCount+1
            if y<-100 and y>-300:
                if clickCount%2==0:
                    circle.goto(200,-200)
                    clickCount=clickCount+1
                else:
                    cross.goto(200,-200)
                    clickCount=clickCount+1   

wn.onclick(clickthingy)
wn.listen()
wn.mainloop()

1 个答案:

答案 0 :(得分:0)

您每次点击都会将clickCount变量重置为零。改为使其成为全局变量:

clickCount = 0

def clickthingy(x,y): 
    global clickCount
    ....
  

免责声明:虽然这会使它发挥作用,但通常会被认为是可怕的风格。尝试重构你的程序(正如yurib和That1Guy的评论所建议的那样)。