#๐Ÿ”’ is this a sufficient way to "animate" things? (pygame)

29 messages ยท Page 1 of 1 (latest)

storm kiln
#

small snippet containing the "animated" part of the code

# importing pygame, loading images and initialising stuff
FPS = 20
frame_count = 0
While True:
#  for event in pygame.event.get() and other stuff
  if frame_count//5%4 == 0:
    character_right = character_right1 #(pygame.load.image("path\\etc.1))
  if frame_count//5%4 == 1:
    character_right = character_right2 #(pygame.load.image("path\\etc.2))
  if frame_count//5%4 == 2:
    character_right = character_right3 #(pygame.load.image("path\\etc.3))
  if frame_count//5%4 == 3:
    character_right = character_right4 #(pygame.load.image("path\\etc.4))
# a bunch of game states and stuff
  if game_active:
    screen.blit(character_right, character_rect)
    frame_count += 1
  pygame.display.update()
  clock.tick(FPS)

this animation was only experimental, as i'm sure theres a better way to animate things, but it seems like it works for now?
i'm just skeptical, and wondering if this way of doing stuff will become really inefficient as soon as i'm animating lots of surfaces

cobalt lanternBOT
#

@storm kiln

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.

teal helm
#

Don't load things in the main loop, and don't rely on frame count as they can have an inconsistent timing

#

I suggest using a list for surfaces that you load before the main loop, and a timer

storm kiln
fleet basin
#

I would say this is perfectly valid. Though for real usage you'd want to make a class representing an animated sprite, so that you can just .draw it and it'd automatically blit the right frame.

storm kiln
teal helm
#

something like

surfaces = [pygame.load.image("image1.png"), pygame.load.image("image2.png"), ...]
frame_duration = 300 # ms
index = 0
sum_dt = 0

while True:
   dt = clock.tick(FPS)
   sum_dt += dt # time since last frame change
   if sum_dt >= frame_duration: # change frame
      index += 1
      index %= len(surfaces)
      sum_dt -= frame_duration # or reset to 0
   surface = surfaces[index]
   screen.blit(surface)
#

With this, you can handle any number of frame in the animation

#

and you can integreate it easily in a class

#

With something like


class Animation:

  def __init__(self, paths: list[str] frame_duration: int = 300):
    self.surfaces = [pygame.load.image(path) for path in paths]
    self.frame_duration = frame_duration
    self.index = 0
    self.sum_dt = 0

  def update(dt):
    self.sum_dt += dt # time since last frame change
    if self.sum_dt >= self.frame_duration: # change frame
      self.index += 1
      self.index %= len(self.surfaces) # ensure you cycle the list, can also use itertools.cycle
      self.sum_dt -= self.frame_duration # or reset to 0

  def get_current(self) -> Surface:
    return self.surfaces[self.index]
storm kiln
#

i keep seeing the

self.

everywhere, is that a class thing?

teal helm
#

it is

storm kiln
#

ok

teal helm
#

a class is like a type

#

like int or float

storm kiln
#

i might need to touch down on some more python stuff before i try to do anything more complicated ;-;

teal helm
#

you create instances of the class by calling
Animation(...) with the good argument (of couse it depends on the name of the class)

storm kiln
teal helm
#

then you get an object whose type is the class you built

#

and in the class definition, you use self to refer to the object itself

#

class A:
   
   def __init__(self, arg):
      self.arg = arg

   def print_arg(self):
      print(self.arg)

a1 = A(18)
a2 = A("Hello World")

here, a1 and a2 are objects of type A, but a1's arg is 18 while a2's arg is "Hello World"

#

self.arg is an attribute of the class, so like a value it stores with the name arg, and print_arg is a method, so a function that will apply to it. All methods need self as first parameter bc that's how python works

#

__init__ is a special method that is called when you create the object of that type

storm kiln
#

hmmm

storm kiln
#

thanks for the help, i'm just learning more about classes and stuff right now so i can better understand what i've been told

cobalt lanternBOT
#
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.