[text] Mr. Stickman Reaces to the Exit

Viewer

copyrawdownloadembedprintName: Mr. Stickman Reaces to the Exit
  1. from tkinter import *
  2. import random
  3. import time
  4. class Game:
  5.     def __init__(self):
  6.         self.tk = Tk()
  7.         self.tk.title("Mr. Stick Man Races for the Exit")
  8.         self.tk.resizable(0, 0)
  9.         self.tk.wm_attributes("-topmost", 1)
  10.         self.canvas = Canvas(self.tk, width=500, height=500, \
  11.                 highlightthickness=0)
  12.         self.canvas.pack()
  13.         self.tk.update()
  14.         self.canvas_height = 500
  15.         self.canvas_width = 500
  16.         self.bg = PhotoImage(file="background.gif")
  17.         w = self.bg.width()
  18.         h = self.bg.height()
  19.         for x in range(0, 5):
  20.             for y in range(0, 5):
  21.                 self.canvas.create_image(x * w, y * h, \
  22.                         image=self.bg, anchor='nw')
  23.         self.sprites = []
  24.         self.running = True
  25.     
  26.     def mainloop(self):
  27.         while 1:
  28.             if self.running == True:
  29.                 for sprite in self.sprites:
  30.                     sprite.move()
  31.             self.tk.update_idletasks()
  32.             self.tk.update()
  33.             time.sleep(0.01)
  34.  
  35. class Coords:
  36.     def __init__(self, x1=0, y1=0, x2=0, y2=0):
  37.         self.x1 = x1
  38.         self.y1 = y1
  39.         self.x2 = x2
  40.         self.y2 = y2
  41.         
  42. def within_x(co1, co2):
  43.     if (co1.x1 > co2.x1 and co1.x1 < co2.x2) \
  44.             or (co1.x2 > co2.x1 and co1.x2 < co2.x2) \
  45.             or (co2.x1 > co1.x1 and co2.x1 < co1.x2) \
  46.             or (co2.x2 > co1.x1 and co2.x2 < co1.x1):
  47.         return True
  48.     else:
  49.         return False
  50.             
  51. def within_y(co1, co2):
  52.     if (co1.y1 > co2.y1 and co1.y1 < co2.y2) \
  53.             or (co1.y2 > co2.y1 and co1.y2 < co2.y2) \
  54.             or (co2.y1 > co1.y1 and co2.y1 < co1.y2) \
  55.             or (co2.y2 > co1.y1 and co2.y2 < co1.y1):
  56.         return True
  57.     else:
  58.         return False
  59.             
  60. def collided_left(co1, co2):
  61.     if within_y(co1, co2):
  62.         if co1.x1 <= co2.x2 and co1.x1 >= co2.x1:
  63.             return True
  64.     return False
  65.         
  66. def collided_right(co1, co2):
  67.     if within_y(co1, co2):
  68.         if co1.x2 >= co2.x1 and co1.x2 <= co2.x2:
  69.             return True
  70.     return False
  71.         
  72. def collided_top(co1, co2):
  73.     if within_x(co1, co2):
  74.         if co1.y1 <= co2.y2 and co1.y1 >= co2.y1:
  75.             return True
  76.     return False
  77.         
  78. def collided_bottom(y, co1, co2):
  79.     if within_x(co1, co2):
  80.         y_calc = co1.y2 + y
  81.         if y_calc >= co2.y1 and y_calc <= co2.y2:
  82.             return True
  83.     return False
  84.         
  85. class Sprite:
  86.     def __init__(self, game):
  87.         self.game = game
  88.         self.endgame = False
  89.         self.coordinates = None
  90.     def move(self):
  91.         pass
  92.     def coords(self):
  93.         return self.coordinates
  94.         
  95. class PlatformSprite(Sprite):
  96.     def __init__(self, game, photo_image, x, y, width, height):
  97.     Sprite.__init__(self, game)
  98.     self.photo_image = photo_image
  99.     self.image = game.canvas.create_image(x, y, \
  100.             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, \
  117.                 image=self.images_left[0], anchor='nw')
  118.         self.x = -2
  119.         self.y = 0
  120.         self.current_image = 0
  121.         self.current_image_add = 1
  122.         self.jump_count = 0
  123.         self.last_time = time.time()
  124.         self.coordinates = Coords()
  125.         game.canvas.bind_all('<KeyPress-Left>', self.turn_left)
  126.         game.canvas.bind_all('<KeyPress-Right>', self.turn_right)
  127.         game.canvas.bind_all('<space>', self.jump)
  128.         
  129.     def turn_left(self, evt):
  130.         if self.y == 0:
  131.             self.x = -2
  132.             
  133.     def turn_right(self, evt):
  134.         if self.y == 0:
  135.             self.x = 2
  136.             
  137.     def jump(self, evt):
  138.         if self.y == 0:
  139.             self.y = -4
  140.             self.jump_count = 0
  141.             
  142.     def animate(self):
  143.         if self.x != 0 and self.y == 0:
  144.             if time.time() - self.last_time > 0.1:
  145.                 self.last_time= time.time()
  146.                 self.current_image += self.current_image_add
  147.                 if self.current_image >= 2:
  148.                     self.current_image_add = -1
  149.                 if self.current_image <= 0:
  150.                     self.current_image_add = 1
  151.         if self.x < 0:
  152.             if self.y != 0:
  153.                 self.game.canvas.itemconfig(self.image, \
  154.                         image=self.images_left[2])
  155.         else:
  156.                 self.game.canvas.itemconfig(self.image, \
  157.                         image=self.images_left[self.current_image])
  158.         elif self.x > 0:
  159.             if self.y != 0:
  160.                 self.game.canvas.itemconfig(self.image, \
  161.                         image=self.images_right[2])
  162.             else:
  163.                 self.game.canvas.itemconfig(self.image, \
  164.                         image=self.images_right[self.current_image])
  165.                         
  166.     def coords(self):
  167.         xy = self.game.canvas.coords(self.image)
  168.         self.coordinates.x1 = xy[0]
  169.         self.coordinates.y1 = xy[1]
  170.         self.coordinates.x2 = xy[0] + 27
  171.         self.coordinates.y2 = xy[1] + 30
  172.         return self.coordinates
  173.         
  174.     def move(self):
  175.         self.animate()
  176.         if self.y < 0:
  177.             self.jump_count += 1
  178.             if self.jump_count > 20:
  179.                 self.y = 4
  180.         if self.y > 0:
  181.             self.jump_count -= 1
  182.         co = self.coords()
  183.         left = True
  184.         right = True
  185.         top = True
  186.         bottom = True
  187.         falling = True
  188.         if self.y > 0 and co.y2 >= self.game.canvas_height:
  189.             self.y = 0
  190.             bottom = False
  191.         elif self.y < 0 and co.y1 <= 0:
  192.             self.y = 0
  193.             top = False
  194.         if self.x > 0 and co.x2 >= self.game.canvas_width:
  195.             self.x = 0
  196.             right = False
  197.         elif self.x < 0 and co.x1 <= 0:
  198.             self.x = 0
  199.             left = False
  200.         for sprite in self.game.sprites:
  201.             if sprite == self:
  202.                 continue
  203.             sprite_co = sprite.coords()
  204.             if top and self.y < 0 and collided_top(co, sprite_co):
  205.                 self.y = -self.y
  206.                 top = False
  207.             if bottom and self.y > 0 and collided_bottom(self.y, \
  208.                     co, sprite_co):
  209.                 self.y = sprite_co.y1 - co.y2
  210.                 if self.y < 0:
  211.                     self.y = 0
  212.                 bottom = False
  213.                 top = False
  214.             if bottom and falling and self.y == 0 \
  215.                     and co.y2 < self.game.canvas_height \
  216.                     and collided_bottom(1, co, sprite_co):
  217.                 falling = False
  218.             if left and self.x < 0 and collided_left(co, sprite_co):
  219.                 self.x = 0
  220.                 left = False
  221.                 if sprite.endgame:
  222.                     self.game.running = False
  223.             if right and self.x > 0 and collided_right(co, sprite_co):
  224.                 self.x = 0
  225.                 right = False
  226.                 if sprite.endgame:
  227.                     self.game.running = False
  228.         if falling and bottom and self.y == 0 \
  229.                     and co.y2 < self.game.canvas_height:
  230.             self.y = 4
  231.         self.game.canvas.move(self.image, self.x, self.y)
  232.         
  233. class DoorSprite(Sprite):
  234.     def __init__(self, game, photo_image, x, y, width, height):
  235.         Sprite.__init__(self, game)
  236.         self.photo_image = photo_image
  237.         self.image = game.canvas.create_image(x, y, \
  238.                 image=self.photo_image, anchor='nw')
  239.         self.coordinates = Coords(x, y, x + (width / 2), y + height)
  240.         self.endgame = True
  241.  
  242. g = Game()
  243. platform1 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
  244.     0, 480, 100, 10)
  245. platform2 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
  246.     150, 440, 100, 10)
  247. platform3 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
  248.     300, 400, 100, 10)
  249. platform4 = PlatformSprite(g, PhotoImage(file="platform1.gif"), \
  250.     300, 160, 100, 10)
  251. platform5 = PlatformSprite(g, PhotoImage(file="platform2.gif"), \
  252.     175, 350, 66, 10)
  253. platform6 = PlatformSprite(g, PhotoImage(file="platform2.gif"), \
  254.     50, 300, 66, 10)
  255. platform7 = PlatformSprite(g, PhotoImage(file="platform2.gif"), \
  256.     170, 120, 66, 10)
  257. platform8 = PlatformSprite(g, PhotoImage(file="platform2.gif"), \
  258.     45, 60, 66, 10)
  259. platform9 = PlatformSprite(g, PhotoImage(file="platform3.gif"), \
  260.     170, 250, 32, 10)
  261. platform10 = PlatformSprite(g, PhotoImage(file="platform3.gif"), \
  262.     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()

Editor

You can edit this paste and save as new:


File Description
  • Mr. Stickman Reaces to the Exit
  • Paste Code
  • 06 May-2021
  • 9.41 Kb
You can Share it: