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#
- self.endgame = False
- self.coordinates = None
- def move(self):#
- pass#
- def coords(self):
- return self.coordinates
- class Platform(Sprite):
- def __init__(self, game, photo_image, x, y, width, height,speed):
- Sprite.__init__(self, game)
- self.photo_image = photo_image
- self.speed = speed
- self.count = 0
- self.run = 0
- if self.speed != 0:
- self.run = 1
- self.image = game.canvas.create_image(x,y,image=self.photo_image,anchor='nw')
- self.coordinates = Coords(x, y, x + width, y + height)
- def move(self):
- if self.run == 1:
- self.count += self.speed
- if self.count > 100:
- self.speed =-1
- if self.count < 0:
- self.speed = 1
- self.game.canvas.move(self.image, self.speed, 0)
- 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(0, 0, image = self.images_left[0], anchor = 'nw')
- self.x = -2
- self.y = 0
- self.current_image = 0
- self.current_image_add = 1
- self.jump_count = 0
- self.last_time = time.time()
- self.coordinates = Coords()
- game.canvas.bind_all('<KeyPress-Left>',self.turn_left)
- game.canvas.bind_all('<KeyPress-Right>',self.turn_right)
- game.canvas.bind_all('<space>',self.jump)
- def turn_left(self, event):
- if self.y == 0:
- self.x = -2
- def turn_right(self, event):
- if self.y == 0:
- self.x = 2
- def jump(self, event):
- if self.y == 0:
- self.y = -4
- self.jump_count = 0
- def animate(self):
- if self.x != 0 and self.y == 0:
- if time.time() - self.last_time > 0.1:
- self.last_time = time.time()
- self.current_image = (self.current_image + self.current_image_add) % 6
- if self.x < 0:
- if self.y == 0:
- self.game.canvas.itemconfig(self.image,image=self.images_left[self.current_image])
- else:
- self.game.canvas.itemconfig(self.image,image=self.images_left[5])
- elif self.x > 0:
- if self.y == 0:
- self.game.canvas.itemconfig(self.image,image=self.images_right[self.current_image])
- else:
- self.game.canvas.itemconfig(self.image,image=self.images_right[5])
- def coords(self):
- xy = self.game.canvas.coords(self.image)
- self.coordinates.x1 = xy[0]
- self.coordinates.y1 = xy[1]
- self.coordinates.x2 = xy[0] + 80
- self.coordinates.y2 = xy[1] + 80
- return self.coordinates
- def move(self):
- #self.game.canvas.move(self.image, self.x, self.y)
- self.animate()
- #self.game.canvas.move(self.image, self.x, self.y)
- if self.y < 0:
- self.jump_count += 1
- if self.jump_count > 20:
- self.y = 4
- if self.y > 0:
- self.jump_count -= 1
- co = self.coords()
- left = True
- right = True
- top = True
- bottom = True
- falling = True
- if self.y > 0 and co.y2 >= 500:
- self.y = 0
- bottom = False
- elif self.y < 0 and co.y1 <= 0:
- self.y = 0
- top = False
- if self.x > 0 and co.x2 >= 800:
- self.x = 0
- right = False
- elif self.x < 0 and co.x1 <= 0:
- self.x = 0
- left = False
- for sprite in self.game.sprites:
- if sprite == self:
- continue
- sprite_co = sprite.coords()
- if top and self.y < 0 and int_top(co, sprite_co):
- self.y = - self.y
- top = False
- if bottom and self.y > 0 and int_bottom(self.y,co, sprite_co):
- self.y = sprite_co.y1 - co.y2
- if self.y < 0:
- self.y = 0
- bottom = False
- top = False
- if bottom and falling and self.y == 0 and co.y2 < 500 and int_bottom(1, co, sprite_co):
- falling = False
- if left and self.x < 0 and int_left(co, sprite_co):
- self.x = 0
- left = False
- if right and self.x > 0 and int_right(co, sprite_co):
- self.x = 0
- right = False
- if falling and bottom and self.y == 0 and co.y2 < 500:
- self.y = 4
- 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.y1 = y1
- self.x2 = x2
- 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
- def int_left(obj1, obj2):
- return intersection_y(obj1, obj2) and obj2.x1 <= obj1.x1 and obj1.x1 <= obj2.x2
- def int_right(obj1, obj2):
- return intersection_y(obj1, obj2) and obj2.x1 <= obj1.x2 and obj1.x2 <= obj2.x2
- def int_top(obj1, obj2):
- return intersection_x(obj1, obj2) and obj2.y1 <= obj1.y1 and obj1.y1 <= obj2.y2
- def int_bottom(d, obj1, obj2):
- return intersection_x(obj1, obj2) and obj2.y1 <= obj1.y2 + d and obj1.y2 + d <= obj2.y2
- g = Game()
- platforma1 = Platform(g, PhotoImage(file='p1.gif'), 10, 440, 100, 10,0)
- g.sprites.append(platforma1)
- platforma2 = Platform(g, PhotoImage(file='pl1.gif'), 150, 410, 100, 10,0)
- g.sprites.append(platforma2)
- platforma3 = Platform(g, PhotoImage(file='pl2.gif'), 300, 360, 60, 10,0)
- g.sprites.append(platforma3)
- platforma4 = Platform(g, PhotoImage(file='pl1.gif'), 250, 100, 100, 10,0)
- g.sprites.append(platforma4)
- platforma4 = Platform(g, PhotoImage(file='pl2.gif'), 670, 460, 60, 10,0)
- g.sprites.append(platforma4)
- platforma5 = Platform(g, PhotoImage(file='pl1.gif'), 500, 50, 100, 10,0)
- g.sprites.append(platforma5)
- platforma6 = Platform(g, PhotoImage(file='pl1.gif'), 300, 400, 100, 10,2)
- g.sprites.append(platforma6)
- f = Fox(g)
- g.sprites.append(f)
- g.mainloop()
Add Comment
Please, Sign In to add comment