Advertisement
Guest User

Untitled

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