Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import Canvas, Tk
- from random import *
- root = Tk()
- canvas_w = 800# ширина холста
- canvas_h = 400# высота холста
- c = Canvas(width = canvas_w, height = canvas_h, bg = 'deepskyblue')
- c.create_rectangle(0, canvas_h // 2, canvas_w, canvas_h, fill = 'light green', width = 0)
- c.pack()
- #создали корзину
- catch = c.create_rectangle(canvas_w//2 - 40, canvas_h - 100, canvas_w//2+40, canvas_h - 80, fill = 'orange', width = 2)
- #движение корзины
- def move_left(event):
- (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
- if catch_x > 0:
- c.move(catch, -20, 0)
- def move_right(event):
- (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
- if catch_x2 < 800:
- c.move(catch, 20, 0)
- def move_up(event):
- (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
- if catch_y > canvas_h // 2:
- c.move(catch, 0, -20)
- def move_down(event):
- (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
- if catch_y2 < canvas_h:
- c.move(catch, 0, 20)
- c.bind('<Left>', move_left)
- c.bind('<Right>', move_right)
- c.bind('<Up>', move_up)
- c.bind('<Down>', move_down)
- c.focus_set()
- #создаем яйцо
- egg_h = 30
- egg_w = 20
- speed = 500 # как часто смещается вниз
- colors = ['red', 'blue', 'green', 'yellow', 'black', 'orange']
- eggs = []
- def create_egg():
- x = randint(0, 800)
- y = 0
- egg = c.create_oval(x, y, x + egg_w, y + egg_h, fill = choice(colors))
- eggs.append(egg)
- root.after(3000, create_egg)
- create_egg()
- def move_egg():
- for egg in eggs:
- c.move(egg, 0, 10)
- (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
- if egg_y2 > canvas_h:
- eggs.remove(egg)
- c.delete(egg)
- root.after(speed, move_egg)
- move_egg()
- def check_catch():
- for egg in eggs:
- (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
- (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
- if catch_x < egg_x and egg_x2 < catch_x2 and catch_y2 > egg_y2 and catch_y < egg_y2:
- eggs.remove(egg)
- c.delete(egg)
- print('Поймали!')
- root.after(100, check_catch)
- check_catch()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement