gorskaja2019

Untitled

Apr 20th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 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. catch = c.create_arc(canvas_w//2 - 40, canvas_h - 100, canvas_w//2+40, canvas_h - 60, start = 200, extent = 140, fill = 'yellow', outline = 'yellow', width = 3, style = 'chord')
  14. #движение корзины
  15. def move_left(event):
  16. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  17. if catch_x > 0:
  18. c.move(catch, -20, 0)
  19. def move_right(event):
  20. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  21. if catch_x2 < 800:
  22. c.move(catch, 20, 0)
  23. def move_up(event):
  24. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  25. if catch_y > canvas_h // 2:
  26. c.move(catch, 0, -20)
  27. def move_down(event):
  28. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  29. if catch_y2 < canvas_h:
  30. c.move(catch, 0, 20)
  31.  
  32. c.bind('<Left>', move_left)
  33. c.bind('<Right>', move_right)
  34. c.bind('<Up>', move_up)
  35. c.bind('<Down>', move_down)
  36. c.focus_set()
  37.  
  38. #создаем яйцо
  39. egg_h = 30
  40. egg_w = 20
  41. speed = 500 # как часто смещается вниз
  42.  
  43. colors = ['red', 'blue', 'green', 'yellow', 'black', 'orange']
  44. eggs = []
  45. def create_egg():
  46. x = randint(0, 800)
  47. y = 0
  48. egg = c.create_oval(x, y, x + egg_w, y + egg_h, fill = choice(colors))
  49. eggs.append(egg)
  50. root.after(3000, create_egg)
  51. create_egg()
  52.  
  53. def move_egg():
  54. for egg in eggs:
  55. c.move(egg, 0, 10)
  56. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  57. if egg_y2 > canvas_h:
  58. eggs.remove(egg)
  59. c.delete(egg)
  60. root.after(speed, move_egg)
  61. move_egg()
  62.  
  63. def check_catch():
  64. for egg in eggs:
  65. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  66. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  67. if catch_x < egg_x and egg_x2 < catch_x2 and catch_y2 > egg_y2 and catch_y < egg_y2:
  68. eggs.remove(egg)
  69. c.delete(egg)
  70. print('Поймали!')
  71. root.after(100, check_catch)
  72. check_catch()
  73. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment