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, paddle, color):
- self.canvas = canvas
- self.paddle = paddle
- 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
- class Paddle:
- def __init__(self, canvas, color):
- self.canvas = canvas
- self.id = canvas.create_rectangle(0, 0, 100, 15, fill = color)
- self.canvas.move(self.id, 200, 300)
- self.x = 0
- self.canvas.bind_all('<Left>', self.move_left)
- self.canvas.bind_all('<Right>', self.move_right)
- def draw(self):
- self.canvas.move(self.id, self.x, 0)
- (x1, y1, x2, y2) = self.canvas.coords(self.id)
- if x1 <= 0 or x2 >= 500:
- self.x *= -1
- self.canvas.move(self.id, 5*self.x, 0)
- def move_left(self, event):
- self.x = -2
- #self.canvas.move(self.id, -2, 0)
- def move_right(self, event):
- self.x = 2
- #self.canvas.move(self.id, 2, 0)
- root = Tk()
- root.title('Пинг-понг')
- canvas = Canvas(width = 500, height = 400, bg = 'deepskyblue')
- canvas.pack()
- HarryPotter = Paddle(canvas, 'orange')
- balls = []
- for i in range(3):
- R = '%02x'%randint(0,255)
- G = '%02x'%randint(0,255)
- B = '%02x'%randint(0,255)
- color = '#'+R+G+B
- ball = Ball(canvas, HarryPotter, color)
- balls.append(ball)
- while True:
- for ball in balls:
- ball.draw()
- HarryPotter.draw()
- root.update_idletasks()
- root.update()
- sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement