Advertisement
gorskaja2019

Untitled

Apr 20th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 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 - 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. egg_time = 3000 # как часто появляются
  43. eggs = []
  44. def create_egg():
  45. global egg_time
  46. x = randint(0, 800)
  47. y = 0
  48. R = '%02x'%randint(0,255)
  49. G = '%02x'%randint(0,255)
  50. B = '%02x'%randint(0,255)
  51. egg = c.create_oval(x, y, x + egg_w, y + egg_h, fill = '#'+R+G+B)
  52. eggs.append(egg)
  53. root.after(egg_time, create_egg)
  54. create_egg()
  55.  
  56. def move_egg():
  57. global speed
  58. for egg in eggs:
  59. c.move(egg, 0, 10)
  60. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  61. if egg_y2 > canvas_h:
  62. eggs.remove(egg)
  63. c.delete(egg)
  64. root.after(speed, move_egg)
  65. move_egg()
  66.  
  67. def check_catch():
  68. global egg_time, speed
  69. for egg in eggs:
  70. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  71. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  72. if catch_x < egg_x and egg_x2 < catch_x2 and egg_y2 > catch_y2 - 30:
  73. eggs.remove(egg)
  74. c.delete(egg)
  75. print('Поймали!')
  76. #увеличить частоту появления яиц и скорость падения
  77. egg_time = int(0.9 * egg_time)
  78. speed = int(0.9 * speed)
  79. root.after(100, check_catch)
  80. check_catch()
  81. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement