Python中的一个小游戏

时间:2015-11-28 07:59:47

标签: python

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()

目的是:

  

如果您点击的三角形中包含某些圆圈,则这些圆圈将消失。

在GUI中,它通常可以工作,除了:

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

好吧,在py中,它显示了相同的内容:

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

如何修复错误?我没有使用tkinter。

1 个答案:

答案 0 :(得分:2)

您的judge函数在循环中递归调用自身:

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

此外,因为list2是一个局部变量(因此在递归调用judge时不会改变),我在dict1函数中看不到judge更新本身,这个循环将永远循环。