Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我该如何在pygame中制作一个像照相机一样的动作?_Python_Animation_Pygame - Fatal编程技术网

Python 我该如何在pygame中制作一个像照相机一样的动作?

Python 我该如何在pygame中制作一个像照相机一样的动作?,python,animation,pygame,Python,Animation,Pygame,我的游戏是一个平台游戏。我希望播放器在距离中心X像素时移动,向左或向右移动 我知道pygame没有任何东西可以让摄像机移动 当玩家到达距离中心X像素的点时,停止玩家移动,让地形向相反方向移动,以显示移动地形的错觉,像摄像机运动一样动作。让摄像机对准播放器的一个非常基本的方法就是偏移你所画的一切,这样播放器就始终在摄像机的中心。在我自己的游戏中,我使用一个函数来转换坐标: def to_pygame_coords(coords): # move the coordinates so tha

我的游戏是一个平台游戏。我希望播放器在距离中心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
若要在此基础上展开,使其不完全位于播放机上,您可以将窗口置于长方体的中心。然后更新框的中心,这样,如果玩家离开框,框将随之移动,从而移动相机

未测试负坐标的伪代码:

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中加上或减去移动速度。这使得所有的东西都能以你的角色移动的速度平滑地移动回去。我在我所有的游戏中都使用这个,我从来没有遇到过问题。

可视化:

视差滚动:通常有多个层,以不同的速度滚动,以显示距离

二维tilemap滚动:


在纸上绘制坐标/这些图像有助于将问题形象化。

您的意思是希望摄影机以玩家为中心,还是以玩家到达边缘时“推动”的玩家周围的盒子为中心?