Advertisement
gorskaja2019

Untitled

Apr 20th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 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. score = 0
  13. score_text = c.create_text(10, 10, anchor = 'nw', font = 'Verdana 18', fill = 'green', text = 'Счет: '+str(score))
  14. lives = 5
  15. lives_text = c.create_text(canvas_w - 10, 0, anchor = 'ne', font = 'Verdana 18', fill = 'green', text = 'Жизни: '+str(lives))
  16.  
  17.  
  18.  
  19. #создали корзину
  20. #catch = c.create_rectangle(canvas_w//2 - 40, canvas_h - 100, canvas_w//2+40, canvas_h - 80, fill = 'orange', width = 2)
  21. 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')
  22. #движение корзины
  23. def move_left(event):
  24. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  25. if catch_x > 0:
  26. c.move(catch, -20, 0)
  27. def move_right(event):
  28. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  29. if catch_x2 < 800:
  30. c.move(catch, 20, 0)
  31. def move_up(event):
  32. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  33. if catch_y > canvas_h // 2 - 20:
  34. c.move(catch, 0, -20)
  35. def move_down(event):
  36. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  37. if catch_y2 < canvas_h:
  38. c.move(catch, 0, 20)
  39.  
  40. c.bind('<Left>', move_left)
  41. c.bind('<Right>', move_right)
  42. c.bind('<Up>', move_up)
  43. c.bind('<Down>', move_down)
  44. c.focus_set()
  45.  
  46. #создаем яйцо
  47. egg_h = 30
  48. egg_w = 20
  49. speed = 500 # как часто смещается вниз
  50. egg_time = 3000 # как часто появляются
  51. eggs = []
  52. def create_egg():
  53. global egg_time
  54. x = randint(0, 800)
  55. y = 0
  56. R = '%02x'%randint(0,255)
  57. G = '%02x'%randint(0,255)
  58. B = '%02x'%randint(0,255)
  59. egg = c.create_oval(x, y, x + egg_w, y + egg_h, fill = '#'+R+G+B)
  60. eggs.append(egg)
  61. root.after(egg_time, create_egg)
  62. create_egg()
  63.  
  64. def move_egg():
  65. global speed
  66. for egg in eggs:
  67. c.move(egg, 0, 10)
  68. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  69. if egg_y2 > canvas_h:
  70. eggs.remove(egg)
  71. c.delete(egg)
  72. root.after(speed, move_egg)
  73. move_egg()
  74.  
  75. def check_catch():
  76. global egg_time, speed
  77. for egg in eggs:
  78. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  79. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  80. if catch_x < egg_x and egg_x2 < catch_x2 and egg_y2 > catch_y2 - 30:
  81. eggs.remove(egg)
  82. c.delete(egg)
  83. print('Поймали!')
  84. #увеличить частоту появления яиц и скорость падения
  85. egg_time = int(0.9 * egg_time)
  86. speed = int(0.9 * speed)
  87. root.after(100, check_catch)
  88. check_catch()
  89. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement