Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import*
- from random import *
- from time import *
- class Ball:
- def __init__(self, canvas, color):
- self.canvas = canvas
- self.id = canvas.create_oval(10,10,25,25,fill=color)
- self.canvas.move(self.id, 225, 180)
- starts = [-3, -2, -1, 1, 2, 3]
- shuffle(starts)
- self.x = choice(starts)
- self.y = choice(starts)
- def draw(self):
- self.canvas.move(self.id, self.x, self.y)
- (x1, y1, x2, y2) = self.canvas.coords(self.id)
- if x2 >= self.canvas.winfo_width() or x1 <= 0:
- self.x *= -1
- if y2 >= self.canvas.winfo_height() or y1 <= 0:
- self.y *= -1
- root = Tk()
- root.title('Пинг-понг')
- canvas = Canvas(width = 500, height = 400, bg = 'deepskyblue')
- canvas.pack()
- balls = []
- for i in range(100):
- R = '%02x'%randint(0,255)
- G = '%02x'%randint(0,255)
- B = '%02x'%randint(0,255)
- color = '#'+R+G+B
- ball = Ball(canvas, color)
- balls.append(ball)
- while True:
- for ball in balls:
- ball.draw()
- root.update_idletasks()
- root.update()
- sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement