a

Sunday, 19 February 2017

Code for simple Animation loop in python tkinter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import time

from Tkinter import*

screen=Tk()
drawingboard=Canvas(screen,width =400,height=400,bg='white' )
drawingboard.pack()
ball1=drawingboard.create_oval(10,20,30,40,fill='red')
ball2=drawingboard.create_oval(10,20,30,40,fill='blue')
ball3=drawingboard.create_oval(10,20,30,40,fill='green')

x=5
y=3
a=2
b=-5
p=2
q=-7

while True:
    
    drawingboard.move(ball1,x,y)
    drawingboard.move(ball2,a,b)
    drawingboard.move(ball3,p,q)
    destiny=drawingboard.coords(ball3)
    if destiny[1]>=400:
        q=-q
    if destiny[1]<=0:
        q=-q
    if destiny[2]>=400:
        p=-p
    if destiny[2]<=0:
        p=-p
    
    place=drawingboard.coords(ball2)
    if place[1]>=400:
        b=-b
    if place[1]<=0:
        b=-b
    if place[2]>=400:
        a=-a
    if place[2]<=0:
        a=-a
   
    
    position=drawingboard.coords(ball1)
    if position[2]>=400:
        x=-x
        
        
    if position[2]<=0:
        x=-x
    if position[1]>=400:
        y=-y
    if position[1]<=0:
        y=-y

   
    
    screen.update()
    time.sleep(0.02)



    
    
screen.mainloop()


1 comment: