Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from time import *
- class Game:
- def __init__(self):
- self.root = Tk()
- self.canvas = Canvas(self.root, width = 800, height = 500)
- self.canvas.pack()
- self.root.update()
- self.bg = PhotoImage(file = 'bg.gif')
- for x in range(8):
- for y in range(5):
- self.canvas.create_image(100 * x,100 * y,image=self.bg,anchor='nw')
- self.sprites = []
- def mainloop(self):
- while True:
- for sprite in self.sprites:
- sprite.move()
- self.root.update_idletasks()
- self.root.update()
- sleep(0.01)
- class Sprite:
- def __init__(self, game):
- self.game = game
- def move(self):
- pass
- class Platform(Sprite):
- def __init__(self, game, photo_image, x, y):
- Sprite.__init__(self, game)
- self.photo_image = photo_image
- self.image = game.canvas.create_image(x,y,image=self.photo_image,anchor='nw')
- class Fox(Sprite):
- def __init__(self, game):
- Sprite.__init__(self, game)
- #self.images = [PhotoImage(file = 'fox.gif')]
- self.images_left = [PhotoImage(file = 'f1_left.gif'),
- PhotoImage(file = 'f2_left.gif'),
- PhotoImage(file = 'f3_left.gif'),
- PhotoImage(file = 'f4_left.gif'),
- PhotoImage(file = 'f5_left.gif'),
- PhotoImage(file = 'f6_left.gif')]
- self.images_right = [PhotoImage(file = 'f1_right.gif'),
- PhotoImage(file = 'f2_right.gif'),
- PhotoImage(file = 'f3_right.gif'),
- PhotoImage(file = 'f4_right.gif'),
- PhotoImage(file = 'f5_right.gif'),
- PhotoImage(file = 'f6_right.gif')]
- self.image = game.canvas.create_image(200, 400, image = self.images_left[0], anchor = 'nw')
- self.x = -2
- self.y = 0
- self.current_image = 0
- self.last_time = time()
- game.canvas.bind_all('<KeyPress-Left>',self.turn_left)
- game.canvas.bind_all('<KeyPress-Right>',self.turn_right)
- def turn_left(self, event):
- self.x = -2
- def turn_right(self, event):
- self.x = 2
- def animate(self):
- #на какой кадр? self.current_image
- if self.x != 0 and self.y == 0:
- if time() - self.last_time > 0.1:
- self.last_time = time()
- self.current_image = (self.current_image + 1) % 6
- if self.x < 0:
- if self.y == 0:
- self.game.canvas.itemconfig(self.image, image = self.images_left[self.current_image])
- elif self.x > 0:
- if self.y == 0:
- self.game.canvas.itemconfig(self.image, image = self.images_right[self.current_image])
- def move(self):
- self.animate()
- self.game.canvas.move(self.image, self.x, self.y)
- class Coords:
- def __init__(self, x1=0,y1=0,x2=0,y2=0):
- self.x1 = x1
- self.x2 = x2
- self.y1 = y1
- self.y2 = y2
- def intersection_x(obj1, obj2):
- A = max(obj1.x1, obj2.x1)
- B = min(obj1.x2, obj2.x2)
- return B > A
- def intersection_y(obj1, obj2):
- A = max(obj1.y1, obj2.y1)
- B = min(obj1.y2, obj2.y2)
- return B > A
- #P1 = Coords(0, 0, 100, 100)
- #P2 = Coords(50, 50, 100, 100)
- #print(intersection_x(P1,P2))
- g = Game()
- platforma1 = Platform(g, PhotoImage(file='pl1.gif'), 20, 400)
- g.sprites.append(platforma1)
- platforma2 = Platform(g, PhotoImage(file='pl1.gif'), 50, 300)
- g.sprites.append(platforma2)
- f = Fox(g)
- g.sprites.append(f)
- g.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement