draw only the visible area of the map

This commit is contained in:
= 2021-03-15 14:10:19 +01:00
parent a1ede76b6b
commit 205d415e39
3 changed files with 8 additions and 6 deletions

View File

@ -17,7 +17,8 @@ class Camera:
return pos[0] + self.camera.left, pos[1] + self.camera.top return pos[0] + self.camera.left, pos[1] + self.camera.top
def get_visible_area(self): def get_visible_area(self):
pass return Rect(-self.camera.left, -self.camera.top,
SCREEN_WIDTH - self.camera.left, SCREEN_HEIGHT - self.camera.top)
def update(self, target): def update(self, target):
x = -target.pos[0] + int(SCREEN_WIDTH / 2) x = -target.pos[0] + int(SCREEN_WIDTH / 2)

View File

@ -11,8 +11,9 @@ class GameMap:
self.layers.append(TileLayer(width, height)) self.layers.append(TileLayer(width, height))
def draw(self, camera): def draw(self, camera):
visible_area = camera.get_visible_area()
for layer in self.layers: for layer in self.layers:
layer.draw(camera) layer.draw(camera, visible_area)
self.player.draw(camera) self.player.draw(camera)
def update(self, camera, delta, pressed_keys): def update(self, camera, delta, pressed_keys):

View File

@ -9,9 +9,9 @@ class TileLayer:
self.tiles = [[Tile() for x in range(self.width)] for y in range(self.height)] self.tiles = [[Tile() for x in range(self.width)] for y in range(self.height)]
self.image = Image('atlas.png') self.image = Image('atlas.png')
def draw(self, camera): def draw(self, camera, visible_area):
for y in range(self.height): for y in range(int(visible_area.top/32), int(visible_area.height/32) + 1):
for x in range(self.width): for x in range(int(visible_area.left/32), int(visible_area.width/32) + 1):
self.image.pos = (x*32, y*32) self.image.pos = (x * 32, y * 32)
self.image.origin = self.tiles[y][x].origin self.image.origin = self.tiles[y][x].origin
camera.draw(self.image) camera.draw(self.image)