Advertisement
gorskaja2019

Untitled

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