"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > Pygame 플랫 포머 게임에서 부드러운 스크롤을 구현하는 방법은 무엇입니까?

Pygame 플랫 포머 게임에서 부드러운 스크롤을 구현하는 방법은 무엇입니까?

2025-03-22에 게시되었습니다
검색:422

How to Implement Smooth Scrolling in Pygame Platformer Games?

스크롤 구현 :

카메라를 구현하려면 카메라

클래스를 사용하여 게임 세계와 플레이어의 위치를 ​​정의합니다. 그런 다음이 오프셋은 화면에 그려 질 때 모든 게임 엔티티의 위치에 적용됩니다.

카메라 클래스 생성 :

클래스 카메라 : 클래스 카메라 : def __init __ (self, camera_func, 너비, 높이) : self.camera_func = camera_func self.state = rect (0, 0, 너비, 높이) def apply (self, target) : return target.rect.move (self.state.topleft) DEF 업데이트 (자체, 대상) : self.state = self.camera_func (self.state, target.rect)

class Camera:
    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)
    camera_func :
  • 카메라가 플레이어를 따르는 방법을 결정합니다. 픽셀. l, t, _, _ = target_rect # l = 왼쪽, t = 상단 _, _, w, h = 카메라 # w = 너비, h = 높이 return rect (-l half_width, -t half_height, w, h)

def complex_camera (카메라, target_rect) : x = -target_rect.center [0] win_width/2 y = -target_rect.center [1] win_height/2 camera.topleft = (pygame.vector2 ((x, y)) - pygame.vector2 (camera.topleft)) * 0.06 # smoothness coolnes 추가 camary.x = max (-(camera.width-win_width), min (0, camera.x)) camera.y = max (-(camera.height-win_height), min (0, camera.y))) 카메라

  • 엔티티에 스크롤 적용 카메라 = 카메라 (complex_camera, total_level_width, total_level_height) # 카메라를 업데이트하십시오 카메라 .update (플레이어) # 모든 엔티티에 스크롤을 적용하십시오 엔터티의 E : screen.blit (e.image, camera.apply (e))

    추가 고려 사항 :

    class Camera:
        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)
  • 하위 클래스를 사용하여 카메라-해동 스프라이트 그룹을 처리하기위한 하위 클래스를 사용합니다. 필요. 플레이어가 화면 밖으로 나가는 것을 방지하기 위해 레벨 경계를 처리합니다.

최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3