-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprites.py
86 lines (57 loc) · 2.36 KB
/
sprites.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import os
from typing import Generator, Tuple
from screeninfo import get_monitors # type: ignore
from file_io import read_json
def load_file(file_name: str) -> str:
return os.path.join(os.path.dirname(__file__), file_name)
game_info = read_json(load_file("data/config.json"))
game_width = game_info["width"]*32
game_height = game_info["height"]*32
game_offset = game_info["offset"]
def get_screen_size() -> Tuple[int, int]:
return next(((m.width, m.height) for m in get_monitors() if m.is_primary),
(0, 0))
screensize = get_screen_size()
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = '1'
# as of right now, this will only
# work with a board of size 20x20
# and offsets of 100 pixels
position = (screensize[0] // 2 - game_width // 2 - game_offset,
screensize[1] // 2 - game_height // 2 - game_offset)
os.environ["SDL_VIDEO_WINDOW_POS"] = f"{str(position[0])}, {str(position[1])}"
import pygame as pg # noqa: E402
screen = pg.display.set_mode(flags=pg.HIDDEN)
def img_outline(img: pg.surface.Surface,
color: Tuple[int, int, int],
loc: Tuple[int, int],
screen: pg.surface.Surface) -> None:
mask = pg.mask.from_surface(img)
mask_outline = mask.outline()
mask_surf = pg.Surface(img.get_size())
for pixel in mask_outline:
mask_surf.set_at(pixel, color)
mask_surf.set_colorkey((0, 0, 0))
screen.blit(mask_surf, (loc[0] - 1, loc[1]))
screen.blit(mask_surf, (loc[0] + 1, loc[1]))
screen.blit(mask_surf, (loc[0], loc[1] - 1))
screen.blit(mask_surf, (loc[0], loc[1] + 1))
def blit_sprite(sprite: pg.surface.Surface,
outline_color: Tuple[int, int, int],
location: Tuple[int, int],
screen: pg.surface.Surface) -> None:
img_outline(sprite, outline_color, location, screen)
screen.blit(sprite, location)
sprites_folder_path = load_file("textures/")
def image_loader(path: str) -> Generator[Tuple[str, pg.surface.Surface],
None,
None]:
for i in os.listdir(path):
yield ((os.path.splitext(i)[0]),
pg.image.load(path + i).convert_alpha())
class Sprites:
def __init__(self):
self.sprites = dict(image_loader(sprites_folder_path))
def main() -> None:
pass
if __name__ == "__main__":
main()