Guest User

Code

a guest
Feb 14th, 2015
1,411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. from tkinter import *
  2. from random import randint
  3.  
  4. #Return random color #RRGGBB
  5. def getRandomColor():
  6.     color = '#'
  7.     for j in range(6):
  8.         color+= toHexChar(randint(0, 15))
  9.     return color
  10.  
  11. #Convert an integer to a single hex digit in a character
  12. def toHexChar(hexValue):
  13.     if 0 <= hexValue <= 9:
  14.         return chr(hexValue+ord('0'))
  15.     else:
  16.         return chr(hexValue - 10 + ord('A'))
  17.  
  18. #Define ball class
  19. class Ball:
  20.     def __init__(self):
  21.         self.x = 0
  22.         self.y = 0
  23.         self.dx = 2
  24.         self.dy = 2
  25.         self.radius = 9
  26.         self.color = getRandomColor()
  27.  
  28.  
  29. class BounceBalls():
  30.     def __init__(self):
  31.         self.ballList = []
  32.         window = Tk()
  33.         window.title("Bouncing Balls")
  34.         self.width = 350
  35.         self.height = 150
  36.         self.canvas = Canvas(window, bg = "white", width = self.width, height = self.height)
  37.         self.canvas.pack()
  38.  
  39.         frame = Frame(window)
  40.         frame.pack()
  41.  
  42.         buttonStop = Button(frame, text = "Stop", command = self.stop)
  43.         buttonStop.pack(side = LEFT)
  44.  
  45.         buttonFaster = Button(frame, text = "Faster", command = self.increaseBallSpeed)
  46.         buttonFaster.pack(side = LEFT)
  47.  
  48.         buttonSlower = Button(frame, text = "Slower", command = self.decreaseBallSpeed)
  49.         buttonSlower.pack(side = LEFT)
  50.  
  51.         buttonResume = Button(frame, text = "Resume", command = self.resume)
  52.         buttonResume.pack(side = LEFT)
  53.  
  54.         buttonAdd = Button(frame, text = "Add", command = self.add)
  55.         buttonAdd.pack(side = LEFT)
  56.  
  57.         buttonRemove = Button(frame, text = "Remove", command = self.remove)
  58.         buttonRemove.pack(side = LEFT)
  59.  
  60.  
  61.         self.sleepTime = 50
  62.         self.isStopped = False
  63.         self.animate()
  64.         window.mainloop()
  65.  
  66.  
  67.     def stop(self): #Stop animation
  68.         self.isStopped = True
  69.  
  70.     def resume(self): #Resume animation
  71.         self.isStopped = False
  72.         self.animate()
  73.  
  74.     def add(self): #Add a new ball
  75.         self.ballList.append(Ball())
  76.  
  77.     def remove(self): #Remove the last ball
  78.         self.ballList.pop()
  79.  
  80.     def animate(self): #Animate ball movements
  81.         while not self.isStopped:
  82.             self.canvas.after(self.sleepTime)
  83.             self.canvas.update()
  84.             self.canvas.delete("ball")
  85.             for ball in self.ballList:
  86.                 self.redisplayBall(ball)
  87.  
  88.  
  89.     def increaseBallSpeed(self):
  90.         if self.sleepTime <= 10:
  91.             self.sleepTime = 10
  92.         else:
  93.             self.sleepTime-=10
  94.  
  95.     def decreaseBallSpeed(self):
  96.         self.sleepTime+=10
  97.  
  98.  
  99.     def redisplayBall(self, ball):
  100.         if ball.x > self.width or ball.x < 0:
  101.             ball.dx = -ball.dx
  102.         if ball.y > self.height or ball.y < 0:
  103.             ball.dy = -ball.dy
  104.  
  105.  
  106.         ball.x += ball.dx
  107.         ball.y += ball.dy
  108.         self.canvas.create_oval(ball.x - ball.radius, ball.y - ball.radius,
  109.                                 ball.x + ball.radius, ball.y + ball.radius,
  110.                                 fill = ball.color, tags = "ball")
  111.  
  112.  
  113. BounceBalls()
Advertisement
Add Comment
Please, Sign In to add comment