Advertisement
gorskaja2019

Untitled

May 29th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 KB | None | 0 0
  1. from tkinter import *
  2. from time import *
  3.  
  4. class Game:
  5. def __init__(self):
  6. self.root = Tk()
  7. self.canvas = Canvas(self.root, width = 800, height = 500)
  8. self.canvas.pack()
  9. self.root.update()
  10. self.bg = PhotoImage(file = 'bg.gif')
  11. for x in range(8):
  12. for y in range(5):
  13. self.canvas.create_image(100 * x,100 * y,image=self.bg,anchor='nw')
  14. self.sprites = []
  15. self.prices = []
  16. self.score = 0
  17. self.lives = 3
  18. self.score_text = self.canvas.create_text(800 - 20,10,anchor = 'ne', font = 'Verdana 18', fill = 'darkblue', text = 'Счет: '+str(self.score))
  19. self.lives_text = self.canvas.create_text(800 - 20,40,anchor = 'ne', font = 'Verdana 18', fill = 'darkblue', text = 'Жизней: '+str(self.lives))
  20. def mainloop(self):
  21. while True:
  22. for sprite in self.sprites:
  23. sprite.move()
  24. self.root.update_idletasks()
  25. self.root.update()
  26. sleep(0.01)
  27.  
  28. class Sprite:
  29. def __init__(self, game):
  30. self.game = game
  31. self.coordinates = None
  32. def move(self):
  33. pass
  34. def coords(self):
  35. return self.coordinates
  36.  
  37. class Trophy(Sprite):
  38. def __init__(self, game, photo_image, x, y, width, height, type = 0):
  39. #0-coin, 1-cheese,2-meshok, 3-book
  40. Sprite.__init__(self, game)
  41. self.photo_image = photo_image
  42. self.type = type
  43. self.width = width
  44. self.height = height
  45. self.image = game.canvas.create_image(x,y,image=self.photo_image,anchor='nw')
  46. self.coordinates = Coords(x, y, x + width, y + height)
  47.  
  48. class Platform(Sprite):
  49. def __init__(self, game, photo_image, x, y, width, height, speed = 0):
  50. Sprite.__init__(self, game)
  51. self.photo_image = photo_image
  52. self.speed = speed
  53. self.count = 0
  54. self.width = width
  55. self.height = height
  56. self.image = game.canvas.create_image(x,y,image=self.photo_image,anchor='nw')
  57. self.coordinates = Coords(x, y, x + width, y + height)
  58. def move(self):
  59. if self.speed != 0:
  60. self.count += self.speed
  61. if self.count > 200:
  62. self.speed = - self.speed
  63. if self.count < 0:
  64. self.speed = - self.speed
  65. self.game.canvas.move(self.image, self.speed, 0)
  66. def coords(self):
  67. xy = self.game.canvas.coords(self.image)
  68. self.coordinates.x1 = xy[0]
  69. self.coordinates.y1 = xy[1]
  70. self.coordinates.x2 = xy[0] + self.width
  71. self.coordinates.y2 = xy[1] + self.height
  72. return self.coordinates
  73.  
  74. class Fox(Sprite):
  75. def __init__(self, game):
  76. Sprite.__init__(self, game)
  77.  
  78. #self.images = [PhotoImage(file = 'fox.gif')]
  79. self.images_left = [PhotoImage(file = 'f1_left.gif'),
  80. PhotoImage(file = 'f2_left.gif'),
  81. PhotoImage(file = 'f3_left.gif'),
  82. PhotoImage(file = 'f4_left.gif'),
  83. PhotoImage(file = 'f5_left.gif'),
  84. PhotoImage(file = 'f6_left.gif')]
  85. self.images_right = [PhotoImage(file = 'f1_right.gif'),
  86. PhotoImage(file = 'f2_right.gif'),
  87. PhotoImage(file = 'f3_right.gif'),
  88. PhotoImage(file = 'f4_right.gif'),
  89. PhotoImage(file = 'f5_right.gif'),
  90. PhotoImage(file = 'f6_right.gif')]
  91.  
  92. self.image = game.canvas.create_image(50, 50, image = self.images_left[0], anchor = 'nw')
  93. self.x = -2
  94. self.y = 0
  95. self.current_image = 0
  96. self.last_time = time()
  97. self.coordinates = Coords()
  98. self.jump_count = 0
  99. game.canvas.bind_all('<KeyPress-Left>',self.turn_left)
  100. game.canvas.bind_all('<KeyPress-Right>',self.turn_right)
  101. game.canvas.bind_all('<KeyPress-Up>',self.stop)
  102. game.canvas.bind_all('<space>',self.jump)
  103. def stop(self, event):
  104. self.x = 0
  105. def turn_left(self, event):
  106. if self.y == 0:
  107. self.x = -2
  108. def turn_right(self, event):
  109. if self.y == 0:
  110. self.x = 2
  111. def jump(self, event):
  112. if self.y == 0:
  113. self.y = -4
  114. self.jump_count = 0
  115. def animate(self):
  116. #на какой кадр? self.current_image
  117. if self.x != 0 and self.y == 0:
  118. if time() - self.last_time > 0.1:
  119. self.last_time = time()
  120. self.current_image = (self.current_image + 1) % 6
  121. if self.x < 0:
  122. if self.y == 0:
  123. self.game.canvas.itemconfig(self.image, image = self.images_left[self.current_image])
  124. elif self.x > 0:
  125. if self.y == 0:
  126. self.game.canvas.itemconfig(self.image, image = self.images_right[self.current_image])
  127.  
  128. def coords(self):
  129. xy = self.game.canvas.coords(self.image)
  130. self.coordinates.x1 = xy[0]
  131. self.coordinates.y1 = xy[1]
  132. self.coordinates.x2 = xy[0] + 80
  133. self.coordinates.y2 = xy[1] + 80
  134. return self.coordinates
  135.  
  136. def move(self):
  137. self.animate()
  138. if self.y < 0:
  139. self.jump_count += 1
  140. if self.jump_count > 20:
  141. self.y = 4
  142. if self.y > 0:
  143. self.jump_count -= 1
  144. co = self.coords()
  145. left = True
  146. right = True
  147. top = True
  148. bottom = True
  149. falling = True
  150. if self.y > 0 and co.y2 >= 500:
  151. self.y = 0
  152. bottom = False
  153. elif self.y < 0 and co.y1 <= 0:
  154. self.y = 0
  155. top = False
  156. if self.x > 0 and co.x2 >= 800:
  157. self.x = 0
  158. right = False
  159. elif self.x < 0 and co.x1 <= 0:
  160. self.x = 0
  161. left = False
  162. #перебираем призы
  163. for price in self.game.prices:
  164. price_co = price.coords()
  165. if int_top(co, price_co) or int_left(co, price_co) or int_right(co, price_co) or int_bottom(1, co, price_co):
  166. self.game.score += 10
  167. self.game.canvas.itemconfigure(self.game.score_text, text = 'Счет: '+str(self.game.score))
  168. self.game.canvas.delete(price.image)
  169. self.game.prices.remove(price)
  170.  
  171. #перебираем все из списка sprites
  172. for sprite in self.game.sprites:
  173. if sprite == self:
  174. continue
  175. sprite_co = sprite.coords()
  176.  
  177. if top and self.y < 0 and int_top(co, sprite_co):
  178. self.y = - self.y
  179. top = False
  180. if bottom and self.y > 0 and int_bottom(self.y, co, sprite_co):
  181. self.y = sprite_co.y1 - co.y2
  182. if self.y < 0:
  183. self.y = 0
  184. bottom = False
  185. top = False
  186. if bottom and falling and self.y == 0 and co.y2 < 500 and int_bottom(1, co, sprite_co):
  187. falling = False
  188. if left and self.x < 0 and int_left(co, sprite_co):
  189. self.x = 0
  190. left = False
  191. if right and self.x > 0 and int_right(co, sprite_co):
  192. self.x = 0
  193. right = False
  194. if falling and bottom and self.y == 0 and co.y2 < 500:
  195. self.y = 4
  196.  
  197.  
  198. self.game.canvas.move(self.image, self.x, self.y)
  199.  
  200. class Coords:
  201. def __init__(self, x1=0,y1=0,x2=0,y2=0):
  202. self.x1 = x1
  203. self.x2 = x2
  204. self.y1 = y1
  205. self.y2 = y2
  206.  
  207. def intersection_x(obj1, obj2):
  208. A = max(obj1.x1, obj2.x1)
  209. B = min(obj1.x2, obj2.x2)
  210. return B > A
  211.  
  212. def intersection_y(obj1, obj2):
  213. A = max(obj1.y1, obj2.y1)
  214. B = min(obj1.y2, obj2.y2)
  215. return B > A
  216.  
  217. def int_left(obj1, obj2):
  218. return intersection_y(obj1, obj2) and obj2.x1 <= obj1.x1 and obj1.x1 <= obj2.x2
  219.  
  220. def int_right(obj1, obj2):
  221. return intersection_y(obj1, obj2) and obj2.x1 <= obj1.x2 and obj1.x2 <= obj2.x2
  222.  
  223. def int_top(obj1, obj2):
  224. return intersection_x(obj1, obj2) and obj2.y1 <= obj1.y1 and obj1.y1 <= obj2.y2
  225.  
  226. def int_bottom(d, obj1, obj2):
  227. return intersection_x(obj1, obj2) and obj2.y1 <= obj1.y2 + d and obj1.y2 + d <= obj2.y2
  228.  
  229.  
  230. g = Game()
  231. platforma1 = Platform(g, PhotoImage(file='pl1.gif'), 20, 400, 100, 10)
  232. g.sprites.append(platforma1)
  233. platforma2 = Platform(g, PhotoImage(file='pl1.gif'), 500, 350, 100, 10, 1)
  234. g.sprites.append(platforma2)
  235. platforma3 = Platform(g, PhotoImage(file='pl2.gif'), 290, 300, 60, 10)
  236. g.sprites.append(platforma3)
  237. platforma4 = Platform(g, PhotoImage(file='pl2.gif'), 350, 450, 60, 10)
  238. g.sprites.append(platforma4)
  239.  
  240. price1 = Trophy(g, PhotoImage(file='coin.gif'), 50, 450, 50, 50)
  241. g.prices.append(price1)
  242.  
  243. f = Fox(g)
  244. g.sprites.append(f)
  245. g.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement