繁体   English   中英

我将如何制作像pygame中的运动这样的相机?

[英]How would I go about making a camera like movement in pygame?

我的游戏是平台游戏。 我希望播放器在距中心X像素时向左或向右移动。

我了解pygame没有任何可以让相机移动的东西。

当播放器到达距中心X像素的位置时,停止播放器移动,并使地形向相反方向移动,以显示可移动地形的错觉,就像照相机运动一样。

使相机居中放置在播放器上的一种非常基本的方法是仅偏移您绘制的所有内容,以使播放器始终位于相机的中心。 在我自己的游戏中,我使用一个函数来转换坐标:

def to_pygame_coords(coords):
    # move the coordinates so that 0, 0 is the player's position
    # then move the origin to the center of the window
    return coords - player.position.center + window.position.center

要对此进行扩展,以使其不在播放器上绝对定位,您可以将窗口居中放置在盒子上。 然后,更新盒子的中心,这样,如果玩家离开盒子,盒子将随他一起移动(因此移动摄像机)。

伪代码(未测试负坐标):

BOX_WIDTH = 320
BOX_HEIGHT = 240
box_origin = player.position.center
def update_box(player_coords):
    if player_coords.x - box_origin.x > BOX_WIDTH:
        box_origin.x = player_coords.x - BOX_WIDTH
    elif box_origin.x - player_coords.x > BOX_WIDTH:
        box_origin.x = player_coords.x + BOX_WIDTH
    if player_coords.y - box_origin.y > BOX_HEIGHT:
        box_origin.y = player_coords.y - BOX_HEIGHT
    elif box_origin.y - player_coords.y > BOX_HEIGHT:
        box_origin.y = player_coords.y + BOX_HEIGHT

def to_pygame_coords(coords):
    # move the coordinates so that 0, 0 is the box's position
    # then move the origin to the center of the window
    return coords - box_origin + window.position.center

您可以将xscroll制作成可以在屏幕上滚动的所有内容。 然后,当您距中心一定距离时,您无需在xscroll中添加或减去移动速度,而不是将玩家的移动速度添加到他的位置。 这使得一切都以与角色移动相同的速度非常平稳地返回。 我在所有游戏中都使用了此功能,但从未遇到过问题。

可视化:

视差滚动: http : //blog.shinylittlething.com/wp-content/uploads/2009/08/parallax.png (通常具有多层,以不同的速度滚动以显示距离。)

2d tilemap滚动: http ://mikecann.co.uk/wp-content/uploads/2011/11/tm.png

在纸上绘制坐标/这些图像有助于可视化问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM