Python Pygame摄像机运动
所以,我正在用Pygame用Python制作一个2d TopView游戏。我一直在尝试创建一个相机运动,将保持在屏幕中央的球员。我该怎么做?我想有一个单一的表面“地图”,这将是blit的屏幕表面。如果这样做,我可以只构建一次地图,然后以某种方式调整它的位置,使玩家始终保持在屏幕中央。我的播放器将其位置更新如下:Python Pygame摄像机运动,python,pygame,Python,Pygame,所以,我正在用Pygame用Python制作一个2d TopView游戏。我一直在尝试创建一个相机运动,将保持在屏幕中央的球员。我该怎么做?我想有一个单一的表面“地图”,这将是blit的屏幕表面。如果这样做,我可以只构建一次地图,然后以某种方式调整它的位置,使玩家始终保持在屏幕中央。我的播放器将其位置更新如下: def update(self, dx=0, dy=0): newpos = (self.pos[0] + dx, self.pos[1] + dy) # Cal
def update(self, dx=0, dy=0):
newpos = (self.pos[0] + dx, self.pos[1] + dy) # Calculates a new position
entityrect = pygame.Rect(newpos, self.surface.get_size()) # Creates a rect for the player
collided = False
for o in self.objects: # Loops for solid objects in the map
if o.colliderect(entityrect):
collided = True
break
if not collided:
# If the player didn't collide, update the position
self.pos = newpos
return collided
map1 = pygame.Surface((3000, 3000))
img = pygame.image.load("floor.png")
for x in range(0, 3000, img.get_width()):
for y in range(0, 3000, img.get_height()):
map1.blit(img, (x, y))
camera = [0,0]
...
def update(self, dx=0, dy=0):
newpos = (self.pos[0] + dx, self.pos[1] + dy) # Calculates a new position
entityrect = pygame.Rect(newpos, self.surface.get_size())
camera[0] += dx
camera[1] += dy
...
screen.blit(player, (player.pos[0]-camera[0], player.pos[1]-camera[1]))
我发现了,但那是一个侧面的平台。所以我的地图看起来像这样:
def update(self, dx=0, dy=0):
newpos = (self.pos[0] + dx, self.pos[1] + dy) # Calculates a new position
entityrect = pygame.Rect(newpos, self.surface.get_size()) # Creates a rect for the player
collided = False
for o in self.objects: # Loops for solid objects in the map
if o.colliderect(entityrect):
collided = True
break
if not collided:
# If the player didn't collide, update the position
self.pos = newpos
return collided
map1 = pygame.Surface((3000, 3000))
img = pygame.image.load("floor.png")
for x in range(0, 3000, img.get_width()):
for y in range(0, 3000, img.get_height()):
map1.blit(img, (x, y))
camera = [0,0]
...
def update(self, dx=0, dy=0):
newpos = (self.pos[0] + dx, self.pos[1] + dy) # Calculates a new position
entityrect = pygame.Rect(newpos, self.surface.get_size())
camera[0] += dx
camera[1] += dy
...
screen.blit(player, (player.pos[0]-camera[0], player.pos[1]-camera[1]))
那么我该如何做摄像机运动呢?任何帮助都将不胜感激
另外,我希望你能理解我的问题,英语不是我的母语 你没有展示你是如何绘制地图或玩家的,但你可以这样做:
def update(self, dx=0, dy=0):
newpos = (self.pos[0] + dx, self.pos[1] + dy) # Calculates a new position
entityrect = pygame.Rect(newpos, self.surface.get_size()) # Creates a rect for the player
collided = False
for o in self.objects: # Loops for solid objects in the map
if o.colliderect(entityrect):
collided = True
break
if not collided:
# If the player didn't collide, update the position
self.pos = newpos
return collided
map1 = pygame.Surface((3000, 3000))
img = pygame.image.load("floor.png")
for x in range(0, 3000, img.get_width()):
for y in range(0, 3000, img.get_height()):
map1.blit(img, (x, y))
camera = [0,0]
...
def update(self, dx=0, dy=0):
newpos = (self.pos[0] + dx, self.pos[1] + dy) # Calculates a new position
entityrect = pygame.Rect(newpos, self.surface.get_size())
camera[0] += dx
camera[1] += dy
...
screen.blit(player, (player.pos[0]-camera[0], player.pos[1]-camera[1]))
然后你像这样画地图
screen.blit(map1, (0,0),
(camera[0], camera[1], screen.get_width(), screen.get_height())
)
这样,地图将沿相机的相反方向滚动,使播放器保持静止
如果你不想让玩家在你的世界里移动,但不想在屏幕上移动,你可以这样做:
def update(self, dx=0, dy=0):
newpos = (self.pos[0] + dx, self.pos[1] + dy) # Calculates a new position
entityrect = pygame.Rect(newpos, self.surface.get_size()) # Creates a rect for the player
collided = False
for o in self.objects: # Loops for solid objects in the map
if o.colliderect(entityrect):
collided = True
break
if not collided:
# If the player didn't collide, update the position
self.pos = newpos
return collided
map1 = pygame.Surface((3000, 3000))
img = pygame.image.load("floor.png")
for x in range(0, 3000, img.get_width()):
for y in range(0, 3000, img.get_height()):
map1.blit(img, (x, y))
camera = [0,0]
...
def update(self, dx=0, dy=0):
newpos = (self.pos[0] + dx, self.pos[1] + dy) # Calculates a new position
entityrect = pygame.Rect(newpos, self.surface.get_size())
camera[0] += dx
camera[1] += dy
...
screen.blit(player, (player.pos[0]-camera[0], player.pos[1]-camera[1]))
如果你想让你的玩家一直在屏幕中央,那么“移动玩家”实际上应该只是以相反的方式移动地图。如果玩家应该向右扫射一段距离
x
,则将地图向左移动一段距离-x
。Darthfett在您提供的链接中给出的答案似乎非常相关。您看过这个答案了吗: