From ebced9200d59de31197e054911c8f393aeec67ac Mon Sep 17 00:00:00 2001 From: = Date: Sun, 14 Mar 2021 22:43:26 +0100 Subject: [PATCH] Replace texture atlas by using origin coordinates --- assets/stevenson.png | Bin 0 -> 2014 bytes survival/image.py | 22 ++++++++++++++++++++++ survival/player.py | 22 ++++++++++++++++++---- survival/textureatlas.py | 28 ---------------------------- survival/tile_layer.py | 17 +++++------------ 5 files changed, 45 insertions(+), 44 deletions(-) create mode 100644 assets/stevenson.png create mode 100644 survival/image.py delete mode 100644 survival/textureatlas.py diff --git a/assets/stevenson.png b/assets/stevenson.png new file mode 100644 index 0000000000000000000000000000000000000000..00e57c8621bc8fe1d986a16d295a4762ae004a47 GIT binary patch literal 2014 zcmV<42O;>0P)Pa_-7IWAq|0q7%Cf+hNd#MwHhr%DI06jIL>`IaT~{R?E5#;7tX%vtPjg*Ax%uZe z{s5QKQ29)(!rO6S@tggSmDzQiuJiNd0Ib$WkA4B7+z)Eo7`^9nFfRR^r-z~)98n~^ z2ZqJJ<5N$gqR*jt@ki}d5(Jmu%h0YnKnaQ~N4xcnTbY2uEP zGbl0#@I0V&Cvg|68T#bcgCBmFk_S)mYy82p$p86CelLm4u;AI!4#wnzXCcLgTQ+r^ zpPz^B`^wuOgg~476R23{_w93ppv8DQr6?hJ-xi;hSHJo~Xw3hnX5;TxCtf7@d1&L+ z3bffjKoVmR}ivD;-CLDO{Ie2H}%N3ej_pddUX@) z<#n)?*vRk!D2fbqp0`{*A`GNF5F z=pRZlen*w>OVqVB%|NxR zF)%p5_`y2>U?&m@RN{SDSj!gk}TS7R-8$?@k$p1|`<}*>Ti-*7)qo_$5O{b)$@XkDoxTZsF>cOQ=*vI6r8) zt_w{qz^vTF!NbQ8kIA_B+66eZZ6l9oLpq88)mk0e@)DA%4EkaU1fj7WX5%pHf%@_i zQmHf#OTAXq{C16|CK11R1J|xE!Lm$@Bqwp(zyRk5twgdh!P~fP5ZCj0$g+abu?+f0 z_VM&`WfMaKeV(oG>;TJ}2D7%woy*W!FN?P}M4CO2N!L}#O1$UQ5HitX zKb|Lm=)?iC*)$((HC076o8e2XW_j5xob^IYT}CF8h9vfW85T94+b}sLX{u^XOe8^w zgm>nz!?A26GHewv$oWAlljXB~RMv;Fu&@YGmM}E2AE`_h?2N)zn_6DY=pX_V33Dvg zyL4k^o42!Zn7S?`MS)?g5F+^6Vcyexrq$I|i}6c0ZlG+;Lx9M8-kQ>f(d+@v4_d3M ztDI0OyaU(Vgdoar;Xvu@=jr3)W1bry9Yuh2vUPt#{VoqO(}$1p|7Wkf$-h;2j(i50 z)I=avup57y0_H#d(^rvAr&@e>%Ek2IdqAjz*?+zPLSEtgp!KNm3@6YW#q`~GgAx<7 zSN;xxyw1~K!s(7I@O32Yv9mL~jxkm1Ve;E=KN5H)e31E%ot?q|7~@;*`O9zo#8ZC0 zVzq=*eBN(%wj*qRfvlp|)uY5ZI8*{6r4Q!S-LfHY^i~Odd;I}hr z7l#pjJAlOY;^PvJMnXW7uD#B0Dhhr(Q|e;hqpKqv+=hUbT(<)M{h;n#H~h%AxAT1|L)Y?F-}S%34hy6h?w70Sa$T9(^UlE zCvrkOjJc~K;_ns5LzlMrjsV$fbI4D92!bT?`@)@vJUrEV2|Ir(6N2_q<2S`@$}L>& z_&Hw=z#5!sa`COk7BL~Mw6N8_3G(cd=K`mH?XwTi+}8cl?FS<%^G>xjJnb|lcKuV1 wFunudsrNWr2Pa^9DOT}OJAt2G-x7fT0I_Cc1&kt(egFUf07*qoM6N<$f-^|tr2qf` literal 0 HcmV?d00001 diff --git a/survival/image.py b/survival/image.py new file mode 100644 index 0000000..bca5908 --- /dev/null +++ b/survival/image.py @@ -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)) diff --git a/survival/player.py b/survival/player.py index f0e9c33..a3a728e 100644 --- a/survival/player.py +++ b/survival/player.py @@ -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 diff --git a/survival/textureatlas.py b/survival/textureatlas.py deleted file mode 100644 index c723c27..0000000 --- a/survival/textureatlas.py +++ /dev/null @@ -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) diff --git a/survival/tile_layer.py b/survival/tile_layer.py index 9dfaf75..ce4aaf3 100644 --- a/survival/tile_layer.py +++ b/survival/tile_layer.py @@ -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)