SHARE
TWEET

Untitled

gorskaja2019 Apr 20th, 2019 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from tkinter import Canvas, Tk
  2. from random import *
  3.  
  4. root = Tk()
  5.  
  6. canvas_w = 800# ширина холста
  7. canvas_h = 400# высота холста
  8. c = Canvas(width = canvas_w, height = canvas_h, bg = 'deepskyblue')
  9. c.create_rectangle(0, canvas_h // 2, canvas_w, canvas_h, fill = 'light green', width = 0)
  10. c.pack()
  11. #создали корзину
  12. catch = c.create_rectangle(canvas_w//2 - 40, canvas_h - 100, canvas_w//2+40, canvas_h - 80, fill = 'orange', width = 2)
  13. #движение корзины
  14. def move_left(event):
  15.     (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  16.     if catch_x > 0:
  17.         c.move(catch, -20, 0)
  18. def move_right(event):
  19.     (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  20.     if catch_x2 < 800:
  21.         c.move(catch, 20, 0)
  22. def move_up(event):
  23.     (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  24.     if catch_y > canvas_h // 2:
  25.         c.move(catch, 0, -20)
  26. def move_down(event):
  27.     (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  28.     if catch_y2 < canvas_h:
  29.         c.move(catch, 0, 20)        
  30.  
  31. c.bind('<Left>', move_left)
  32. c.bind('<Right>', move_right)
  33. c.bind('<Up>', move_up)
  34. c.bind('<Down>', move_down)
  35. c.focus_set()
  36.  
  37. #создаем яйцо
  38. egg_h = 30
  39. egg_w = 20
  40. speed = 500 # как часто смещается вниз
  41.  
  42. colors = ['red', 'blue', 'green', 'yellow', 'black', 'orange']
  43. eggs = []
  44. def create_egg():
  45.     x = randint(0, 800)
  46.     y = 0
  47.     egg = c.create_oval(x, y, x + egg_w, y + egg_h, fill = choice(colors))
  48.     eggs.append(egg)
  49.     root.after(3000, create_egg)
  50. create_egg()
  51.  
  52. def move_egg():
  53.     for egg in eggs:
  54.         c.move(egg, 0, 10)
  55.         (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  56.         if egg_y2 > canvas_h:
  57.             eggs.remove(egg)
  58.             c.delete(egg)
  59.     root.after(speed, move_egg)
  60. move_egg()
  61.  
  62. def check_catch():
  63.     for egg in eggs:
  64.         (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  65.         (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  66.         if catch_x < egg_x and egg_x2 < catch_x2 and catch_y2 > egg_y2 and catch_y < egg_y2:
  67.             eggs.remove(egg)
  68.             c.delete(egg)
  69.             print('Поймали!')
  70.     root.after(100, check_catch)
  71. check_catch()
  72. root.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top