Advertisement
Guest User

piece of TRASH

a guest
May 25th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.84 KB | None | 0 0
  1. from tkinter import *
  2. import random
  3. import time
  4.  
  5. class Game:
  6. def __init__(self):
  7. self.tk = Tk()
  8. self.tk.title("Mr Stick Man Runs For the Exit")
  9. self.tk.resizable(0, 0)
  10. self.tk.wm_attributes("-topmost", 1)
  11. self.canvas = Canvas(self.tk, width=500, height=500, \
  12. highlightthickness=0)
  13. self.canvas.pack()
  14. self.tk.update()
  15. self.canvas_height = 500
  16. self.canvas_width = 500
  17. self.bg = PhotoImage(file="background.gif")
  18. w = self.bg.width()
  19. h = self.bg.height()
  20. for x in range(0, 5):
  21. for y in range(0, 5):
  22. self.canvas.create_image(x * w, y * h, \
  23. image=self.bg, anchor='nw')
  24. self.sprites = []
  25. self.running = True
  26. self.game_over_text = self.canvas.create_text(250, 250, \
  27. text='YOU WIN!', state='hidden')
  28.  
  29.  
  30.  
  31. def mainloop(self):
  32. while 1:
  33. if self.running:
  34. for sprite in self.sprites:
  35. sprite.move()
  36. else:
  37. time.sleep(1)
  38. self.canvas.itemconfig(self.game_over_text, \
  39. state='normal')
  40. self.tk.update_idletasks()
  41. self.tk.update()
  42. time.sleep(0.01)
  43.  
  44.  
  45. class Coords:
  46. def __init__(self, x1=0, y1=0, x2=0, y2=0):
  47. self.x1 = x1
  48. self.y1 = y1
  49. self.x2 = x2
  50. self.y2 = y2
  51.  
  52.  
  53. def within_x(co1, co2):
  54. if (co1.x1 > co2.x1 and co1.x1 < co2.x2) \
  55. or (co1.x2 > co2.x1 and co1.x2 < co2.x2) \
  56. or (co2.x1 > co1.x1 and co2.x1 < co1.x2) \
  57. or (co2.x2 > co1.x1 and co2.x2 < co1.x2):
  58. return True
  59. else:
  60. return False
  61.  
  62. def within_y(co1, co2):
  63. if (co1.x1 > co2.x1 and co1.x1 < co2.x2) \
  64. or (co1.x2 > co2.x1 and co1.x2 < co2.x2) \
  65. or (co2.x1 > co1.x1 and co2.x1 < co1.x2) \
  66. or (co2.x2 > co1.x1 and co2.x2 < co1.x2):
  67. return True
  68. else:
  69. return False
  70.  
  71.  
  72. def collided_left(co1, co2):
  73. if within_y(co1, co2):
  74. if co1.x1 >= co2.x2 and co1.x1 <= co2.x1:
  75. return True
  76. return False
  77.  
  78. def collided_right(co1, co2):
  79. if within_x(co1, co2):
  80. if co1.y1 <= co2.y2 and co1.y1 >= co2.y1:
  81. return True
  82. return False
  83.  
  84. def collided_top(co1, co2):
  85. if within_x(co1, co2):
  86. if co1.y1 <= co2.y2 and co1.y1 >= co2.y1:
  87. return True
  88. return False
  89.  
  90. def collided_bottom(y, co1, co2):
  91. if within_x(co1, co2):
  92. y_calc = co1.y2 + y
  93. if y_calc >= co2.y1 and y_calc <= co2.y2:
  94. return True
  95. return False
  96.  
  97. class Sprite:
  98. def __init__(self, game):
  99. self.game = game
  100. self.endgame = False
  101. self.coordinates = None
  102. def move(self):
  103. pass
  104. def coords(self):
  105. return self.coordinates
  106.  
  107.  
  108. class PlatformSprite(Sprite):
  109. def __init__(self, game, photo_image, x, y, width, height):
  110. Sprite.__init__(self, game)
  111. self.photo_image = photo_image
  112. self.image = game.canvas.create_image(x, y, \
  113. image=self.photo_image, anchor='nw')
  114. self.coordinates = Coords(x, y, x + width, y + height)
  115.  
  116. class MovingPlatformSprite(PlatformSprite):
  117. def __init__(self, game, photo_image, x, y, width, height):
  118. PlatformSprite.__init__(self, game, photo_image, x, y, \
  119. width, height)
  120. self.x = 2
  121. self.counter = 0
  122. self.last_time = time.time()
  123. self.width = width
  124. self.height = height
  125.  
  126. def coords(self):
  127. xy = self.game.canvas.coords(self.image)
  128. self.coordinates.x1 = xy[0]
  129. self.coordinates.y1 = xy[1]
  130. self.coordinates.x2 = xy[0] + self.width
  131. self.coordinates.y2 = xy[1] + self.height
  132. return self.coordinates
  133.  
  134. def move(self):
  135. if time.time() - self.last_time > 0.03:
  136. self.last_time = time.time()
  137. self.game.canvas.move(self.image, self.x, 0)
  138. if self.counter > 20:
  139. self.x *= -1
  140. self.counter = 0
  141.  
  142. class DoorSprite(Sprite):
  143. def __init__(self, game, x, y, width, height):
  144. Sprite.__init__(self, game)
  145. self.closed_door = PhotoImage(file="closed_door.gif")
  146. self.open_door = PhotoImage(file="open_door.gif")
  147. self.image = game.canvas.create_image(x, y, image=self.closed_door, anchor='nw')
  148. self.coordinates = Coords(x, y, x + (width / 2), y + height)
  149. self.endgame = True
  150.  
  151. def opendoor(self):
  152. self.game.canvas.itemconfig(self.image, image=self.open_door)
  153. self.game.tk.update_idletasks()
  154.  
  155. def closedoor(self):
  156. self.game.canvas.itemconfig(self.image, image=self.closed_door)
  157. self.game.tk.update_idletasks()
  158.  
  159. class StickFigureSprite(Sprite):
  160. def __init__(self, game):
  161. Sprite.__init__(self, game)
  162. self.images_left = [
  163. PhotoImage(file="figure-L1.gif"),
  164. PhotoImage(file="figure-L2.gif"),
  165. PhotoImage(file="figure-L3.gif")
  166. ]
  167. self.images_right = [
  168. PhotoImage(file="figure-R1.gif"),
  169. PhotoImage(file="figure-R2.gif"),
  170. PhotoImage(file="figure-R3.gif")
  171. ]
  172. self.image = game.canvas.create_image(200, 470, image=self.images_left[0], anchor='nw')
  173. self.x = -2
  174. self.y = 0
  175. self.current_image = 0
  176. self.current_image_add = 1
  177. self.jump_count = 0
  178. self.last_time = time.time()
  179. self.coordinates = Coords()
  180. game.canvas.bind_all('KeyPress-Left>', self.turn_left)
  181. game.canvas.bind_all('Keypress-Right>', self.turn_right)
  182. game.canvas.bind_all('<space>', self.jump)
  183.  
  184.  
  185. def turn_left(self, evt):
  186. if self.y == 0:
  187. self.x = -2
  188.  
  189. def turn_right(self, evt):
  190. if self.y == 0:
  191. self.x = 2
  192.  
  193. def jump(self, evt):
  194. if self.y == 0:
  195. self.y = -4
  196.  
  197. def animate(self):
  198. if self.x !=0 and self.y == 0:
  199. if time.time() - self.last_time > 0.1:
  200. self.last_time = time.time()
  201. self.current_image += self.current_image_add
  202. if self.current_image >= 2:
  203. self.current_image_add = -1
  204. if self.current_image <= 0:
  205. self.current_image_add = 1
  206. if self.x < 0:
  207. if self.y !=0:
  208. self.game.canvas.itemconfig(self.image, \
  209. image=self.images_left[2])
  210.  
  211. else:
  212. self.game.canvas.itemconfig(self.image, \
  213. image=self.images_left[self.current_image])
  214.  
  215. elif self.x > 0:
  216. if self.y !=0:
  217. self.game.canvas.coords(self.image, \
  218. image=self.images_right[self.current_image])
  219.  
  220. else:
  221. self.game.canvas.itemconfig(self,image, \
  222. image=self.images_right[self.current_image])
  223.  
  224. def coords(self):
  225. xy = self.game.canvas.coords(self.image)
  226. self.coordinates.x1 = xy[0]
  227. self.coordinates.y1 = xy[1]
  228. self.coordinates.x2 = xy[0] + 27
  229. self.coordinates.y2 = xy[1] + 30
  230. return self.coordinates
  231.  
  232. def move(self):
  233. self.animate()
  234. if self.y < 0:
  235. self.jump_count += 1
  236. if self.jump_count > 20:
  237. self.y = 4
  238. if self.y > 0:
  239. self.jump_count -= 1
  240. co = self.coords()
  241. left = True
  242. right = True
  243. top = True
  244. bottom = True
  245. falling = True
  246. if self.y > 0 and co.y2 >= self.game.canvas_height:
  247. self.y = 0
  248. bottom = False
  249. elif self.y < 0 and co.y1 <=0:
  250. self.y = 0
  251. top = False
  252. if self.x > 0 and co.x2 >= self.game.canvas_width:
  253. self.x = 0
  254. right = False
  255. elif self.x < 0 and co.x1 <= 0:
  256. self.x = 0
  257. left = False
  258. for sprite in self.game.sprites:
  259. if sprite == self:
  260. continue
  261. sprite_co = sprite.coords()
  262. if top and self.y < 0 and collided_top(co, sprite_co):
  263. self.y = -self.y
  264. top = False
  265. if bottom and self.y > 0 and collided_bottom(self.y, co, sprite_co):
  266. self.y = sprite_co.y1 - co.y2
  267. if self.y < 0:
  268. self.y = 0
  269. bottom = False
  270. top = False
  271. if bottom and falling and self.y == 0 \
  272. and co.y2 < self.game.canves_height \
  273. and collided_bottom(1, co, sprite_co):
  274. falling = False
  275. if left and self.x < 0 and collided_left(co, sprite_co):
  276. self.x = 0
  277. left = False
  278. if sprite.endgame:
  279. self.game.running = False
  280. if falling and bottom and self.y == 0 \
  281. and co.y2 < self.game.canvas_height:
  282. self.y = 4
  283. self.came.canvas.move(self.image, self.x, self.y)
  284.  
  285.  
  286. class DoorSprite(Sprite):
  287. def __init__(self, game, photo_image, x, y, width, height):
  288. Sprite.__init__(self, game)
  289. self.photo_image = photo_image
  290. self.image = game.canvas.create_image(x, y, \
  291. image=self.photo_image, anchor='nw')
  292. self.coordinates = Coords(x, y, x + (width / 2), y + height)
  293. self.endgame = True
  294.  
  295.  
  296. g = Game()
  297. platform1 = PlatformSprite(g, PhotoImage(file="platform1.gif"), 0, 480, 100, 10)
  298. platform2 = PlatformSprite(g, PhotoImage(file="platform1.gif"), 150, 440, 100, 10)
  299. platform3 = PlatformSprite(g, PhotoImage(file="platform1.gif"), 300, 400, 100, 10)
  300. platform4 = PlatformSprite(g, PhotoImage(file="platform1.gif"), 300, 160, 100, 10)
  301. platform5 = PlatformSprite(g, PhotoImage(file="platform2.gif"), 175, 350, 66, 10)
  302. platform6 = PlatformSprite(g, PhotoImage(file="platform2.gif"), 50, 300, 66, 10)
  303. platform7 = PlatformSprite(g, PhotoImage(file="platform2.gif"), 170, 120, 66, 10)
  304. platform8 = PlatformSprite(g, PhotoImage(file="platform2.gif"), 45, 60, 66, 10)
  305. platform9 = PlatformSprite(g, PhotoImage(file="platform3.gif"), 170, 250, 32, 10)
  306. platform10 = PlatformSprite(g, PhotoImage(file="platform3.gif"), 230, 200, 32, 10)
  307. g.sprites.append(platform1)
  308. g.sprites.append(platform2)
  309. g.sprites.append(platform3)
  310. g.sprites.append(platform4)
  311. g.sprites.append(platform5)
  312. g.sprites.append(platform6)
  313. g.sprites.append(platform7)
  314. g.sprites.append(platform8)
  315. g.sprites.append(platform9)
  316. g.sprites.append(platform10)
  317. door = DoorSprite(g, PhotoImage(file="closed_door.gif"), 45, 30, 40, 35)
  318. g.sprites.append(door)
  319. sf = StickFigureSprite(g)
  320. g.sprites.append(sf)
  321. g.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement