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.running = True
- 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 = []
- self.prices = []
- self.spirites = []
- self.score = 0
- self.lives = 3
- self.score_text = self.canvas.create_text(800 - 20,10,anchor = 'ne', font = 'Verdana 18', fill = 'darkblue', text = 'Счет: '+str(self.score))
- self.lives_text = self.canvas.create_text(800 - 20,40,anchor = 'ne', font = 'Verdana 18', fill = 'darkblue', text = 'Жизней: '+str(self.lives))
- def mainloop(self):
- while True:
- if self.running:
- for sprite in self.sprites:
- sprite.move()
- for spirit in self.spirites:
- spirit.move()
- self.root.update_idletasks()
- self.root.update()
- sleep(0.01)
- class Sprite:
- def __init__(self, game):
- self.game = game
- self.coordinates = None
- def move(self):
- pass
- def coords(self):
- return self.coordinates
- class Trophy(Sprite):
- def __init__(self, game, photo_image, x, y, width, height, type = 0):
- #0-coin, 1-cheese,2-meshok, 3-book
- Sprite.__init__(self, game)
- self.photo_image = photo_image
- self.type = type
- self.width = width
- self.height = height
- self.image = game.canvas.create_image(x,y,image=self.photo_image,anchor='nw')
- self.coordinates = Coords(x, y, x + width, y + height)
- class Spirit(Sprite):
- def __init__(self, game, photo_image, x, y, width, height, speed = 1):
- Sprite.__init__(self, game)
- self.photo_image = photo_image
- self.speed = speed
- self.count = 0
- self.width = width
- self.height = height
- 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.speed != 0:
- self.count += self.speed
- if self.count > 100:
- self.speed = - self.speed
- if self.count < 0:
- self.speed = - self.speed
- self.game.canvas.move(self.image, self.speed, 0)
- 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] + self.width
- self.coordinates.y2 = xy[1] + self.height
- return self.coordinates
- class Platform(Sprite):
- def __init__(self, game, photo_image, x, y, width, height, speed = 0):
- Sprite.__init__(self, game)
- self.photo_image = photo_image
- self.speed = speed
- self.count = 0
- self.width = width
- self.height = height
- 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.speed != 0:
- self.count += self.speed
- if self.count > 200:
- self.speed = - self.speed
- if self.count < 0:
- self.speed = - self.speed
- self.game.canvas.move(self.image, self.speed, 0)
- 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] + self.width
- self.coordinates.y2 = xy[1] + self.height
- return self.coordinates
- 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(50, 50, image = self.images_left[0], anchor = 'nw')
- self.x = -2
- self.y = 0
- self.current_image = 0
- self.last_time = time()
- self.coordinates = Coords()
- self.jump_count = 0
- game.canvas.bind_all('<KeyPress-Left>',self.turn_left)
- game.canvas.bind_all('<KeyPress-Right>',self.turn_right)
- game.canvas.bind_all('<KeyPress-Up>',self.stop)
- game.canvas.bind_all('<space>',self.jump)
- def stop(self, event):
- self.x = 0
- 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):
- #на какой кадр? 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 coords(self):
- xy = self.game.canvas.coords(self.image)
- self.coordinates.x1 = xy[0] + 5
- self.coordinates.y1 = xy[1]
- self.coordinates.x2 = xy[0] + 65
- self.coordinates.y2 = xy[1] + 80
- return self.coordinates
- def move(self):
- self.animate()
- 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 price in self.game.prices:
- price_co = price.coords()
- if int_top(co, price_co) or int_left(co, price_co) or int_right(co, price_co) or int_bottom(1, co, price_co):
- if price.type != 3:
- self.game.score += 10
- self.game.canvas.itemconfigure(self.game.score_text, text = 'Счет: '+str(self.game.score))
- self.game.canvas.delete(price.image)
- self.game.prices.remove(price)
- else:
- self.game.running = False
- #перебираем призраков
- for spirit in self.game.spirites:
- spirit_co = spirit.coords()
- if int_top(co, spirit_co) or int_left(co, spirit_co) or int_right(co, spirit_co) or int_bottom(1, co, spirit_co):
- self.game.lives -= 1
- self.game.canvas.itemconfigure(self.game.lives_text, text = 'Жизни: '+str(self.game.lives))
- self.game.canvas.delete(spirit.image)
- self.game.spirites.remove(spirit)
- if self.game.lives == 0:
- self.game.running = False
- #перебираем все из списка sprites
- 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.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
- 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='pl1.gif'), 20, 400, 100, 10)
- g.sprites.append(platforma1)
- platforma2 = Platform(g, PhotoImage(file='pl1.gif'), 180, 350, 100, 10)
- g.sprites.append(platforma2)
- platforma3 = Platform(g, PhotoImage(file='pl2.gif'), 290, 300, 60, 10)
- g.sprites.append(platforma3)
- platforma4 = Platform(g, PhotoImage(file='pl2.gif'), 350, 450, 60, 10)
- g.sprites.append(platforma4)
- platforma5 = Platform(g, PhotoImage(file='pl1.gif'), 440, 400, 100, 10, 1)
- g.sprites.append(platforma5)
- platforma6 = Platform(g, PhotoImage(file='pl1.gif'), 700, 360, 100, 10)
- g.sprites.append(platforma6)
- platforma7 = Platform(g, PhotoImage(file='pl1.gif'), 530, 310, 100, 10)
- g.sprites.append(platforma7)
- platforma8 = Platform(g, PhotoImage(file='pl1.gif'), 350, 280, 100, 10, 1)
- g.sprites.append(platforma8)
- platforma9 = Platform(g, PhotoImage(file='pl1.gif'), 280, 230, 100, 10)
- g.sprites.append(platforma9)
- platforma10 = Platform(g, PhotoImage(file='pl2.gif'), 450, 200, 60, 10)
- g.sprites.append(platforma10)
- platforma11 = Platform(g, PhotoImage(file='pl1.gif'), 550, 170, 100, 10)
- g.sprites.append(platforma11)
- platforma12 = Platform(g, PhotoImage(file='pl1.gif'), 400, 120, 100, 10, 1)
- g.sprites.append(platforma12)
- platforma13 = Platform(g, PhotoImage(file='pl1.gif'), 250, 100, 100, 10)
- g.sprites.append(platforma13)
- price1 = Trophy(g, PhotoImage(file='coin.gif'), 10, 450, 50, 50)
- g.prices.append(price1)
- price2 = Trophy(g, PhotoImage(file='meshok.gif'), 200, 450, 50, 50)
- g.prices.append(price2)
- price3 = Trophy(g, PhotoImage(file='book.gif'), 700, 450, 50, 50, 3)
- g.prices.append(price3)
- spirit1 = Spirit(g, PhotoImage(file='spirit.gif'), 300, 450, 45, 45)
- g.spirites.append(spirit1)
- f = Fox(g)
- g.sprites.append(f)
- g.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement