Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. from tkinter import *
  2. import random
  3.  
  4. class Parois:
  5. def __init__(self, canvas, positionx):
  6. self.positionx = positionx
  7. self.positiony = 10
  8. self.canvas = canvas
  9. self.maParois = self.canvas.create_rectangle (self.positionx,
  10. self.positiony,
  11. self.positionx + 20,
  12. self.positiony + 280,
  13. fill = "blue")
  14. def detectCollision(self, balle):
  15. if (((balle.positionx + 50 == self.positionx) and
  16. (self.positionx == 280)) or
  17. ((balle.positionx == self.positionx + 20) and
  18. (self.positionx == 20))):
  19. balle.vx = -balle.vx
  20.  
  21.  
  22.  
  23. class Balle:
  24. def __init__(self, canvas):
  25. self.positionx = random.randint(50,200)
  26. self.positiony = random.randint(50,200)
  27. self.vx = random.randint(-1,1)
  28. self.vy = random.randint(-1,1)
  29. if (self.vy == 0 and self.vx == 0):
  30. self.vx = -1
  31. self.canvas = canvas
  32. self.maBalle = self.canvas.create_oval(self.positionx,
  33. self.positiony,
  34. self.positionx+50,
  35. self.positiony+50,
  36. fill="red" )
  37.  
  38. def bouge(self):
  39. self.positionx += self.vx
  40. self.positiony += self.vy
  41. self.canvas.move(self.maBalle, self.vx,
  42. self.vy)
  43.  
  44. def creeBalle():
  45. mesBalles.append(Balle(myCanvas))
  46.  
  47. def myLoop ():
  48. for b in mesBalles:
  49. b.bouge()
  50. maParois1.detectCollision(b)
  51. maParois2.detectCollision(b)
  52. fen.after(1,myLoop)
  53.  
  54. fen = Tk()
  55.  
  56. myCanvas = Canvas(fen, width=300, height=300)
  57. mesBalles = []
  58. monBouton = Button(fen, text="Cree Balle",
  59. command=creeBalle)
  60. maParois1 = Parois(myCanvas, 20)
  61. maParois2 = Parois(myCanvas, 280)
  62. monBouton.pack()
  63. myCanvas.pack()
  64. myLoop()
  65. fen.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement