use texture atlas instead of separate sprites
This commit is contained in:
parent
6a7a33fe24
commit
ede6112af4
@ -6,6 +6,7 @@ from pygame.rect import Rect
|
|||||||
from survival.player import Player
|
from survival.player import Player
|
||||||
from survival.quad_tree import QuadTree
|
from survival.quad_tree import QuadTree
|
||||||
from survival.stone import Stone
|
from survival.stone import Stone
|
||||||
|
from survival.textureatlas import TextureAtlas
|
||||||
from survival.tile import Tile
|
from survival.tile import Tile
|
||||||
|
|
||||||
|
|
||||||
@ -18,17 +19,14 @@ class GameMap:
|
|||||||
self.player = Player()
|
self.player = Player()
|
||||||
self.game_objects.append(self.player)
|
self.game_objects.append(self.player)
|
||||||
self.tiles = [[Tile() for x in range(width)] for y in range(height)]
|
self.tiles = [[Tile() for x in range(width)] for y in range(height)]
|
||||||
self.textures = [pygame.image.load(os.path.join('..', 'assets', 'map1.png')),
|
self.atlas = TextureAtlas(os.path.join('..', 'assets', 'atlas.png'))
|
||||||
pygame.image.load(os.path.join('..', 'assets', 'map2.png')),
|
|
||||||
pygame.image.load(os.path.join('..', 'assets', 'map3.png')),
|
|
||||||
pygame.image.load(os.path.join('..', 'assets', 'map4.png'))]
|
|
||||||
self.game_objects.append(Stone([100, 200]))
|
self.game_objects.append(Stone([100, 200]))
|
||||||
self.quad_tree = QuadTree(0, Rect(0, 0, width * 32, height * 32))
|
self.quad_tree = QuadTree(0, Rect(0, 0, width * 32, height * 32))
|
||||||
|
|
||||||
def draw(self, window):
|
def draw(self, window):
|
||||||
for y in range(self.height):
|
for y in range(self.height):
|
||||||
for x in range(self.width):
|
for x in range(self.width):
|
||||||
window.blit(self.textures[self.tiles[y][x].background_id], (x * 32, y * 32))
|
window.blit(self.atlas.image_at(self.tiles[y][x].origin, (32, 32)), (x*32, y*32))
|
||||||
|
|
||||||
for game_object in self.game_objects:
|
for game_object in self.game_objects:
|
||||||
game_object.draw(window)
|
game_object.draw(window)
|
||||||
|
@ -5,8 +5,8 @@ class TextureAtlas(object):
|
|||||||
def __init__(self, filename):
|
def __init__(self, filename):
|
||||||
self.atlas = pygame.image.load(filename).convert()
|
self.atlas = pygame.image.load(filename).convert()
|
||||||
|
|
||||||
def image_at(self, rectangle, color_key=None):
|
def image_at(self, origin, target, color_key=None):
|
||||||
rect = pygame.Rect(rectangle)
|
rect = pygame.Rect(origin, target)
|
||||||
image = pygame.Surface(rect.size).convert()
|
image = pygame.Surface(rect.size).convert()
|
||||||
image.blit(self.atlas, (0, 0), rect)
|
image.blit(self.atlas, (0, 0), rect)
|
||||||
if color_key is not None:
|
if color_key is not None:
|
||||||
|
@ -4,4 +4,4 @@ from random import randrange
|
|||||||
|
|
||||||
class Tile:
|
class Tile:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.background_id = randrange(4)
|
self.origin = (0, 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user