Advertisement
gorskaja2019

Пинг-2

May 1st, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 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.x = 0
  29.         self.canvas.bind_all('<Left>', self.move_left)
  30.         self.canvas.bind_all('<Right>', self.move_right)
  31.        
  32.     def draw(self):
  33.         self.canvas.move(self.id, self.x, 0)
  34.         (x1, y1, x2, y2) = self.canvas.coords(self.id)
  35.         if x1 <= 0:
  36.             self.x = 0
  37.         elif x2 >= 500:
  38.             self.x = 0
  39.        
  40.     def move_left(self, event):
  41.         self.x = -2
  42.         #self.canvas.move(self.id, -2, 0)
  43.        
  44.     def move_right(self, event):
  45.         self.x = 2
  46.         #self.canvas.move(self.id, 2, 0)
  47.  
  48.    
  49. root = Tk()
  50. root.title('Пинг-понг')
  51. canvas = Canvas(width = 500, height = 400, bg = 'deepskyblue')
  52. canvas.pack()
  53.  
  54. paddle = Paddle(canvas, 'orange')
  55. balls = []
  56. for i in range(3):
  57.     R = '%02x'%randint(0,255)
  58.     G = '%02x'%randint(0,255)
  59.     B = '%02x'%randint(0,255)
  60.     color = '#'+R+G+B
  61.     ball = Ball(canvas, color)
  62.     balls.append(ball)
  63.  
  64. while True:
  65.     for ball in balls:
  66.         ball.draw()
  67.     paddle.draw()
  68.     root.update_idletasks()
  69.     root.update()
  70.     sleep(0.01)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement