#๐Ÿ”’ How to do this more pythonic

55 messages ยท Page 1 of 1 (latest)

strange frost
#
class Sprite:
    def __init__(self):
        raise RuntimeError("Do not use Sprite constructor directly")
    
    @classmethod
    def from_src(cls, img_src: str, frame_count: int, animation_duration_ms: int):
        return cls._create(img_src, frame_count, animation_duration_ms)
    
    @classmethod
    def from_surface(cls, surface: pygame.Surface, frame_count: int, animation_duration_ms: int):
        return cls._create(None, frame_count, animation_duration_ms, surface)

    @classmethod
    def _create(cls, img_src: str, frame_count: int, animation_duration_ms: int, surface: pygame.Surface = None):
        self = object.__new__(cls)
        
        if not surface:
            self._sheet = pygame.image.load(img_src)
        else:
            self._sheet = surface

        self._frame_count = frame_count
        self._animation_duration = animation_duration_ms


        self._last_frame_start_time = None
        self._current_frame_index = 0

        # assings 0 index sprite frame initially.
        # this is important just in case anything wanna access sprite frame surface before draw
        self._set_frame()

        return self
spiral badgerBOT
#

@strange frost

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

gaunt sundial
#

whats the point of making them protected if you don't use them within the class at all

strange frost
#

basically this is main problem

#

i have from_src and from_surface factory methods

#

i use them to create Sprite

gaunt sundial
#

yeah but,

        if not surface:
            self._sheet = pygame.image.load(img_src)
        else:
            self._sheet = surface

        self._frame_count = frame_count
        self._animation_duration = animation_duration_ms


        self._last_frame_start_time = None
        self._current_frame_index = 0

why do they gotta be protected though

strange frost
#

i make them public if needed

#

it is just my practice

gaunt sundial
#

in python its actually the opposite

strange frost
gaunt sundial
#

in python nothing is private

strange frost
#

it is for dx

gaunt sundial
#

but i dont see you accessing them within the class so it doesnt make sense to make it protected in the first place

gaunt sundial
#

oh

strange frost
#

i cannot send it is too long

gaunt sundial
#

!paste

spiral badgerBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

strange frost
#

please just focus the factory function side it is my main problem. it looks ugly and also i cannot type return type

#

like this:

class Sprite: 
....
  @classmethod
      def from_src(cls, img_src: str, frame_count: int, animation_duration_ms: int) -> Surface:
          return cls._create(img_src, frame_count, animation_duration_ms)
...
gaunt sundial
#

that is quite an interesting class you got

#

you cannot create an instance yet you have methods which require an instance so you manually make an instance through the class methods and pass it to normal methods

gaunt sundial
strange frost
#

there was weird naming there

gaunt sundial
#

also why dont you allow instantiation of the Sprite class?

strange frost
#

in other languages i just used two different overloaded constructor

strange frost
#

i just let user create their own image and do whatever on it then pass sprite

#

sprite class just responsible to play that sprite sheet

#

in my case my sprite sheet character is so small so i do this:

surface = pygame.transform.scale_by(pygame.image.load("asset/Idle.png"), 4)
 self._sprite = Sprite.from_surface(surface, frame_count=18, animation_duration_ms=2000)
#

i decided to just force user pass surface

gaunt sundial
gaunt sundial
gaunt sundial
strange frost
gaunt sundial
#

yeah.

strange frost
#

python is "keep it stupid simple" language

gaunt sundial
#

if youre gonna force it to pass a surface only you gonna remove from_src and _create and just put it's functionality into from_surface?

strange frost
#

oh i sent wrong code lol

#

wait

gaunt sundial
#

oh lol

strange frost
#

that not surface check is not needed i will fix it

gaunt sundial
#

oh yeah thats much better

spiral badgerBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.