Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Kaminarikitten/fb3d3d00be81fe72d6c71e870d335c57 to your computer and use it in GitHub Desktop.
Save Kaminarikitten/fb3d3d00be81fe72d6c71e870d335c57 to your computer and use it in GitHub Desktop.
Implements a 'Breakable' class inheriting from 'Destroyable' to represent breakable background elements like walls in a Pygame game. It uses lxml for XML saving and features a cracked sprite overlay.

Breakable Object Implementation in Pygame

Preview:
"""
Defines Breakable class, a static Destroyable object that is an element of the background.
Generally, this class helps to represent breakable walls.
"""

from __future__ import annotations

import pygame
from lxml import etree

from src.game_entities.destroyable import Destroyable
from src.gui.constant_sprites import constant_sprites


class Breakable(Destroyable):
    """
    A Breakable represent an element of the background that can be destroyed, like a brittle wall.
    At display time, some cracks are added to the sprite of the entity to differentiate it from
    other elements that might be similar but un-breakable.

    Keyword arguments:
    position -- the current position of the entity on screen
    sprite -- the relative path to the visual representation of the entity
    hit_points -- the total of damage that the entity can take before disappearing
    defense -- the resistance of the entity from physical attacks
    resistance -- the resistance of the entity from spiritual attacks

    Attributes:
    sprite_link -- the relative path to the visual representation of the element
    """

    def __init__(
        self,
        position: tuple[int, int],
        sprite: str,
        hit_points: int,
        defense: int,
        resistance: int,
    ) -> None:
        super().__init__("Breakable", position, sprite, hit_points, defense, resistance)
        # Useful in case of saving
        self.sprite_link: str = sprite

    def display(self, screen: pygame.Surface) -> None:
        """
        Display the entity on the given screen and adding some cracks on it to add emphasis to its
        fragile nature.

        Keyword arguments:
        screen -- the screen on which the entity should be drawn
        """
        super().display(screen)
        screen.blit(constant_sprites["cracked"], self.position)

    def save(self, tree_name: str) -> etree.Element:
        """
        Save the current state of the breakable entity in XML format.

        Return the result of this generation.

        Keyword arguments:
        tree_name -- the name that should be given to the root element of the generated XML.
        """
        tree: etree.Element = super().save(tree_name)

        # Save sprite
        sprite: etree.SubElement = etree.SubElement(tree, "sprite")
        sprite.text = self.sprite_link

        return tree
Associated Context
Type Text Note ( .Text Note )
Associated Tags Breakable class Destroyable object Brittle walls LXML library Game entities Constant sprites Display method Save method Tree name Framework: Pygame pygame breakable object game development python lxml game entity destroyable object sprite
💡 Smart Description The code defines a Breakable class that represents the breakable walls. It includes methods to display and save entities, as well as an element of the background can be destroyed with brittle wall's cracks on it in XML format for saving
Implements a 'Breakable' class inheriting from 'Destroyable' to represent breakable background elements like walls in a Pygame game. It uses lxml for XML saving and features a cracked sprite overlay.
🔎 Suggested Searches Example code for defining abreakable component that can be destroyed after saving
How to create a breakable instance in Python using Destroyable and Destroyable
lxml save game state
pygame breakable objects
game development breakable walls
Code snippet for creating a breakpoint of background by destroying an element from the background's game_entitiesdestroyable: Brittle wall. Creating a breakpoints between brittle walls or spiritual attacks?
Python Breakable class with Destroyable object
pygame sprite manipulation
python game entities
Related Links https://www.geeksforgeeks.org/
https://www.geeksforgeeks.org/binary-search/
https://www.geeksforgeeks.org/python-lists/
https://www.pygame.org/docs/
https://www.101computing.net/getting-started-with-pygame/
https://lxml.de/tutorial.html
Related People Anna Jones
Sensitive Information No Sensitive Information Detected
Shareable Link https://947de8c8-995c-423d-a43b-201dda169881.pieces.cloud/?p=a5934caa4d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment