Python 我的相机跟随算法(包括图像)

Python 我的相机跟随算法(包括图像),python,Python,这是我的旧算法 def spawn(self): self.position_move() #to adjust when the player is spawned def position_move(self): #gets called everytime the player moves camera.x = -object.x+Window.size[0]/2.0 camera.y = -object.y+Window.size[1]/2.0 虽然当我

这是我的旧算法

def spawn(self):
    self.position_move() #to adjust when the player is spawned
def position_move(self):
    #gets called everytime the player moves
    camera.x = -object.x+Window.size[0]/2.0
    camera.y = -object.y+Window.size[1]/2.0
虽然当我走近一个角落时,我能看到房间的“外面”,但效果很好。

我更新算法的全部目的是防止看到房间的“外面”:

    def spawn(self): #when the player is first spawned
        self.position_move() #to adjust when the player is spawned

    def position_move(self):
            #camera.x = -(top left x corner of camera coordinate)
            #camera.y = -(top left y corner of camera coordinate)
             #room_size= total room (or world) size

        diff=(object.x-Window.size[0]/2)
        if diff<0:
            camera.x = object.x+diff
        else:
            diff=(object.x+Window.size[0]/2)-room_size[0]
            if diff<0:
                camera.x = -object.x+Window.size[0]/2.0
        diff=(object.y-Window.size[1]/2)

        if diff<0:
            camera.y = diff+Window.size[1]/2
        else:
            diff=room_size[1]-(object.y+Window.size[1]/2)

            if diff>0:
                camera.y = -object.y+Window.size[1]/2.0
def spawn(self):#玩家第一次繁殖时
self.position_move()#在生成玩家时进行调整
def位置_移动(自):
#camera.x=-(摄影机坐标的左上角x)
#camera.y=-(摄影机坐标的左上角y)
#房间大小=总房间(或世界)大小
diff=(object.x-Window.size[0]/2)

如果diff不确定代码中的错误是什么(我还没有真正查看),那么这里有一个简单的方法将相机绑定到世界:

# This assumes coordinates refer to the top-left of the desired viewport

# Since this is not the case, first invert the object's y to make it so:
object_y = room_size[1] - object.y

camera.x = max(0, object.x - window.size[0] / 2)
camera.y = max(0, object_y - window.size[1] / 2)

if camera.x + window.size[0] > room_size[0]:
    camera.x = room_size[0]  - window.size[0]
if camera.y + window.size[1] > room_size[1]:
    camera.y = room_size[1]  - window.size[1]

# Again, since you're using bottom-left coordinate system, invert the y:
camera.y = room_size[1] - camera.y

我不是说你应该删除这个问题,但这可能是一个更好的地方。如果房间是长方形的,你也许可以简单地用计算机计算出允许的中心的上下限。这个问题(字面上)伤了我一段时间的眼睛。