Passed
Push — master ( ff156b...4e45e0 )
by Yohann
01:13
created

tiles.Tile.__init__()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
import math
2
from typing import Dict
3
4
import pygame
5
6
from src import TEXTURES_FOLDER, HALF_WIDTH, HALF_HEIGHT
7
from src.functions.loaders import load_sprites_from_dir
8
9
10
class Tile:
11
    SIZE: int = 32
12
    TILE_COUNT_X: int = math.ceil(HALF_WIDTH / SIZE) + 1
13
    TILE_COUNT_Y: int = math.ceil(HALF_HEIGHT / SIZE) + 1
14
15
    SPRITES: Dict[str, pygame.Surface] = load_sprites_from_dir(
16
        f"{TEXTURES_FOLDER}/tiles",
17
        (
18
            "wood_0", "wood_1", "wood_2", "wood_3", "wood_4", "wood_5",
19
            "block_gold", "block_wood",
20
            "blue_1", "blue_2", "blue_3", "blue_4",
21
            "blue_5", "blue_6", "blue_7", "blue_8", "blue_9"
22
        )
23
    )
24
25
    def __init__(self, name, coords):
26
        self.name = name
27
        self.rect = pygame.Rect(coords, (self.SIZE, self.SIZE))
28
29
    def update(self, camera):
30
        if self.rect.x - camera.x < -self.SIZE:
31
            self.rect.x += self.SIZE * self.TILE_COUNT_X
32
33
        elif self.rect.x - camera.x > HALF_WIDTH:
34
            self.rect.x -= self.SIZE * self.TILE_COUNT_X
35
36
        elif self.rect.y - camera.y < -self.SIZE:
37
            self.rect.y += self.SIZE * self.TILE_COUNT_Y
38
39
        elif self.rect.y - camera.y > HALF_HEIGHT:
40
            self.rect.y -= self.SIZE * self.TILE_COUNT_Y
41
42
    @property
43
    def sprite(self):
44
        return self.SPRITES[self.name]
45