Advertisement
gorskaja2019

Untitled

Apr 20th, 2019
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. from tkinter import Canvas, Tk, messagebox, Button
  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. def f(event):
  13. main(1)
  14. but1.unbind('<Button-1>', f)
  15. but2.unbind('<Button-1>', g)
  16.  
  17. def g(event):
  18. main(2)
  19. but1.unbind('<Button-1>', f)
  20. but2.unbind('<Button-1>', g)
  21.  
  22. but1 = Button(bg = 'yellow', text = 'Уровень 1')
  23. but1.pack()
  24. but1.bind('<Button-1>',f)
  25. but2 = Button(bg = 'yellow', text = 'Уровень 2')
  26. but2.pack()
  27. but2.bind('<Button-1>',g)
  28.  
  29. def main(tmp):
  30. #текстовые поля
  31. global score
  32. score = 0
  33. score_text = c.create_text(10, 10, anchor = 'nw', font = 'Verdana 18', fill = 'green', text = 'Счет: '+str(score))
  34. global lives
  35. lives = 3
  36. lives_text = c.create_text(canvas_w - 10, 0, anchor = 'ne', font = 'Verdana 18', fill = 'green', text = 'Жизни: '+str(lives))
  37.  
  38. #создали корзину
  39. #catch = c.create_rectangle(canvas_w//2 - 40, canvas_h - 100, canvas_w//2+40, canvas_h - 80, fill = 'orange', width = 2)
  40. 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')
  41. #движение корзины
  42. def move_left(event):
  43. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  44. if catch_x > 0:
  45. c.move(catch, -20, 0)
  46. def move_right(event):
  47. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  48. if catch_x2 < 800:
  49. c.move(catch, 20, 0)
  50. def move_up(event):
  51. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  52. if catch_y > canvas_h // 2 - 20:
  53. c.move(catch, 0, -20)
  54. def move_down(event):
  55. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  56. if catch_y2 < canvas_h:
  57. c.move(catch, 0, 20)
  58.  
  59. c.bind('<Left>', move_left)
  60. c.bind('<Right>', move_right)
  61. c.bind('<Up>', move_up)
  62. c.bind('<Down>', move_down)
  63. c.focus_set()
  64.  
  65. #создаем яйцо
  66. global egg_h
  67. egg_h = 30
  68. global egg_w
  69. egg_w = 20
  70.  
  71. global speed, egg_time, difficulty
  72. if tmp == 1:
  73. speed = 500 # как часто смещается вниз 400
  74. egg_time = 4000 # как часто появляются 2000
  75. difficulty = 0.95 # difficulty = 0.9
  76. else:
  77. speed = 400 # как часто смещается вниз 400
  78. egg_time = 2000 # как часто появляются 2000
  79. difficulty = 0.9 # difficulty = 0.9
  80. eggs = []
  81. def create_egg():
  82. global egg_time
  83. x = randint(0, 800)
  84. y = 0
  85. R = '%02x'%randint(0,255)
  86. G = '%02x'%randint(0,255)
  87. B = '%02x'%randint(0,255)
  88. egg = c.create_oval(x, y, x + egg_w, y + egg_h, fill = '#'+R+G+B)
  89. eggs.append(egg)
  90. root.after(egg_time, create_egg)
  91. create_egg()
  92.  
  93. def move_egg():
  94. global speed, lives
  95. for egg in eggs:
  96. c.move(egg, 0, 10)
  97. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  98. if egg_y2 > canvas_h:
  99. eggs.remove(egg)
  100. c.delete(egg)
  101. lives -= 1
  102. c.itemconfig(lives_text, text = 'Жизни: '+str(lives))
  103. #если жизни закончились, конец игры
  104. if lives == 0:
  105. messagebox.showinfo('Конец игры!','Вы набрали '+str(score)+' очков')
  106. root.destroy()
  107. root.after(speed, move_egg)
  108. move_egg()
  109.  
  110. def check_catch():
  111. global egg_time, speed, score, difficulty
  112. for egg in eggs:
  113. (catch_x, catch_y, catch_x2, catch_y2) = c.coords(catch)
  114. (egg_x, egg_y, egg_x2, egg_y2) = c.coords(egg)
  115. if catch_x < egg_x and egg_x2 < catch_x2 and egg_y2 > catch_y2 - 30:
  116. eggs.remove(egg)
  117. c.delete(egg)
  118. score += 10
  119. c.itemconfig(score_text, text = 'Счет: '+str(score))
  120. #увеличить частоту появления яиц и скорость падения
  121. egg_time = int(difficulty * egg_time)
  122. speed = int(difficulty * speed)
  123. root.after(100, check_catch)
  124. check_catch()
  125. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement