0
from graphics import *
from math import *
from random import *
import time
import threading


def face(win,r,i,R,G,B):#each circle
    global flag
    global dict1
    global listx
    x1=randint(-250*10000,250*10000)/10000.0
    y1=randint(-120*10000,120*10000)/10000.0
    p=Point(x1,y1)


    c=Circle(p,r)
    c.setFill(color_rgb(R,G,B))
    c.setOutline(color_rgb(R,G,B))
    c.draw(win)


    time.clock()
    t0=time.clock()
    k1=randint(-20*10000,20*10000)/10000.0
    k2=randint(-20*10000,20*10000)/10000.0
    while time.clock()<1000 and flag[i-2]==1:
        time.sleep(0.001)
        t1=time.clock()

        x0=x1+k1*t1
        y0=y1+k2*t1

        dict1[i]=[x0,y0]
        t=t1-t0
        t0=t1
        x2=k1*t
        y2=k2*t
        c.move(x2,y2)
    c.undraw()
    x0=999999
    y0=999999
    dict1[i]=[x0,y0]

def point(x1,x2,x3,y1,y2,y3,x,y,r,i):#whether the circle is in the triangle
    try:


        S=0.5*abs(x1*y2+x2*y3+x3*y1-x3*y2-x2*y1-x1*y3)

        L1=sqrt((x1-x2)**2+(y1-y2)**2)

        L2=sqrt((x2-x3)**2+(y2-y3)**2)

        L3=sqrt((x3-x1)**2+(y3-y1)**2)

        h1=2*S/L1
        h2=2*S/L2
        h3=2*S/L3
        d1=abs(((y2-y1)*x-(x2-x1)*y-x1*y2+x2*y1)/L1)
        d2=abs(((y3-y2)*x-(x3-x2)*y-x2*y3+x3*y2)/L2)
        d3=abs(((y1-y3)*x-(x1-x3)*y-x3*y1+x1*y3)/L3)

        if r<d1<h1 and r<d2<h2 and r<d3<h3 and d1*L1+d2*L2+d3*L3<=2*S+0.01:

            return 1
    except:
        return 0

def judge(win,r,x,n):
    global flag
    global dict1
    global t

    p1=win.getMouse()
    x1,y1=p1.getX(),p1.getY()

    p2=win.getMouse()
    x2,y2=p2.getX(),p2.getY()

    p3=win.getMouse()
    x3,y3=p3.getX(),p3.getY()

    P=Polygon(p1,p2,p3)
    P.draw(win)
    d=dict1
    try:

       t.undraw()
    except:
       t=Text(Point(0,0),str(x))

    t.draw(win)
    for i in range(2,n):
        c=d[i]
        if point(x1,x2,x3,y1,y2,y3,c[0],c[1],dict3[i],i)==1 :

            t.undraw()
            flag[i-2]=0
            x=x+1
            t=Text(Point(0,0),str(x))
            t.draw(win)
    list2=[]
    time.sleep(0.1)
    P.undraw()
    for i in range(2,n):
        list2.append(0)
    while dict1!=list2:

        judge(win,r,x,n)





def game(n,win):
    threads=[]
    for j in range(2,n):
        i=j
        r=randrange(5,25)
        R,G,B=randint(0,255),randint(0,255),randint(0,255)
        t2=threading.Thread(target=face,args=(win,r,i,R,G,B,))
        threads.append(t2)
        flag.append(1)
        list0.append(j)
        dict3[j]=r
    for i in list0:
        listx.append(i)
    t1=threading.Thread(target=judge,args=(win,r,x,n,))
    threads.append(t1)
    for t in threads:
        t.setDaemon(True)
        t.start()

def main():
    global flag
    global dict1
    global x
    global list0
    global listx
    global dict3
    dict3={}
    listx=[]
    x=0
    dict1={}
    list0=[]
    flag=[]
    win=GraphWin("triangle and circle",960,540)
    win.setCoords(-320,-180,320,180)
    input=Entry(Point(0,0),12)
    input.draw(win)
    win.getMouse()
    input.undraw()
    r=eval(input.getText())
    game(r,win)
main()

The purpose is:

If some circles are included in the triangle you click,these circles will disappear.

In the GUI, it usually works except:

Exception _tkinter.TclError: 'out of stack space (infinite loop?)' in <bound method StringVar.__del__ of <Tkinter.StringVar instance at 0x0000000002DD71C8>> ignored

Well, in the py, it shows the same :

_tkinter.TclError: out of stack space (infinite loop?)

How to fix the bug?I didn't use tkinter.

1 Answer 1

3

Your judge function is calling itself recursively in a loop:

while dict1!=list2:
    judge(win,r,x,n)

Moreover since list2 is a local variable (therefore not changing while you are recursively call judge) and I don't see dict1 getting updated in the judge function itself, that loop is going to loop forever.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry,it should be 'while flag!=list2:' and the mistake remains.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.