gorskaja2019

Заготовка для пинг-понга

May 1st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. from tkinter import*
  2. from random import *
  3. from time import *
  4.  
  5. class Ball:
  6.     def __init__(self, canvas, color):
  7.         self.canvas = canvas
  8.         self.id = canvas.create_oval(10,10,25,25,fill=color)
  9.         self.canvas.move(self.id, 225, 180)
  10.         starts = [-3, -2, -1, 1, 2, 3]
  11.         shuffle(starts)
  12.         self.x = choice(starts)
  13.         self.y = choice(starts)
  14.        
  15.     def draw(self):
  16.         self.canvas.move(self.id, self.x, self.y)
  17.         (x1, y1, x2, y2) = self.canvas.coords(self.id)
  18.         if x2 >= self.canvas.winfo_width() or x1 <= 0:
  19.             self.x *= -1
  20.         if y2 >= self.canvas.winfo_height() or y1 <= 0:
  21.             self.y *= -1
  22.  
  23. root = Tk()
  24. root.title('Пинг-понг')
  25. canvas = Canvas(width = 500, height = 400, bg = 'deepskyblue')
  26. canvas.pack()
  27.  
  28. balls = []
  29. for i in range(100):
  30.     R = '%02x'%randint(0,255)
  31.     G = '%02x'%randint(0,255)
  32.     B = '%02x'%randint(0,255)
  33.     color = '#'+R+G+B
  34.     ball = Ball(canvas, color)
  35.     balls.append(ball)
  36.  
  37. while True:
  38.     for ball in balls:
  39.         ball.draw()
  40.     root.update_idletasks()
  41.     root.update()
  42.     sleep(0.01)
Add Comment
Please, Sign In to add comment