Advertisement
gorskaja2019

Untitled

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