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
#๐ How to do this more pythonic
55 messages ยท Page 1 of 1 (latest)
@strange frost
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.
Closes after a period of inactivity, or when you send !close.
are the attributes you assign to the made instance in _create meant to be protected?
whats the point of making them protected if you don't use them within the class at all
Sprite class can be created either with img_src (string) or via surface(pygame.Surface.) not both
basically this is main problem
i have from_src and from_surface factory methods
i use them to create Sprite
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
i always create protected members
i make them public if needed
it is just my practice
in python its actually the opposite
most languages it is always private first as far as i know
in python nothing is private
i know. i do not care runtime tricks
it is for dx
but i dont see you accessing them within the class so it doesnt make sense to make it protected in the first place
that class is not full code
oh
i cannot send it is too long
!paste
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.
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)
...
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
yeah it wouldnt make sense because that method is return an instance of the Sprite class.
youll have to do from __future__ import annotations at the top of your file to make that sort of type hint
pleas look this:
https://paste.pythondiscord.com/6FLQ
there was weird naming there
i see thanks
also why dont you allow instantiation of the Sprite class?
in other languages i just used two different overloaded constructor
because sometimes you wanna transform sprite sheet image then create Sprite from it you know
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
lmao i see
this is how id do ig, i added those attributes to the init so you can get the type hint within the class atleast and fixed some typing issues
much better but still sucks
yeah.
python is "keep it stupid simple" language
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?
oh lol
that not surface check is not needed i will fix it
oh yeah thats much better
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.