我想从您那里知道如何确保pyglet(2D)中的摄像头始终跟随播放器,并将其始终保持在屏幕中间。另外,我想知道如何使用鼠标滚轮将播放器始终保持在屏幕中间来进行线性缩放。明确地说,如果有人知道Factorio,我希望相机的行为相同。在周围,我仅找到有关如何通过移动鼠标等操作的示例。不幸的是,我还没有找到我感兴趣的东西。
这是我当前正在使用的脚本:
主类(我不报告所有脚本,但报告与相机有关的部分):
def on_resize(self, width, height):
self.camera.init_gl(width, height)
def on_mouse_scroll(self, x, y, dx, dy):
self.camera.scroll(dy)
def _world(self):
self.camera = camera(self)
self.player = player(self, 0, 0)
self.push_handlers(self.player.keyboard)
相机脚本:
class camera(object):
zoom_in_factor = 1.2
zoom_out_factor = 1 / zoom_in_factor
def __init__(self, game):
self.game = game
self.left = 0
self.right = self.game.width
self.bottom = 0
self.top = self.game.height
self.zoom_level = 1
self.zoomed_width = self.game.width
self.zoomed_height = self.game.height
def init_gl(self, width, height):
self.width = width
self.height = height
glViewport(0, 0, self.width, self.height)
def draw(self):
glPushMatrix()
glOrtho(self.left, self.right, self.bottom, self.top, 1, -1)
glTranslatef(-self.game.player.sprite.x + self.width / 2, -self.game.player.sprite.y + self.height / 2, 0)
self.game.clear()
if self.game.runGame:
for sprite in self.game.mapDraw_3:
self.game.mapDraw_3[sprite].draw()
glPopMatrix()
print(self.game.player.sprite.x, self.game.player.sprite.y)
def scroll(self, dy):
f = self.zoom_in_factor if dy > 0 else self.zoom_out_factor if dy < 0 else 1
if .1 < self.zoom_level * f < 2:
self.zoom_level *= f
vx = self.game.player.sprite.x / self.width
vy = self.game.player.sprite.y / self.height
vx_in_world = self.left + vx * self.zoomed_width
vy_in_world = self.bottom + vy * self.zoomed_height
self.zoomed_width *= f
self.zoomed_height *= f
self.left = vx_in_world - vx * self.zoomed_width
self.right = vx_in_world + (1 - vx) * self.zoomed_width
self.bottom = vy_in_world - vy * self.zoomed_height
self.top = vy_in_world + (1 - vy) * self.zoomed_height
这就是我想要得到的(以Factorio为例):
此刻我从这里拿来并根据需要进行修改的脚本:
How to pan and zoom properly in 2D?
但是,正如您所看到的,我正在使用的脚本基于其他人创建的内容,我讨厌以这种方式使用某些内容,因为它不属于我。因此,我仅将其用于实验和创建自己的相机类。这就是为什么我要提意见。
我看过的其他示例:
https://www.programcreek.com/python/example/91285/pyglet.gl.glOrtho
https://groups.google.com/forum/#!topic/pyglet-users/g4dfSGPNCOk
https://www.tartley.com/2d-graphics-with-pyglet-and-opengl
我看过其他地方,但我不记得链接了>>
[为了避免重复,是的,我看了pyglet的指南,但是至少我是如此愚蠢(我不排除它),没有发现任何可以帮助我理解如何做的东西。
我想从您那里知道如何确保pyglet(2D)中的摄像头始终跟随播放器,并将其始终保持在屏幕中间。另外,我想知道如何制作...
嗯,我不确定您的第一个问题,但我可以为您提供帮助。