Demo entry 5873824

Exercice3

   

Submitted by anonymous on Aug 25, 2016 at 16:14
Language: Python 3. Code size: 2.1 kB.

<p>'''
Created on 23 août 2016</p>

<p>@author: vfsilesieux
'''
balles=[]
from random import randint</p>

<p>x,y=150,10
dx,dy=10,10
flag=0
speedx,speedy=0,0</p>

<p>def anime():
    for i in range(0,len(balles)):
        balles[i].move()
    fenetre.after(50,anime)
def start():
    for i in range(0,len(balles)):
        balles[i].start()
    anime()
def stop():
    for i in range(0,len(balles)):
        balles[i].stop()
        print(i)
from tkinter import *
fenetre = Tk()
fenetre.geometry('350x300')
can = Canvas(fenetre,bg='dark grey',height=250, width=250)
can.pack(side=LEFT,padx=5,pady=5)</p>

<p>boutonGO = Button(fenetre, text='GO', width =3,command=start)
boutonGO.pack(side=BOTTOM)
boutonSTOP = Button(fenetre, text='STOP', width =3,command=stop)
boutonSTOP.pack(side=BOTTOM)</p>

<p>class Balles(object):
    def <strong>init</strong>(self, rayon, couleur,x,y,dx,dy):
        self.rayon=rayon
        self.couleur=couleur
        self.x=x
        self.dx=dx
        self.y=y
        self.dy=dy
        self.oval=can.create_oval(self.x,self.y,self.x+10,self.y+10,width=2,fill=self.couleur)</p>

<pre><code>def start(self):
    global flag
    if flag==0:
        self.dx,self.dy=speedx,speedy
    flag=1

def stop(self):
    global speedx,speedy
    speedx,speedy=self.dx,self.dy
    global flag
    flag=0

def move(self):

    if flag==0:     
        self.dx,self.dy=0,0
    if self.x&gt;240:
        self.dx=-self.dx
    if self.x&lt;10:
        self.dx=-self.dx
    if self.y&lt;10:
        self.dy=-self.dy
    if self.y&gt;240:
        self.dy=-self.dy
    self.x,self.y=self.x+self.dx,self.y+self.dy
    can.coords(self.oval,self.x,self.y,self.x+10,self.y+10)
</code></pre>

<p>for i in range(10):
    balles.append(Balles(2,"blue",randint(10,240),randint(10,240),randint(-10,10),randint(-10,10)))
    print(i)
for j in range(10):
    balles.append(Balles(2,"red",randint(10,240),randint(10,240),randint(-10,10),randint(-10,10)))
    print(j)
print (len(balles))
fenetre.mainloop()</p>

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).