Advertisement
gorskaja2019

Untitled

Apr 20th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. from tkinter import Canvas, Tk, messagebox
  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 = 3
  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 = 4000 # как часто появляются
  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, lives
  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. lives -= 1
  73. c.itemconfig(lives_text, text = 'Жизни: '+str(lives))
  74. #если жизни закончились, конец игры
  75. if lives == 0:
  76. messagebox.showinfo('Конец игры!','Вы набрали '+str(score)+' очков')
  77. root.destroy()
  78. root.after(speed, move_egg)
  79. move_egg()
  80.  
  81. def check_catch():
  82. global egg_time, speed, score
  83. for egg in eggs:
  84. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  85. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  86. if catch_x < egg_x and egg_x2 < catch_x2 and egg_y2 > catch_y2 - 30:
  87. eggs.remove(egg)
  88. c.delete(egg)
  89. score += 10
  90. c.itemconfig(score_text, text = 'Счет: '+str(score))
  91. #увеличить частоту появления яиц и скорость падения
  92. egg_time = int(0.9 * egg_time)
  93. speed = int(0.9 * speed)
  94. root.after(100, check_catch)
  95. check_catch()
  96. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement