Replace texture atlas by using origin coordinates
This commit is contained in:
parent
0f92cbce7d
commit
ebced9200d
BIN
assets/stevenson.png
Normal file
BIN
assets/stevenson.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 KiB |
22
survival/image.py
Normal file
22
survival/image.py
Normal file
@ -0,0 +1,22 @@
|
||||
import os
|
||||
|
||||
import pygame
|
||||
|
||||
|
||||
class Image:
|
||||
def __init__(self, filename):
|
||||
self.texture = pygame.image.load(os.path.join('..', 'assets', filename)).convert_alpha()
|
||||
self.image = self.texture
|
||||
self.origin = (0, 0)
|
||||
self.pos = (0, 0)
|
||||
self.scale = 1
|
||||
|
||||
def set_scale(self, scale):
|
||||
self.image = pygame.transform.scale(self.texture,
|
||||
(self.texture.get_width() * scale, self.texture.get_height() * scale))
|
||||
self.scale = scale
|
||||
|
||||
def draw(self, window):
|
||||
window.blit(self.image, self.pos,
|
||||
pygame.Rect(self.origin[0] * self.scale, self.origin[1] * self.scale, 32 * self.scale,
|
||||
32 * self.scale))
|
@ -2,17 +2,31 @@ import os
|
||||
|
||||
import pygame
|
||||
|
||||
from survival.game_object import GameObject
|
||||
from survival.image import Image
|
||||
|
||||
|
||||
class Player(GameObject):
|
||||
class Player:
|
||||
def __init__(self):
|
||||
super().__init__([0, 0], os.path.join('..', 'assets', 'player.png'))
|
||||
self.pos = [0, 0]
|
||||
self.velocity = [0, 0]
|
||||
self.image = Image('stevenson.png')
|
||||
self.image.set_scale(2)
|
||||
self.origin = (0, 0)
|
||||
self.speed = 3
|
||||
self.movement_target = [self.pos[0], self.pos[1]]
|
||||
|
||||
def draw(self, window):
|
||||
super().draw(window)
|
||||
if self.is_moving():
|
||||
if self.velocity[0] == 1:
|
||||
self.image.origin = (96, 0)
|
||||
elif self.velocity[0] == -1:
|
||||
self.image.origin = (64, 0)
|
||||
elif self.velocity[1] == 1:
|
||||
self.image.origin = (0, 0)
|
||||
else:
|
||||
self.image.origin = (32, 0)
|
||||
self.image.pos = self.pos
|
||||
self.image.draw(window)
|
||||
|
||||
def is_moving(self):
|
||||
return self.pos != self.movement_target
|
||||
|
@ -1,28 +0,0 @@
|
||||
import pygame
|
||||
|
||||
|
||||
class TextureAtlas(object):
|
||||
def __init__(self, filename):
|
||||
self.atlas = pygame.image.load(filename).convert()
|
||||
self.images = {}
|
||||
|
||||
def image_at(self, origin, target, color_key=None):
|
||||
if origin in self.images:
|
||||
return self.images[origin]
|
||||
rect = pygame.Rect(origin, target)
|
||||
image = pygame.Surface(rect.size).convert()
|
||||
image.blit(self.atlas, (0, 0), rect)
|
||||
if color_key is not None:
|
||||
if color_key == -1:
|
||||
color_key = image.get_at((0, 0))
|
||||
image.set_colorkey(color_key, pygame.RLEACCEL)
|
||||
self.images[origin] = image
|
||||
return image
|
||||
|
||||
def images_at(self, rects, color_key=None):
|
||||
return [self.image_at(rect, color_key) for rect in rects]
|
||||
|
||||
def load_row(self, rect, image_count, color_key=None):
|
||||
images = [(rect[0] + rect[2] * x, rect[1], rect[2], rect[3])
|
||||
for x in range(image_count)]
|
||||
return self.images_at(images, color_key)
|
@ -1,6 +1,4 @@
|
||||
import os
|
||||
|
||||
from survival.textureatlas import TextureAtlas
|
||||
from survival.image import Image
|
||||
from survival.tile import Tile
|
||||
|
||||
|
||||
@ -9,16 +7,11 @@ class TileLayer:
|
||||
self.width = width
|
||||
self.height = height
|
||||
self.tiles = [[Tile() for x in range(self.width)] for y in range(self.height)]
|
||||
self.atlas = TextureAtlas(os.path.join('..', 'assets', 'atlas.png'))
|
||||
self.loadTiles()
|
||||
|
||||
def loadTiles(self):
|
||||
for y in range(self.height):
|
||||
for x in range(self.width):
|
||||
self.tiles[y][x].image = self.atlas.image_at(self.tiles[y][x].origin, (32, 32))
|
||||
self.image = Image('atlas.png')
|
||||
|
||||
def draw(self, window):
|
||||
for y in range(self.height):
|
||||
for x in range(self.width):
|
||||
if self.tiles[y][x] is not None:
|
||||
window.blit(self.tiles[y][x].image, (x * 32, y * 32))
|
||||
self.image.pos = (x*32, y*32)
|
||||
self.image.origin = self.tiles[y][x].origin
|
||||
self.image.draw(window)
|
||||
|
Loading…
Reference in New Issue
Block a user