Advertisement
gorskaja2019

Untitled

Apr 20th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. from tkinter import Canvas, Tk
  2. from random import *
  3.  
  4. root = Tk()
  5.  
  6. canvas_w = 1000# ширина холста
  7. canvas_h = 500# высота холста
  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 - 20:
  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. eggs = []
  44. def create_egg():
  45. x = randint(0, 800)
  46. y = 0
  47. R = '%02x'%randint(0,255)
  48. G = '%02x'%randint(0,255)
  49. B = '%02x'%randint(0,255)
  50. egg = c.create_oval(x, y, x + egg_w, y + egg_h, fill = '#'+R+G+B)
  51. eggs.append(egg)
  52. root.after(3000, create_egg)
  53. create_egg()
  54.  
  55. def move_egg():
  56. for egg in eggs:
  57. c.move(egg, 0, 10)
  58. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  59. if egg_y2 > canvas_h:
  60. eggs.remove(egg)
  61. c.delete(egg)
  62. root.after(speed, move_egg)
  63. move_egg()
  64.  
  65. def check_catch():
  66. for egg in eggs:
  67. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  68. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  69. if catch_x < egg_x and egg_x2 < catch_x2 and catch_y2 > egg_y2 and catch_y < egg_y2:
  70. eggs.remove(egg)
  71. c.delete(egg)
  72. print('Поймали!')
  73. root.after(100, check_catch)
  74. check_catch()
  75. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement