gorskaja2019

Untitled

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