Couldn't beat graphic bugs with sprite caching. Returned to old way. Need to create branch with sprite caching and try to finish it.
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -10,3 +10,12 @@ def scale_image(image, n):
|
|||||||
orig_size = image.get_size()
|
orig_size = image.get_size()
|
||||||
new_size = (int(orig_size[0] * n), int(orig_size[1] * n))
|
new_size = (int(orig_size[0] * n), int(orig_size[1] * n))
|
||||||
return pygame.transform.smoothscale(image, new_size)
|
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
|
||||||
14
eb_engine.py
14
eb_engine.py
@@ -45,8 +45,6 @@ class Map:
|
|||||||
|
|
||||||
self.cells[line].append(deepcopy(final_cell))
|
self.cells[line].append(deepcopy(final_cell))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def draw_map(self, screen, current_frame, grid = True):
|
def draw_map(self, screen, current_frame, grid = True):
|
||||||
for j in range(len(self.cells)):
|
for j in range(len(self.cells)):
|
||||||
for i, cell in enumerate(self.cells[j]):
|
for i, cell in enumerate(self.cells[j]):
|
||||||
@@ -64,8 +62,8 @@ class Map:
|
|||||||
if cell.creature_obj:
|
if cell.creature_obj:
|
||||||
cell.creature_obj.draw(dd)
|
cell.creature_obj.draw(dd)
|
||||||
|
|
||||||
#if grid:
|
if grid:
|
||||||
# pygame.draw.rect(screen, self.color, pygame.Rect(dd["x"], dd["y"], dd["w"], dd["h"]), self.bord)
|
pygame.draw.rect(screen, self.color, pygame.Rect(dd["x"], dd["y"], dd["w"], dd["h"]), self.bord)
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Engine:
|
class Engine:
|
||||||
@@ -113,6 +111,8 @@ class Engine:
|
|||||||
unlock = True
|
unlock = True
|
||||||
current_frame = 0
|
current_frame = 0
|
||||||
max_fps = 60
|
max_fps = 60
|
||||||
|
global_counter = 0
|
||||||
|
global_counter_cap = 100000
|
||||||
|
|
||||||
while running:
|
while running:
|
||||||
# poll for events
|
# poll for events
|
||||||
@@ -125,6 +125,7 @@ class Engine:
|
|||||||
self.screen.fill("chartreuse4")
|
self.screen.fill("chartreuse4")
|
||||||
|
|
||||||
easy_map.draw_map(self.screen, current_frame + 1)
|
easy_map.draw_map(self.screen, current_frame + 1)
|
||||||
|
#print(easy_map.cells[0][0].item_obj.sprite_cache)
|
||||||
|
|
||||||
if unlock:
|
if unlock:
|
||||||
keys = pygame.key.get_pressed()
|
keys = pygame.key.get_pressed()
|
||||||
@@ -143,8 +144,11 @@ class Engine:
|
|||||||
if keys[pygame.K_ESCAPE]:
|
if keys[pygame.K_ESCAPE]:
|
||||||
running = False
|
running = False
|
||||||
|
|
||||||
#print(current_frame + 1)
|
|
||||||
current_frame = (current_frame + 1) % max_fps
|
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
|
# flip() the display to put your work on screen
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from common import dataclass, scale_image
|
from common import deepcopy, dataclass, field, scale_image, path_exists
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Object:
|
class Object:
|
||||||
@@ -6,27 +6,39 @@ class Object:
|
|||||||
name: str
|
name: str
|
||||||
sprite_name: str
|
sprite_name: str
|
||||||
sprite_state: int = 0
|
sprite_state: int = 0
|
||||||
sprite_scale: int = 1
|
#sprite_scale: int = 1
|
||||||
sprite_cache: None = None
|
#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):
|
def draw(self, draw_data):
|
||||||
|
#if draw_data["global_counter"] > self.sprite_cache_upd:
|
||||||
|
# self.sprite_cache = {}
|
||||||
|
|
||||||
if draw_data["spr_up"] == 0:
|
if draw_data["spr_up"] == 0:
|
||||||
if self.sprite_state == len(draw_data["sprites"][self.sprite_name]) - 1:
|
if self.sprite_state == len(draw_data["sprites"][self.sprite_name]) - 1:
|
||||||
self.sprite_state = 0
|
self.sprite_state = 0
|
||||||
else:
|
else:
|
||||||
self.sprite_state += 1
|
self.sprite_state += 1
|
||||||
|
|
||||||
if self.sprite_cache is None:
|
#if path_exists(self.sprite_cache, [self.sprite_name, self.sprite_state]):
|
||||||
self.sprite_cache = draw_data["sprites"][self.sprite_name][self.sprite_state]
|
# self.scale_cached(draw_data)
|
||||||
|
#else:
|
||||||
|
# self.cache_sprite(draw_data["sprites"])
|
||||||
|
|
||||||
if self.sprite_scale != draw_data["scale"]:
|
sp = scale_image(draw_data["sprites"][self.sprite_name][self.sprite_state], draw_data["scale"])
|
||||||
self.sprite_scale = draw_data["scale"]
|
rect = sp.get_rect(center = (draw_data["x"] + draw_data["w"] /2, draw_data["y"] + draw_data["h"]/ 2))
|
||||||
self.sprite_cache = scale_image(draw_data["sprites"][self.sprite_name][self.sprite_state], draw_data["scale"])
|
draw_data["screen"].blit(sp, rect)
|
||||||
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))
|
|
||||||
|
|
||||||
draw_data["screen"].blit(self.sprite_cache, rect)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|||||||
17
main.py
17
main.py
@@ -12,7 +12,11 @@ if __name__ == "__main__":
|
|||||||
# - посмотреть класс спрайта или сделать свой +
|
# - посмотреть класс спрайта или сделать свой +
|
||||||
# - добавить отрисовку существ и предметов с анимацией +
|
# - добавить отрисовку существ и предметов с анимацией +
|
||||||
# почитать про Surface, Display, доку к pygame-gui
|
# почитать про Surface, Display, доку к pygame-gui
|
||||||
# Начало гуя - кнопка, строка ввода, клик
|
# Начало гуя:
|
||||||
|
# - общая идея гуя
|
||||||
|
# - кнопка отключить сетку
|
||||||
|
# - строка ввода
|
||||||
|
# - клик
|
||||||
# Поиск пути, очередь задач для существ
|
# Поиск пути, очередь задач для существ
|
||||||
# Редактор карты
|
# Редактор карты
|
||||||
# Охотник -> деревня
|
# Охотник -> деревня
|
||||||
@@ -22,17 +26,10 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
#техдолг:
|
#техдолг:
|
||||||
#проверить дефолтдикт field и None = None
|
#проверить дефолтдикт field и None = None
|
||||||
|
#не взлетело кэширование - потом доделать
|
||||||
|
|
||||||
# проверить у ллм на ошибки - РЕГУЛЯРНАЯ АКТИВНОСТЬ:
|
# проверить у ллм на ошибки - РЕГУЛЯРНАЯ АКТИВНОСТЬ:
|
||||||
# - deepcopy +
|
# - deepcopy +
|
||||||
# - общие +
|
# - общие +
|
||||||
main()
|
main()
|
||||||
|
#техдолг - draw_data to dd
|
||||||
|
|
||||||
# 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
|
|
||||||
BIN
res/sprites/grass_small_2.png
Normal file
BIN
res/sprites/grass_small_2.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 68 KiB |
Reference in New Issue
Block a user