I am currently making a 2d game in Pygame and have run into a roadblock trying to make a scrolling camera (follows the character). I have seen some other answers to similar questions, but they have helped very little. Also, I am purposely doing this without the use of Pygame's
Sprite class, so that makes things a little more complex. How should I approach this?
display_width = 800 display_height = 640 size = (display_width, display_height) half_width = int(display_width / 2) half_height = int(display_height / 2) black = (0,0,0) white = (255,255,255) clock = pygame.time.Clock() pygame.display.set_caption("My game") screen = pygame.display.set_mode(size) background = Surface((32,32)) background.convert() background.fill(Color('#783131')) x = 0 y = 0 platformlst =  players =  level = [ "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", "P P", "P P", "P P", "P PPPPPPPPPPP P", "P P", "P P", "P P", "P PPPPPPPP P", "P P", "P PPPPPPP P", "P PPPPPP P", "P P", "P PPPPPPP P", "P P", "P PPPPPP P", "P P", "P PPPPPPPPPPP P", "P P", "P PPPPPPPPPPP P", "P P", "P P", "P P", "P P", "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",] total_level_width = len(level)*32 total_level_height = len(level)*32 map_size = (total_level_width, total_level_height) virtualwindow = pygame.Surface.get_rect(screen) class Camera: def __init__(self, players): self.left_viewbox = display_width/2 - display_width/8 self.right_viewbox = display_width/2 + display_width/10 def follow(self, shift_x): virtualwindow.x += shift_x print virtualwindow.x for i in players: i.rect.x += shift_x def viewbox(self): if player.x <= self.left_viewbox: view_difference = self.left_viewbox - player.x player.x = self.left_viewbox self.follow(view_difference) if player.x >= self.right_viewbox: view_difference = self.right_viewbox - player.x player.x = self.right_viewbox self.follow(view_difference)
player objects are added to a list, which is called out in the
follow() function, which is called by the
viewbox() function. Then the
follow() function should be adding the
shift_x to the
virtualwindow.x. Shouldn't this therefore scroll the map along its
x axis relative with the characters movement?
Here is the full code: https://github.com/tear727/Netse/blob/master/game.py