Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import random
- class Parois:
- def __init__(self, canvas, positionx):
- self.positionx = positionx
- self.positiony = 10
- self.canvas = canvas
- self.maParois = self.canvas.create_rectangle (self.positionx,
- self.positiony,
- self.positionx + 20,
- self.positiony + 280,
- fill = "blue")
- def detectCollision(self, balle):
- if (((balle.positionx + 50 == self.positionx) and
- (self.positionx == 280)) or
- ((balle.positionx == self.positionx + 20) and
- (self.positionx == 20))):
- balle.vx = -balle.vx
- class Balle:
- def __init__(self, canvas):
- self.positionx = random.randint(50,200)
- self.positiony = random.randint(50,200)
- self.vx = random.randint(-1,1)
- self.vy = random.randint(-1,1)
- if (self.vy == 0 and self.vx == 0):
- self.vx = -1
- self.canvas = canvas
- self.maBalle = self.canvas.create_oval(self.positionx,
- self.positiony,
- self.positionx+50,
- self.positiony+50,
- fill="red" )
- def bouge(self):
- self.positionx += self.vx
- self.positiony += self.vy
- self.canvas.move(self.maBalle, self.vx,
- self.vy)
- def creeBalle():
- mesBalles.append(Balle(myCanvas))
- def myLoop ():
- for b in mesBalles:
- b.bouge()
- maParois1.detectCollision(b)
- maParois2.detectCollision(b)
- fen.after(1,myLoop)
- fen = Tk()
- myCanvas = Canvas(fen, width=300, height=300)
- mesBalles = []
- monBouton = Button(fen, text="Cree Balle",
- command=creeBalle)
- maParois1 = Parois(myCanvas, 20)
- maParois2 = Parois(myCanvas, 280)
- monBouton.pack()
- myCanvas.pack()
- myLoop()
- fen.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement