Advertisement
gorskaja2019

Untitled

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