2013-08-16 44 views
2

所以,我正在用Pygame製作一款Python中的2d頂級遊戲。我一直在試圖創造一個讓玩家保持在屏幕中心的相機運動。我將如何做到這一點?我希望在單個表面上有「地圖」,這將會對屏幕表面產生影響。如果這樣做,我可以一次構建地圖,然後以某種方式調整其位置,以便玩家始終保持在屏幕的中心位置。我的播放器更新是這樣的位置:Python Pygame相機機芯

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 

我發現this,但這是一個sideviewed一些成熟。所以我的地圖看起來是這樣的:

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)) 

那麼我會怎麼做相機運動?任何幫助,將不勝感激。

PS。我希望你能理解我在這裏問的是什麼,英語不是我的母語。 =)

+1

如果你想保持你的球員的中心屏幕上,然後「移動播放器」實際上應該以相反的方式移動地圖。如果玩家應該在距離'x'上劃線,則將地圖移動一段距離'-x'。 Darthfett在你提供的鏈接中的回答似乎非常相關。 – wflynny

+0

有你看着這樣的回答:[?如何滾動添加到pygame的一個平臺遊戲(http://stackoverflow.com/questions/14354171/how-to-add-scrolling-to-a-platformer-in-pygame/14357169#14357169) – sloth

回答

2

好了,你沒有告訴你如何繪製地圖或將您的播放器,但你可以做這樣的事情:

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(map1, (0,0), 
      (camera[0], camera[1], screen.get_width(), screen.get_height()) 
      ) 

這樣地圖就會朝着相機的相反方向滾動,使玩家仍然處於靜止狀態。

如果您wan't玩家在你的世界移動,而不是在屏幕上移動,你可以做這樣的事情:

screen.blit(player, (player.pos[0]-camera[0], player.pos[1]-camera[1]))