SHARE
TWEET

Untitled

gorskaja2019 May 22nd, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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(200, 400, 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.        
  63.         game.canvas.bind_all('<KeyPress-Left>',self.turn_left)
  64.         game.canvas.bind_all('<KeyPress-Right>',self.turn_right)
  65.     def turn_left(self, event):
  66.         self.x = -2
  67.     def turn_right(self, event):
  68.         self.x = 2
  69.  
  70.     def animate(self):
  71.         #на какой кадр? self.current_image
  72.         if self.x != 0 and self.y == 0:
  73.             if time() - self.last_time > 0.1:
  74.                 self.last_time = time()
  75.                 self.current_image = (self.current_image + 1) % 6
  76.         if self.x < 0:
  77.             if self.y == 0:
  78.                 self.game.canvas.itemconfig(self.image, image = self.images_left[self.current_image])
  79.         elif self.x > 0:
  80.             if self.y == 0:
  81.                 self.game.canvas.itemconfig(self.image, image = self.images_right[self.current_image])
  82.  
  83.     def coords(self):
  84.         xy = self.game.canvas.coords(self.image)
  85.         self.coordinates.x1 = xy[0]
  86.         self.coordinates.y1 = xy[1]
  87.         self.coordinates.x2 = xy[0] + 80
  88.         self.coordinates.y2 = xy[1] + 80
  89.         return self.coordinates
  90.        
  91.     def move(self):
  92.         self.animate()
  93.         self.game.canvas.move(self.image, self.x, self.y)
  94.  
  95. class Coords:
  96.     def __init__(self, x1=0,y1=0,x2=0,y2=0):
  97.         self.x1 = x1
  98.         self.x2 = x2
  99.         self.y1 = y1
  100.         self.y2 = y2
  101.  
  102. def intersection_x(obj1, obj2):
  103.     A = max(obj1.x1, obj2.x1)
  104.     B = min(obj1.x2, obj2.x2)
  105.     return B > A
  106.  
  107. def intersection_y(obj1, obj2):
  108.     A = max(obj1.y1, obj2.y1)
  109.     B = min(obj1.y2, obj2.y2)
  110.     return B > A
  111.  
  112. def int_left(obj1, obj2):
  113.     return intersection_y(obj1, obj2) and obj2.x1 <= obj1.x1 and obj1.x1 <= obj2.x2
  114.  
  115. def int_right(obj1, obj2):
  116.     return intersection_y(obj1, obj2) and obj2.x1 <= obj1.x2 and obj1.x2 <= obj2.x2
  117.  
  118. def int_top(obj1, obj2):
  119.     return intersection_x(obj1, obj2) and obj2.y1 <= obj1.y1 and obj1.y1 <= obj2.y2
  120.  
  121. def int_bottom(d, obj1, obj2):
  122.     return intersection_x(obj1, obj2) and obj2.y1 <= obj1.y2 + d and obj1.y2 + d <= obj2.y2
  123.  
  124.  
  125. g = Game()
  126. platforma1 = Platform(g, PhotoImage(file='pl1.gif'), 20, 400, 100, 10)
  127. g.sprites.append(platforma1)
  128. platforma2 = Platform(g, PhotoImage(file='pl1.gif'), 50, 300, 100, 10)
  129. g.sprites.append(platforma2)
  130. f = Fox(g)
  131. g.sprites.append(f)
  132. g.mainloop()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top