Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import time
- 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()
- time.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.image = game.canvas.create_image(200, 400, image = self.images[0], anchor = 'nw')
- self.x = 2
- self.y = 0
- 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 move(self):
- self.game.canvas.move(self.image, self.x, self.y)
- g = Game()
- platforma1 = Platform(g, PhotoImage(file='p.gif'), 20, 400)
- g.sprites.append(platforma1)
- platforma2 = Platform(g, PhotoImage(file='p.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