gorskaja2019

Untitled

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