Created
January 24, 2017 04:42
-
-
Save cbscribe/1def492d97f328a24551bb2e2c1e4ce5 to your computer and use it in GitHub Desktop.
Simple pygame scrolling camera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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