Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from random import randrange as rnd, choice
- root = Tk()
- root.geometry('800x600')
- canv = Canvas(root, bg = 'white')
- canv.pack(fill = BOTH, expand = 1)
- colors = ['red', 'orange', 'yellow', 'green', 'blue']
- i = 0
- class Ball():
- def __init__(self):
- root.update_idletasks()
- size = root.geometry()
- x_ball = size.split('x')[0]
- size2 = size.split('x')[1]
- y_ball = size2.split('+')[0]
- self.r = rnd(20, 50)
- self.x = rnd(self.r, (int(x_ball) - self.r))
- self.y = rnd(self.r, (int(y_ball) - self.r))
- self.color = choice(colors)
- self.ball = canv.create_oval(self.x - self.r, self.y - self.r,
- self.x + self.r, self.y + self.r,
- fill = self.color)
- self.dx = choice((-5, -4, -3, 3, 4, 5))
- self.dy = choice((self.dx, -(self.dx)))
- def mirror(self):
- root.update_idletasks()
- size = root.geometry()
- x_ball = size.split('x')[0]
- size2 = size.split('x')[1]
- y_ball = size2.split('+')[0]
- self.x += self.dx
- self.y += self.dy
- if canv.coords(self.ball)[0] <= 0:
- self.dx = - (self.dx)
- if canv.coords(self.ball)[1] <= 0:
- self.dy = - (self.dy)
- if canv.coords(self.ball)[2] >= int(x_ball):
- self.dx = - (self.dx)
- if canv.coords(self.ball)[3] >= int(y_ball):
- self.dy = - (self.dy)
- def move(self):
- canv.move(self.ball, self.dx, self.dy)
- def click(self, event):
- global i
- if self.x > event.x:
- if self.y > event.y:
- if self.x - event.x <= self.r and self.y - event.y <= self.r:
- print(True)
- i += 1
- print(i)
- elif self.y < event.y:
- if self.x - event.x <= self.r and event.y - self.y <= self.r:
- print(True)
- i += 1
- print(i)
- elif self.x < event.x:
- if self.y > event.y:
- if event.x - self.x <= self.r and self.y - event.y <= self.r:
- print(True)
- i += 1
- print(i)
- elif self.y < event.y:
- if event.x - self.x <= self.r and event.y - self.r <= self.r:
- print(True)
- i += 1
- print(i)
- balls = [Ball() for ball in range(3)]
- def tick():
- for ball in balls:
- ball.mirror()
- ball.move()
- root.bind('<Button-1>', ball.click)
- root.after(10, tick)
- tick()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement