gorskaja2019

Untitled

Apr 17th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  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.  
  23. c.bind('<Left>', move_left)
  24. c.bind('<Right>', move_right)
  25. c.focus_set()
  26.  
  27. #создаем яйцо
  28. egg_h = 30
  29. egg_w = 20
  30. speed = 500 # как часто смещается вниз
  31.  
  32. colors = ['red', 'blue', 'green', 'yellow', 'black', 'orange']
  33. eggs = []
  34. def create_egg():
  35. x = randint(0, 800)
  36. y = 0
  37. egg = c.create_oval(x, y, x + egg_w, y + egg_h, fill = choice(colors))
  38. eggs.append(egg)
  39. root.after(3000, create_egg)
  40. create_egg()
  41.  
  42. def move_egg():
  43. for egg in eggs:
  44. c.move(egg, 0, 10)
  45. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  46. if egg_y2 > canvas_h:
  47. eggs.remove(egg)
  48. c.delete(egg)
  49. root.after(speed, move_egg)
  50. move_egg()
  51.  
  52. def check_catch():
  53. for egg in eggs:
  54. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  55. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  56. if catch_x < egg_x and egg_x2 < catch_x2 and catch_y2 > egg_y2 and catch_y < egg_y2:
  57. eggs.remove(egg)
  58. c.delete(egg)
  59. print('Поймали!')
  60. root.after(100, check_catch)
  61. check_catch()
  62. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment