diff --git a/main.py b/main.py index f0b6d63..488dcdd 100644 --- a/main.py +++ b/main.py @@ -11,219 +11,219 @@ MAP_COLLISION_LAYER = 1 class Game(object): - def __init__(self): - self.currentLevelNumber = 0 - self.levels = [] - self.levels.append(Level(fileName = "resources/level1.tmx")) - self.currentLevel = self.levels[self.currentLevelNumber] - - self.player = Player(x = 200, y = 100) - self.player.currentLevel = self.currentLevel - - self.overlay = pygame.image.load("resources/overlay.png") - - def processEvents(self): - for event in pygame.event.get(): - if event.type == pygame.QUIT: - return True - elif event.type == pygame.KEYDOWN: - if event.key == pygame.K_LEFT: - self.player.goLeft() - elif event.key == pygame.K_RIGHT: - self.player.goRight() - elif event.key == pygame.K_UP: - self.player.jump() - elif event.type == pygame.KEYUP: - if event.key == pygame.K_LEFT and self.player.changeX < 0: - self.player.stop() - elif event.key == pygame.K_RIGHT and self.player.changeX > 0: - self.player.stop() - - return False - - def runLogic(self): - self.player.update() - - def draw(self, screen): - screen.fill(BACKGROUND) - self.currentLevel.draw(screen) - self.player.draw(screen) - screen.blit(self.overlay, [0, 0]) - pygame.display.flip() - + def __init__(self): + self.currentLevelNumber = 0 + self.levels = [] + self.levels.append(Level(fileName = "resources/level1.tmx")) + self.currentLevel = self.levels[self.currentLevelNumber] + + self.player = Player(x = 200, y = 100) + self.player.currentLevel = self.currentLevel + + self.overlay = pygame.image.load("resources/overlay.png") + + def processEvents(self): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return True + elif event.type == pygame.KEYDOWN: + if event.key == pygame.K_LEFT: + self.player.goLeft() + elif event.key == pygame.K_RIGHT: + self.player.goRight() + elif event.key == pygame.K_UP: + self.player.jump() + elif event.type == pygame.KEYUP: + if event.key == pygame.K_LEFT and self.player.changeX < 0: + self.player.stop() + elif event.key == pygame.K_RIGHT and self.player.changeX > 0: + self.player.stop() + + return False + + def runLogic(self): + self.player.update() + + def draw(self, screen): + screen.fill(BACKGROUND) + self.currentLevel.draw(screen) + self.player.draw(screen) + screen.blit(self.overlay, [0, 0]) + pygame.display.flip() + class Player(pygame.sprite.Sprite): - def __init__(self, x, y): - pygame.sprite.Sprite.__init__(self) - - self.sprites = SpriteSheet("resources/player.png") - - self.stillRight = self.sprites.image_at((0, 0, 30, 42), colorkey = (250, 255, 0)) - self.stillLeft = self.sprites.image_at((0, 42, 30, 42), colorkey = (250, 255, 0)) - - self.runningRight = (self.sprites.image_at((0, 84, 30, 42), colorkey = (250, 255, 0)), - self.sprites.image_at((30, 84, 30, 42), colorkey = (250, 255, 0)), - self.sprites.image_at((60, 84, 30, 42), colorkey = (250, 255, 0)), - self.sprites.image_at((90, 84, 30, 42), colorkey = (250, 255, 0)), - self.sprites.image_at((120, 84, 30, 42), colorkey = (250, 255, 0))) + def __init__(self, x, y): + pygame.sprite.Sprite.__init__(self) + + self.sprites = SpriteSheet("resources/player.png") + + self.stillRight = self.sprites.image_at((0, 0, 30, 42), colorkey = (250, 255, 0)) + self.stillLeft = self.sprites.image_at((0, 42, 30, 42), colorkey = (250, 255, 0)) + + self.runningRight = (self.sprites.image_at((0, 84, 30, 42), colorkey = (250, 255, 0)), + self.sprites.image_at((30, 84, 30, 42), colorkey = (250, 255, 0)), + self.sprites.image_at((60, 84, 30, 42), colorkey = (250, 255, 0)), + self.sprites.image_at((90, 84, 30, 42), colorkey = (250, 255, 0)), + self.sprites.image_at((120, 84, 30, 42), colorkey = (250, 255, 0))) - self.runningLeft = (self.sprites.image_at((0, 126, 30, 42), colorkey = (250, 255, 0)), - self.sprites.image_at((30, 126, 30, 42), colorkey = (250, 255, 0)), - self.sprites.image_at((60, 126, 30, 42), colorkey = (250, 255, 0)), - self.sprites.image_at((90, 126, 30, 42), colorkey = (250, 255, 0)), - self.sprites.image_at((120, 126, 30, 42), colorkey = (250, 255, 0))) - - self.jumpingRight = (self.sprites.image_at((30, 0, 30, 42), colorkey = (250, 255, 0)), - self.sprites.image_at((60, 0, 30, 42), colorkey = (250, 255, 0)), - self.sprites.image_at((90, 0, 30, 42), colorkey = (250, 255, 0))) + self.runningLeft = (self.sprites.image_at((0, 126, 30, 42), colorkey = (250, 255, 0)), + self.sprites.image_at((30, 126, 30, 42), colorkey = (250, 255, 0)), + self.sprites.image_at((60, 126, 30, 42), colorkey = (250, 255, 0)), + self.sprites.image_at((90, 126, 30, 42), colorkey = (250, 255, 0)), + self.sprites.image_at((120, 126, 30, 42), colorkey = (250, 255, 0))) + + self.jumpingRight = (self.sprites.image_at((30, 0, 30, 42), colorkey = (250, 255, 0)), + self.sprites.image_at((60, 0, 30, 42), colorkey = (250, 255, 0)), + self.sprites.image_at((90, 0, 30, 42), colorkey = (250, 255, 0))) - self.jumpingLeft = (self.sprites.image_at((30, 42, 30, 42), colorkey = (250, 255, 0)), - self.sprites.image_at((60, 42, 30, 42), colorkey = (250, 255, 0)), - self.sprites.image_at((90, 42, 30, 42), colorkey = (250, 255, 0))) - - self.image = self.stillRight - - self.rect = self.image.get_rect() - self.rect.x = x - self.rect.y = y - - self.changeX = 0 - self.changeY = 0 - self.direction = "right" - - self.running = False - self.runningFrame = 0 - self.runningTime = pygame.time.get_ticks() - - self.currentLevel = None - - def update(self): - self.rect.x += self.changeX - tileHitList = pygame.sprite.spritecollide(self, self.currentLevel.layers[MAP_COLLISION_LAYER].tiles, False) - for tile in tileHitList: - if self.changeX > 0: - self.rect.right = tile.rect.left - else: - self.rect.left = tile.rect.right - - if self.rect.right >= SCREEN_WIDTH - 200: - difference = self.rect.right - (SCREEN_WIDTH - 200) - self.rect.right = SCREEN_WIDTH - 200 - self.currentLevel.shiftLevel(-difference) - - if self.rect.left <= 200: - difference = 200 - self.rect.left - self.rect.left = 200 - self.currentLevel.shiftLevel(difference) - - self.rect.y += self.changeY - tileHitList = pygame.sprite.spritecollide(self, self.currentLevel.layers[MAP_COLLISION_LAYER].tiles, False) - if len(tileHitList) > 0: - for tile in tileHitList: - if self.changeY > 0: - self.rect.bottom = tile.rect.top - self.changeY = 1 - - if self.direction == "right": - self.image = self.stillRight - else: - self.image = self.stillLeft - else: - self.rect.top = tile.rect.bottom - self.changeY = 0 - else: - self.changeY += 0.2 - if self.changeY > 0: - if self.direction == "right": - self.image = self.jumpingRight[1] - else: - self.image = self.jumpingLeft[1] - - if self.running and self.changeY == 1: - if self.direction == "right": - self.image = self.runningRight[self.runningFrame] - else: - self.image = self.runningLeft[self.runningFrame] - - if pygame.time.get_ticks() - self.runningTime > 50: - self.runningTime = pygame.time.get_ticks() - if self.runningFrame == 4: - self.runningFrame = 0 - else: - self.runningFrame += 1 - - def jump(self): - self.rect.y += 2 - tileHitList = pygame.sprite.spritecollide(self, self.currentLevel.layers[MAP_COLLISION_LAYER].tiles, False) - self.rect.y -= 2 - - if len(tileHitList) > 0: - if self.direction == "right": - self.image = self.jumpingRight[0] - else: - self.image = self.jumpingLeft[0] - - self.changeY = -6 - - def goRight(self): - self.direction = "right" - self.running = True - self.changeX = 3 - - def goLeft(self): - self.direction = "left" - self.running = True - self.changeX = -3 - - def stop(self): - self.running = False - self.changeX = 0 - - def draw(self, screen): - screen.blit(self.image, self.rect) - + self.jumpingLeft = (self.sprites.image_at((30, 42, 30, 42), colorkey = (250, 255, 0)), + self.sprites.image_at((60, 42, 30, 42), colorkey = (250, 255, 0)), + self.sprites.image_at((90, 42, 30, 42), colorkey = (250, 255, 0))) + + self.image = self.stillRight + + self.rect = self.image.get_rect() + self.rect.x = x + self.rect.y = y + + self.changeX = 0 + self.changeY = 0 + self.direction = "right" + + self.running = False + self.runningFrame = 0 + self.runningTime = pygame.time.get_ticks() + + self.currentLevel = None + + def update(self): + self.rect.x += self.changeX + tileHitList = pygame.sprite.spritecollide(self, self.currentLevel.layers[MAP_COLLISION_LAYER].tiles, False) + for tile in tileHitList: + if self.changeX > 0: + self.rect.right = tile.rect.left + else: + self.rect.left = tile.rect.right + + if self.rect.right >= SCREEN_WIDTH - 200: + difference = self.rect.right - (SCREEN_WIDTH - 200) + self.rect.right = SCREEN_WIDTH - 200 + self.currentLevel.shiftLevel(-difference) + + if self.rect.left <= 200: + difference = 200 - self.rect.left + self.rect.left = 200 + self.currentLevel.shiftLevel(difference) + + self.rect.y += self.changeY + tileHitList = pygame.sprite.spritecollide(self, self.currentLevel.layers[MAP_COLLISION_LAYER].tiles, False) + if len(tileHitList) > 0: + for tile in tileHitList: + if self.changeY > 0: + self.rect.bottom = tile.rect.top + self.changeY = 1 + + if self.direction == "right": + self.image = self.stillRight + else: + self.image = self.stillLeft + else: + self.rect.top = tile.rect.bottom + self.changeY = 0 + else: + self.changeY += 0.2 + if self.changeY > 0: + if self.direction == "right": + self.image = self.jumpingRight[1] + else: + self.image = self.jumpingLeft[1] + + if self.running and self.changeY == 1: + if self.direction == "right": + self.image = self.runningRight[self.runningFrame] + else: + self.image = self.runningLeft[self.runningFrame] + + if pygame.time.get_ticks() - self.runningTime > 50: + self.runningTime = pygame.time.get_ticks() + if self.runningFrame == 4: + self.runningFrame = 0 + else: + self.runningFrame += 1 + + def jump(self): + self.rect.y += 2 + tileHitList = pygame.sprite.spritecollide(self, self.currentLevel.layers[MAP_COLLISION_LAYER].tiles, False) + self.rect.y -= 2 + + if len(tileHitList) > 0: + if self.direction == "right": + self.image = self.jumpingRight[0] + else: + self.image = self.jumpingLeft[0] + + self.changeY = -6 + + def goRight(self): + self.direction = "right" + self.running = True + self.changeX = 3 + + def goLeft(self): + self.direction = "left" + self.running = True + self.changeX = -3 + + def stop(self): + self.running = False + self.changeX = 0 + + def draw(self, screen): + screen.blit(self.image, self.rect) + class Level(object): - def __init__(self, fileName): - self.mapObject = pytmx.load_pygame(fileName) - self.layers = [] - self.levelShift = 0 - - for layer in range(len(self.mapObject.layers)): - self.layers.append(Layer(index = layer, mapObject = self.mapObject)) - - def shiftLevel(self, shiftX): - self.levelShift += shiftX - - for layer in self.layers: - for tile in layer.tiles: - tile.rect.x += shiftX - - def draw(self, screen): - for layer in self.layers: - layer.draw(screen) - + def __init__(self, fileName): + self.mapObject = pytmx.load_pygame(fileName) + self.layers = [] + self.levelShift = 0 + + for layer in range(len(self.mapObject.layers)): + self.layers.append(Layer(index = layer, mapObject = self.mapObject)) + + def shiftLevel(self, shiftX): + self.levelShift += shiftX + + for layer in self.layers: + for tile in layer.tiles: + tile.rect.x += shiftX + + def draw(self, screen): + for layer in self.layers: + layer.draw(screen) + class Layer(object): - def __init__(self, index, mapObject): - self.index = index - self.tiles = pygame.sprite.Group() - self.mapObject = mapObject - - for x in range(self.mapObject.width): - for y in range(self.mapObject.height): - img = self.mapObject.get_tile_image(x, y, self.index) - if img: - self.tiles.add(Tile(image = img, x = (x * self.mapObject.tilewidth), y = (y * self.mapObject.tileheight))) - - def draw(self, screen): - self.tiles.draw(screen) - + def __init__(self, index, mapObject): + self.index = index + self.tiles = pygame.sprite.Group() + self.mapObject = mapObject + + for x in range(self.mapObject.width): + for y in range(self.mapObject.height): + img = self.mapObject.get_tile_image(x, y, self.index) + if img: + self.tiles.add(Tile(image = img, x = (x * self.mapObject.tilewidth), y = (y * self.mapObject.tileheight))) + + def draw(self, screen): + self.tiles.draw(screen) + class Tile(pygame.sprite.Sprite): - def __init__(self, image, x, y): - pygame.sprite.Sprite.__init__(self) - - self.image = image - self.rect = self.image.get_rect() - self.rect.x = x - self.rect.y = y + def __init__(self, image, x, y): + pygame.sprite.Sprite.__init__(self) + + self.image = image + self.rect = self.image.get_rect() + self.rect.x = x + self.rect.y = y class SpriteSheet(object): def __init__(self, fileName): @@ -246,21 +246,21 @@ def load_strip(self, rect, image_count, colorkey = None): tups = [(rect[0]+rect[2]*x, rect[1], rect[2], rect[3]) for x in range(image_count)] return self.images_at(tups, colorkey) - + def main(): - pygame.init() - screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) - pygame.display.set_caption("Pygame Tiled Demo") - clock = pygame.time.Clock() - done = False - game = Game() - - while not done: - done = game.processEvents() - game.runLogic() - game.draw(screen) - clock.tick(60) - - pygame.quit() - + pygame.init() + screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) + pygame.display.set_caption("Pygame Tiled Demo") + clock = pygame.time.Clock() + done = False + game = Game() + + while not done: + done = game.processEvents() + game.runLogic() + game.draw(screen) + clock.tick(60) + + pygame.quit() + main()