diff --git a/__pycache__/common.cpython-314.pyc b/__pycache__/common.cpython-314.pyc index e34ff9d..a187954 100644 Binary files a/__pycache__/common.cpython-314.pyc and b/__pycache__/common.cpython-314.pyc differ diff --git a/__pycache__/eb_engine.cpython-314.pyc b/__pycache__/eb_engine.cpython-314.pyc index b9e7e45..c2c2832 100644 Binary files a/__pycache__/eb_engine.cpython-314.pyc and b/__pycache__/eb_engine.cpython-314.pyc differ diff --git a/__pycache__/eb_objects.cpython-314.pyc b/__pycache__/eb_objects.cpython-314.pyc index 2bbb82d..db870ee 100644 Binary files a/__pycache__/eb_objects.cpython-314.pyc and b/__pycache__/eb_objects.cpython-314.pyc differ diff --git a/common.py b/common.py index 7848acd..6d2f129 100644 --- a/common.py +++ b/common.py @@ -9,4 +9,13 @@ import pygame def scale_image(image, n): orig_size = image.get_size() new_size = (int(orig_size[0] * n), int(orig_size[1] * n)) - return pygame.transform.smoothscale(image, new_size) \ No newline at end of file + return pygame.transform.smoothscale(image, new_size) + +def path_exists(data, path): + current = data + for key in path: + if isinstance(current, dict) and key in current: + current = current[key] + else: + return False + return True \ No newline at end of file diff --git a/eb_engine.py b/eb_engine.py index aec361f..c37a85a 100644 --- a/eb_engine.py +++ b/eb_engine.py @@ -45,8 +45,6 @@ class Map: self.cells[line].append(deepcopy(final_cell)) - - def draw_map(self, screen, current_frame, grid = True): for j in range(len(self.cells)): for i, cell in enumerate(self.cells[j]): @@ -64,8 +62,8 @@ class Map: if cell.creature_obj: cell.creature_obj.draw(dd) - #if grid: - # pygame.draw.rect(screen, self.color, pygame.Rect(dd["x"], dd["y"], dd["w"], dd["h"]), self.bord) + if grid: + pygame.draw.rect(screen, self.color, pygame.Rect(dd["x"], dd["y"], dd["w"], dd["h"]), self.bord) @dataclass class Engine: @@ -113,6 +111,8 @@ class Engine: unlock = True current_frame = 0 max_fps = 60 + global_counter = 0 + global_counter_cap = 100000 while running: # poll for events @@ -125,6 +125,7 @@ class Engine: self.screen.fill("chartreuse4") easy_map.draw_map(self.screen, current_frame + 1) + #print(easy_map.cells[0][0].item_obj.sprite_cache) if unlock: keys = pygame.key.get_pressed() @@ -143,8 +144,11 @@ class Engine: if keys[pygame.K_ESCAPE]: running = False - #print(current_frame + 1) current_frame = (current_frame + 1) % max_fps + if global_counter < global_counter_cap: + global_counter += 1 + else: + global_counter = 0 # flip() the display to put your work on screen pygame.display.flip() diff --git a/eb_objects.py b/eb_objects.py index c1b5de9..27d79b4 100644 --- a/eb_objects.py +++ b/eb_objects.py @@ -1,32 +1,44 @@ -from common import dataclass, scale_image +from common import deepcopy, dataclass, field, scale_image, path_exists @dataclass class Object: - id: str - name: str - sprite_name: str - sprite_state: int = 0 - sprite_scale: int = 1 - sprite_cache: None = None + id: str + name: str + sprite_name: str + sprite_state: int = 0 + #sprite_scale: int = 1 + #sprite_cache: dict = field(default_factory = dict) + #sprite_cache_upd: int = 100 + # + #def cache_sprite(self, sprites): + # if self.sprite_name not in self.sprite_cache: + # self.sprite_cache[self.sprite_name] = {} + # self.sprite_cache[self.sprite_name][self.sprite_state] = deepcopy(sprites[self.sprite_name][self.sprite_state]) + # + #def scale_cached(self, draw_data): + # if self.sprite_scale != draw_data["scale"]: + # self.sprite_scale = draw_data["scale"] + # self.sprite_cache[self.sprite_name][self.sprite_state] = deepcopy(scale_image(draw_data["sprites"][self.sprite_name][self.sprite_state], draw_data["scale"])) + def draw(self, draw_data): + #if draw_data["global_counter"] > self.sprite_cache_upd: + # self.sprite_cache = {} + if draw_data["spr_up"] == 0: if self.sprite_state == len(draw_data["sprites"][self.sprite_name]) - 1: self.sprite_state = 0 else: self.sprite_state += 1 - if self.sprite_cache is None: - self.sprite_cache = draw_data["sprites"][self.sprite_name][self.sprite_state] - - if self.sprite_scale != draw_data["scale"]: - self.sprite_scale = draw_data["scale"] - self.sprite_cache = scale_image(draw_data["sprites"][self.sprite_name][self.sprite_state], draw_data["scale"]) - rect = self.sprite_cache.get_rect(center = (draw_data["x"] + draw_data["w"] /2, draw_data["y"] + draw_data["h"]/ 2)) - else: - rect = self.sprite_cache.get_rect(center = (draw_data["x"] + draw_data["w"] /2, draw_data["y"] + draw_data["h"]/ 2)) + #if path_exists(self.sprite_cache, [self.sprite_name, self.sprite_state]): + # self.scale_cached(draw_data) + #else: + # self.cache_sprite(draw_data["sprites"]) - draw_data["screen"].blit(self.sprite_cache, rect) + sp = scale_image(draw_data["sprites"][self.sprite_name][self.sprite_state], draw_data["scale"]) + rect = sp.get_rect(center = (draw_data["x"] + draw_data["w"] /2, draw_data["y"] + draw_data["h"]/ 2)) + draw_data["screen"].blit(sp, rect) @dataclass diff --git a/main.py b/main.py index 0d434a2..870ba8d 100644 --- a/main.py +++ b/main.py @@ -12,7 +12,11 @@ if __name__ == "__main__": # - посмотреть класс спрайта или сделать свой + # - добавить отрисовку существ и предметов с анимацией + # почитать про Surface, Display, доку к pygame-gui - # Начало гуя - кнопка, строка ввода, клик + # Начало гуя: + # - общая идея гуя + # - кнопка отключить сетку + # - строка ввода + # - клик # Поиск пути, очередь задач для существ # Редактор карты # Охотник -> деревня @@ -22,17 +26,10 @@ if __name__ == "__main__": #техдолг: #проверить дефолтдикт field и None = None + #не взлетело кэширование - потом доделать # проверить у ллм на ошибки - РЕГУЛЯРНАЯ АКТИВНОСТЬ: # - deepcopy + # - общие + main() - - - # P.S. to previous commit: - #instead of Sprite flow load_sprites function was changed: - #gathering the number of sprites with the same names before second _ and hold it at - #sprites dict. also now sprites must be named as objectname_action_number - # - # Main commit - full sprites support, with caching, transparency and smooth scaling - # drawing function moved to Object methods \ No newline at end of file + #техдолг - draw_data to dd \ No newline at end of file diff --git a/res/sprites/grass_small_2.png b/res/sprites/grass_small_2.png new file mode 100644 index 0000000..134b18d Binary files /dev/null and b/res/sprites/grass_small_2.png differ