Example #1
0
class GameScene:
    def __init__(self, manager):
        self.manager = manager
        self.screenrect = get_screenrect()
        self.player = Player(64, 64)
        self.world = World(levels.data)
        self.world.load("World")
        self.camera = Camera()
        self.menu = GameMenu()

        # ?
        self.map_pad = 128

    def update(self, dt):
        self.player.update(dt, self.check_player_position)

    def draw(self, canvas):
        camera_rect = self.camera.get_rect()
        self.world.draw(canvas, camera_rect)
        self.player.draw(canvas, camera_rect)
        self.menu.draw(canvas)

    def handle_event(self, event):
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_TAB:
                self.menu.toggle_active()

        if not self.menu.active:
            self.player.handle_event(event)

    def activate(self, data):
        pass

    def deactivate(self):
        pass

    def move_camera(self, x, y):
        self.camera.x = x
        self.camera.y = y

    def check_player_position(self, rect, direction):
        if self.menu.active:
            return False

        fixed_rect = pygame.Rect(
            rect.x + self.camera.x, rect.y + self.camera.y, rect.width, rect.height
        )

        if not self.screenrect.contains(rect):
            print("Player is offscreen")
            return False

        tile_rects = self.world.get_tiles(fixed_rect)

        for tile in tile_rects:
            if tile["tile_index"] in self.world.current_map["teleport_to"].keys():
                teleport_name = self.world.current_map["teleport_to"][
                    tile["tile_index"]
                ]
                self.world.load(teleport_name)
                player_pos = self.world.current_map["teleport_from"][self.world.old_map]
                self.player.rect.x = player_pos[0] * 32
                self.player.rect.y = player_pos[1] * 32
                return False
            if tile["tile_data"]["solid"]:
                return False

        map_rect = self.world.get_rect()
        camera_rect = self.camera.get_rect()
        has_moved = False

        # dx = abs(rect.x - self.player.rect.x)
        # dy = abs(rect.y - self.player.rect.y)
        dx = 8
        dy = 8

        if map_rect.contains(rect) and self.screenrect.contains(rect):
            if direction == "right":
                if rect.right > self.screenrect.width - self.map_pad:
                    # camera_rect.x += dx
                    camera_moved = self.camera.move(camera_rect, map_rect, 8, direction)
                    if not camera_moved:
                        has_moved = True
                else:
                    has_moved = True
            elif direction == "left":
                if rect.x < self.map_pad:
                    # camera_rect.x -= dx
                    camera_moved = self.camera.move(camera_rect, map_rect, 8, direction)
                    if not camera_moved:
                        has_moved = True
                else:
                    has_moved = True
            elif direction == "up":
                if rect.y < self.map_pad:
                    # camera_rect.y -= dy
                    camera_moved = self.camera.move(camera_rect, map_rect, 8, direction)
                    if not camera_moved:
                        has_moved = True
                else:
                    has_moved = True
            elif direction == "down":
                if rect.y > self.screenrect.height - self.map_pad:
                    # camera_rect.y += dy
                    camera_moved = self.camera.move(camera_rect, map_rect, 8, direction)
                    if not camera_moved:
                        has_moved = True
                else:
                    has_moved = True
        return has_moved
Example #2
0
class Game:
    def __init__(self):
        self.screenrect = get_screenrect()
        self.world = World(Data.world_data)
        self.player = Player()
        self.camera = Camera()
        self.menu = GameMenu()
        self.dialog = Dialog()

        # figure out where this should go
        self.map_pad = 128

    def update(self, dt):
        pass

    def draw(self, canvas):
        if self.menu.active:
            self.menu.draw(canvas)
        else:
            camera_rect = self.camera.get_rect()
            self.world.draw(canvas, camera_rect)
            self.player.draw(canvas, camera_rect)
            if self.dialog.active:
                self.dialog.draw(canvas)

    def check_player_position(self, rect, direction):
        print("checking player position")
        fixed_rect = pygame.Rect(rect.x + self.camera.x,
                                 rect.y + self.camera.y, rect.width,
                                 rect.height)
        world_rect = self.world.get_rect()

        if not self.screenrect.contains(rect):
            print("Player is going to be off of the screen, returning false")
            return False

        if fixed_rect.right > world_rect.width or fixed_rect.bottom > world_rect.height:
            print(
                "Player is going to be outside of the world, returning false")
            return False

        tile = self.world.get_tile_at_rect(fixed_rect)

        if tile["solid"]:
            print("Player tried to walk on a solid tile, returning false")
            return False
        teleport_tile = self.world.is_teleport_tile(tile)
        if teleport_tile:
            self.teleport(teleport_tile)
            return

        camera_rect = self.camera.get_rect()
        has_moved = False
        if world_rect.contains(rect):
            if direction == "right":
                if rect.right > self.screenrect.width - self.map_pad:
                    # player is in an area where we should scroll the camera
                    camera_moved = self.camera.move(camera_rect, world_rect,
                                                    32, direction)
                    if not camera_moved:
                        has_moved = True
                else:
                    has_moved = True
            if direction == "left":
                if rect.x < self.map_pad:
                    # player is in an area where we should scroll the camera
                    camera_moved = self.camera.move(camera_rect, world_rect,
                                                    32, direction)
                    if not camera_moved:
                        has_moved = True
                else:
                    has_moved = True
            if direction == "up":
                if rect.y < self.map_pad:
                    # player is in an area where we should scroll the camera
                    camera_moved = self.camera.move(camera_rect, world_rect,
                                                    32, direction)
                    if not camera_moved:
                        has_moved = True
                else:
                    has_moved = True
            if direction == "down":
                if rect.bottom > self.screenrect.bottom - self.map_pad:
                    # player is in an area where we should scroll the camera
                    camera_moved = self.camera.move(camera_rect, world_rect,
                                                    32, direction)
                    if not camera_moved:
                        has_moved = True
                else:
                    has_moved = True
        return has_moved

    def teleport(self, to):
        print("TELEPORT: ", to)
        old_map = self.world.map_name
        self.world.load_map(to)
        new_player_pos = self.world.get_teleport_from(old_map)
        self.player.x = new_player_pos[0] * 32
        self.player.y = new_player_pos[1] * 32

    def handle_event(self, event):
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_TAB:
                self.menu.toggle()
                return
            elif event.key == pygame.K_d:
                self.dialog.toggle()
                return
        if self.menu.active:
            self.menu.handle_event(event)
        else:
            self.player.handle_event(event, self.check_player_position)