我的游戏是一个平台游戏。我希望玩家在离开中心X个像素时移动,向左或向右移动。
我知道pygame没有任何能让相机移动的东西。
当玩家到达距离中心X像素的点时,停止玩家的移动,并使地形向相反方向移动,以显示可移动地形的错觉,行为类似于相机运动。
发布于 2012-04-27 08:08:24
一个非常基本的方法是让相机在玩家的中心,只是偏移你画的所有东西,这样玩家总是在相机的中心。在我自己的游戏中,我使用一个函数来转换坐标:
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.centerhttps://stackoverflow.com/questions/10343052
复制相似问题