gorskaja2019

Пинг-1

May 1st, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.63 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. class Paddle:
  24.     def __init__(self, canvas, color):
  25.         self.canvas = canvas
  26.         self.id = canvas.create_rectangle(0, 0, 100, 15, fill = color)
  27.         self.canvas.move(self.id, 200, 300)
  28.         self.canvas.bind_all('<Left>', self.move_left)
  29.         self.canvas.bind_all('<Right>', self.move_right)
  30.        
  31.     def move_left(self, event):
  32.         self.canvas.move(self.id, -2, 0)
  33.     def move_right(self, event):
  34.         self.canvas.move(self.id, 2, 0)
  35.  
  36.    
  37. root = Tk()
  38. root.title('Пинг-понг')
  39. canvas = Canvas(width = 500, height = 400, bg = 'deepskyblue')
  40. canvas.pack()
  41.  
  42. paddle = Paddle(canvas, 'orange')
  43. balls = []
  44. for i in range(3):
  45.     R = '%02x'%randint(0,255)
  46.     G = '%02x'%randint(0,255)
  47.     B = '%02x'%randint(0,255)
  48.     color = '#'+R+G+B
  49.     ball = Ball(canvas, color)
  50.     balls.append(ball)
  51.  
  52. while True:
  53.     for ball in balls:
  54.         ball.draw()
  55.     root.update_idletasks()
  56.     root.update()
  57.     sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment