Window scrolling and offest not working in Pygame
Question: I'm working on a simple 2d tile based game in Python using Pygame. I've got
the basics of a camera starting to work, it is bliting the tiles at an offset
but the actual rects of the tiles aren't moving with the blitting. Here's the
camera class, really simple :

    
    
    from Map import level
    import Globals as gb
    class Camera(object):
        def __init__(self, x, y, screen_width, screen_height):
            self.rect = gb.pygame.Rect(x, y, screen_width, screen_height)
        def update(self):
            self.rect.center = gb.player.rect.x, gb.player.rect.y    
    

and the blitting is done here as so:

    
    
    def render(fov, cam):
        for block in new_blocks:
            gb.window.screen.blit(block.image, (block.rect.x - cam.rect.x,
            block.rect.y - cam.rect.y))
    

the map is a simple 2d array, and I'm putting all the tiles into the list
"new_blocks" like this:

    
    
    for row in level.current_map:
        for block in row:
            if block == '.':
                block = Blocks()
                block.pic = 'sand.png'
                block.rect.x = x
                block.rect.y = y
                x += 50
                new_blocks.append(block)
            if block == '#':
                block = Blocks()
                block.ID = 1
                block.is_wall = True
                newImage(block, 'img', 'wall.png')
                block.rect.x = x
                block.rect.y = y
                walls.append(block)
                x += 50
                new_blocks.append(block)
            if x == len(level.current_map[0])*50:
                y += 50
                x = 0
    

A lot of this is messy, and I'm planning to clean it up and use more constants
and declare better variables after I get this working. Thanks for any help!


Answer: Here's the thing: You don't want the camera to modify the actual coordinate
values of the tiles themselves. The answer is actually quite simple, when you
blit the character, use the camera to modify where the character is being
blitted. Then for everything you draw to the screen, there will be two
different sets of coordinates. 1) The actual position of the object and 2) The
coordinates relative to the screen. The one thing you need to watch out for is
that every single object is drawn relative to the screen, because if just one
of them is not, it will mess up your collision detection.

