Advertisement
gorskaja2019

Untitled

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