Skip to content

Instantly share code, notes, and snippets.

@cbscribe
Created January 24, 2017 04:42
Show Gist options
  • Save cbscribe/1def492d97f328a24551bb2e2c1e4ce5 to your computer and use it in GitHub Desktop.
Save cbscribe/1def492d97f328a24551bb2e2c1e4ce5 to your computer and use it in GitHub Desktop.
Simple pygame scrolling camera
class Camera:
def __init__(self, width, height):
self.camera = pg.Rect(0, 0, width, height)
self.width = width
self.height = height
def apply(self, rect):
return rect.move(self.camera.topleft)
def update(self, target):
x = -target.x * CELLSIZE + int(WIDTH / 2)
y = -target.y * CELLSIZE + int(HEIGHT / 2)
# limit scrolling to map size
x = min(0, x) # left
y = min(0, y) # top
x = max(-(self.width - WIDTH), x) # right
y = max(-(self.height - HEIGHT), y) # bottom
self.camera = pg.Rect(x, y, self.width, self.height)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment