Skip to content

Instantly share code, notes, and snippets.

@MrArnaudMichel
Last active July 19, 2024 12:48
Show Gist options
  • Save MrArnaudMichel/963d256aa77153b1b2e242a0054b1d39 to your computer and use it in GitHub Desktop.
Save MrArnaudMichel/963d256aa77153b1b2e242a0054b1d39 to your computer and use it in GitHub Desktop.
Add change map animations

Animation on change map

map.py

Imports :

add:

  • from sql import SQL
  • from tool import Tool

Functions :

__init__

self.map_name: str | None = None
self.map_name_text = None

self.image_change_map = pygame.image.load("../../assets/interfaces/maps/frame_map.png").convert_alpha()
self.animation_change_map = 0
self.animation_change_map_active = False

switch_map after define the group:

self.animation_change_map = 0
self.animation_change_map_active = False

and replace the similar part with this

if switch.name.split("_")[0] == "map":
    self.map_layer.zoom = 3
    self.set_draw_change_map(switch.name)
else:
    self.map_layer.zoom = 4

update

if self.animation_change_map_active:
            self.draw_change_map()

And add these functions:

    def set_draw_change_map(self, map_name: str):
        if not self.animation_change_map_active:
            self.map_name = self.sql.get_name_map(map_name)
            self.animation_change_map_active = True
            self.animation_change_map = 0
            self.map_name_text = Tool().create_text(self.map_name, 30, (255, 255, 255))

    def get_surface_change_map(self, alpha: int = 0):
        surface_change_map = pygame.Surface((215, 53), pygame.SRCALPHA).convert_alpha()
        surface_change_map.blit(self.image_change_map, (0, 0))
        surface_change_map.set_alpha(alpha)
        return surface_change_map

    def draw_change_map(self):
        if self.animation_change_map < 255:
            surface = self.get_surface_change_map(self.animation_change_map).convert_alpha()
            Tool.add_text_to_surface(surface, self.map_name_text,
                                     surface.get_width() // 2 - self.map_name_text.get_width() // 2, 4)
            self.animation_change_map += 2
        elif self.animation_change_map < 1024:
            surface = self.get_surface_change_map(255)
            Tool.add_text_to_surface(surface, self.map_name_text,
                                     surface.get_width() // 2 - self.map_name_text.get_width() // 2, 4)
            self.screen.display.blit(surface, (self.screen.display.get_width() - 255, 600))
            self.animation_change_map += 5
        elif self.animation_change_map < 1279:
            surface = self.get_surface_change_map(1279 - self.animation_change_map)
            Tool.add_text_to_surface(surface, self.map_name_text,
                                     surface.get_width() // 2 - self.map_name_text.get_width() // 2, 4)
            self.screen.display.blit(surface, (self.screen.display.get_width() - 255, 600))
            self.animation_change_map += 5
        else:
            self.animation_change_map_active = False
            self.animation_change_map = 0

sql.py

add this function:

    def get_name_map(self, map_id: str) -> str:
        try:
            return self.select("map_name", ["name"], f"id = '{map_id}'")[0][0]
        except IndexError:
            return "error"

tool.py

and this function

    @staticmethod
    def create_text(text: str, size: int, color: tuple[int, int, int], font: str = "Roboto-Light", bold: bool = False):
        font = pygame.font.Font(f"../../assets/fonts/{font}.ttf", size)
        if bold:
            font.set_bold(True)
        return font.render(text, True, color)
        
    @staticmethod
    def add_text_to_surface(surface, text, x, y):
        surface.blit(text, (x, y))

Add names on bdd

Add a table on your bdd named: map_name like this: image with your own names of maps !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment