首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我该如何在pygame中制作一个类似运动的相机呢?

我该如何在pygame中制作一个类似运动的相机呢?
EN

Stack Overflow用户
提问于 2012-04-27 08:01:44
回答 3查看 1.9K关注 0票数 1

我的游戏是一个平台游戏。我希望玩家在离开中心X个像素时移动,向左或向右移动。

我知道pygame没有任何能让相机移动的东西。

当玩家到达距离中心X像素的点时,停止玩家的移动,并使地形向相反方向移动,以显示可移动地形的错觉,行为类似于相机运动。

EN

Stack Overflow用户

发布于 2012-04-27 08:08:24

一个非常基本的方法是让相机在玩家的中心,只是偏移你画的所有东西,这样玩家总是在相机的中心。在我自己的游戏中,我使用一个函数来转换坐标:

代码语言:javascript
复制
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

要在此基础上进行扩展,使其不完全位于播放器上,您可以将窗口居中放置在一个框上。然后你更新盒子的中心,这样如果玩家离开盒子,盒子就会跟着他移动(从而移动相机)。

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

代码语言:javascript
复制
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
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10343052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档