| python |
| import pygame |
| from pygame import |
| from player import |
| from blocks import |
| from monsters import |
| import time |
| import sys |
| WIN_WIDTH |
| WIN_HEIGHT |
| DISPLAY = (WIN_WIDTH, WIN_HEIGHT) |
| BACKGROUND_COLOR = "#000000" |
| FILE_DIR = os.path.dirname(__file__) |
| class Camera(object): |
| def __init__(self, camera_func, width, height): |
| self.camera_func = camera_func |
| self.state = Rect(0, 0, width, height) |
| def apply(self, target): |
| return target.rect.move(self.state.topleft) |
| def update(self, target): |
| self.state = self.camera_func(self.state, target.rect) |
| def camera_configure(camera, target_rect): |
| l, t, _, _ = target_rect |
| _, _, w, h = camera |
| l, t = -l+WIN_WIDTH / 2, -t+WIN_HEIGHT / 2 |
| l = min(0, l) |
| l = max(-(camera.width-WIN_WIDTH), l) |
| t = max(-(camera.height-WIN_HEIGHT), t) |
| t = min(0, t) |
| return Rect(l, t, w, h) |
| def loadLevel(): |
| global playerX, playerY |
| levelFile = open('%s/levels/1.txt' % FILE_DIR) |
| line = " " |
| commands = [] |
| while line[0] != "/": |
| line = levelFile.readline() |
| if line[0] == "[": |
| while line[0] != "]": |
| line = levelFile.readline() |
| if line[0] != "]": |
| endLine = line.find("|") |
| level.append(line[0: endLine]) |
| if line[0] != "": |
| commands = line.split() |
| if len(commands) > 1: |
| if commands[0] == "player": |
| playerX= int(commands[1]) |
| playerY = int(commands[2]) |
| if commands[0] == "portal": |
| tp = BlockTeleport(int(commands[1]),int(commands[2]),int(commands[3]),int(commands[4])) |
| entities.add(tp) |
| platforms.append(tp) |
| animatedEntities.add(tp) |
| if commands[0] == "monster": |
| mn = Monster(int(commands[1]),int(commands[2]),int(commands[3]),int(commands[4]),int(commands[5]),int(commands[6])) |
| entities.add(mn) |
| platforms.append(mn) |
| monsters.add(mn) |
| def main(): |
| loadLevel() |
| pygame.init() |
| screen = pygame.display.set_mode(DISPLAY) |
| pygame.display.set_caption("Super Mario Boy") |
| bg = Surface((WIN_WIDTH,WIN_HEIGHT)) |
| bg.fill(Color(BACKGROUND_COLOR)) |
| left = right = False |
| up = False |
| running = False |
| hero = Player(playerX,playerY) |
| entities.add(hero) |
| timer = pygame.time.Clock() |
| x=y=0 |
| for row in level: |
| for col in row: |
| if col == "-": |
| pf = Platform(x,y) |
| entities.add(pf) |
| platforms.append(pf) |
| if col == "*": |
| bd = BlockDie(x,y) |
| entities.add(bd) |
| platforms.append(bd) |
| if col == "P": |
| pr = Princess(x,y) |
| entities.add(pr) |
| platforms.append(pr) |
| animatedEntities.add(pr) |
| x += PLATFORM_WIDTH |
| y += PLATFORM_HEIGHT |
| x = 0 |
| total_level_width = len(level[0])*PLATFORM_WIDTH |
| total_level_height = len(level)*PLATFORM_HEIGHT |
| camera = Camera(camera_configure, total_level_width, total_level_height) |
| while not hero.winner: |
| timer.tick(60) |
| for e in pygame.event.get(): |
| if e.type == QUIT: |
| #raise SystemExit#, "QUIT" |
| time.sleep(3) |
| pygame.quit() |
| sys.exit() |
| if e.type == KEYDOWN and e.key == K_UP: |
| up = True |
| if e.type == KEYDOWN and e.key == K_LEFT: |
| left = True |
| if e.type == KEYDOWN and e.key == K_RIGHT: |
| right = True |
| if e.type == KEYDOWN and e.key == K_LSHIFT: |
| running = True |
| if e.type == KEYUP and e.key == K_UP: |
| up = False |
| if e.type == KEYUP and e.key == K_RIGHT: |
| right = False |
| if e.type == KEYUP and e.key == K_LEFT: |
| left = False |
| if e.type == KEYUP and e.key == K_LSHIFT: |
| running = False |
| screen.blit(bg, (0,0)) |
| animatedEntities.update() |
| monsters.update(platforms) |
| camera.update(hero) |
| hero.update(left, right, up, running, platforms) |
| for e in entities: |
| screen.blit(e.image, camera.apply(e)) |
| pygame.display.update() |
| level = [] |
| entities = pygame.sprite.Group() |
| animatedEntities = pygame.sprite.Group() |
| monsters = pygame.sprite.Group() |
| platforms = [] |
| if __name__ == "__main__": |
| main() |
Комментарии