Log In  


Hi,
I'm working on some kind of story game, where you start animations with the X/O buttons.
(in the cart, you can use left right to change the animation-loop, index 0 is "idle", index over 2 crash the game)

My Question is: Is there a common way to do move whole sprites around?

Tutorials I found are all about one sprite animation and not how to how to move the sprite in a loop, instead of players input via buttons. Might not know the search term.

I use an array of 8 frames that will manipulated a sspr() command.
The 8. index sets a new sprite-area, after the other frames are used to move the guy around.

foo bar = {{2,5}, 		{7,18},	{7,27},	{8,27}, 	{9,27},	{10,27}, {11,28},	{11,28,	18,80, 4,4, 0,0, 4,4, true,false}  }
sspr(	sx,sy,				sw,sh,				foobar[1][1],foobar[1][2],						[dw,dh,flip_x,flip_y] ))

animation_box = {
									{ {2,5}, 		{7,18},	{7,27},	{8,27}, 	{9,27},	{10,27}, {11,28},	{11,28,	18,80, 4,4, 0,0, 4,4, true,false}  },
									{ {11,28},	{11,30},  {11,36},	{11,34},	{11,32},	{11,30}, {15,30},	{11,28,	18,80, 4,4, 0,0, 4,4, false,false}  }
								}
animation_frame = 0
wm = {1,	{ 16,80,	 2, 4	,   0,0,   2,4,false,false}	}

        p = animation_box[1]
	animation_frame +=1
	wm[2][5],wm[2][6] = p[animation_frame][1], p[animation_frame][2]

sspr(unpack(wm))

Cart #hifopejdi-1 | 2026-10-03 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA



If I'm following you, idk if it's standard and really depends on how complicated you want to make it.

What I did for my game was to define a set of animation patterns (really just functions run by co-routines) that would execute over time. The specific animation pattern would get randomly selected as the player enters a room for certain sprites and then for others (like torch light), would just animate through a set of frames.

For sprites that say "moved back and forth", it would just run a routine that would increment it's x up to a specific threshold, and then reverse it. Code snippet for the definition of the pattern

 -- left/right
 left_right_pattern = {
  function()
   animate("s_col_x",1,minion_pickups)
  end,
  function()
   animate("s_col_x",-1,minion_pickups)
  end     
 }

The sprites that had the animation patterns applied would get added to a table for the room upon entering, and removed on exit. Each sprite tracked is own position, and frame if needed, and the specific co-routines would apply changes to each sprite and then yield back to the main game loop.

The co-routines that executed the animation patterns would get run by the normal PICO8 game loop as long as I kept them running (is. coresume..)

If you don't need that level of complexity, the same kind of effect can be done without co-routines, the application really just needs to track how quickly (framerate) you want the changes to be applied, and some logic about how to move the sprite (i.e back and forth, up and down, etc.)

HTH,
Mike


Thanks for your post,

I heared of co-routines, but this might be to much right now for what I have in mind for my game? Not sure, right now I 'm working with a simple game state structure and only have code that would run on a potatoe, I hope.
My question is more of a beginner question.

The "problem" I have is with this specific animation pattern and and some logic about how to move the sprite (i.e back and forth, up and down, etc.)

I want some kind of gif-loops, but if you press a button the animation loop changes.

Of course if I move just up/down right/left I could use,

sspr(minion[1], minion.[2]) and a function that manipulate this table  of minion = {1,0} 
function foo
 minion[1]+=1*direction
 if(minion[1] > 60) direction *=-1 --turns 1 into -1
 if(mininon[1] < 0) direction = 0  -- stops 
[...]
end

and a bntp() resets the whole position to a new start.

But if the guy moves in a more random pattern, would I use tables to store these positions?

I want something that is like
spr( unpack( animation[animation_nr][frame_of_animation] )) where i have this table with sprite_nr, spr_x,spr_y

animation =
 {  {1,1,0}, {1,2,0},{1,2,2},{1,2,4},{1,2,6},{1,2,8},{1,4,10}  },
 {  {1,2,0}, {1,4,0}, {1,6,0}, {1,8,0}, {1,10,0}, {1,12,0}, {1,14,0} },
 {  {1,2,2}, {1,4,4}, {1,6,2}, {1,4,0}, {1,2,0}, {1,2,4}, {1,2,0} }

Sure! Sounds like you're on the right path. I think you're approach can work with fixed positions and yes a table would work. Some reasons fixed positions might be problematic in the future are:

  • The game has obstacles that may collide with those positions
  • You decide you want some more specific behaviors in which case may be harder to model in specific coordinates.
  • You want different behaviors for different sprites
  • You want to support different display sizes, etc.

But really it depends on what your going for, and it's certainly okay to go with a more simplified approach that works for your game. If you're going for an implementation that slowly moves the character to each position in the table, then you likely want to lookup LERP which is movement over time to a specific position. It's not random but would allow your table of random coordinates to be moved to smoothly.

If you want a coordinates or path approach, the table could be relative positions from a starting point ( starting at 1,1 move 5 right, then down 5, etc) might be more flexible. If you want random sets of x,y you could just do flr(rnd(127)+1 for x and y.

This video has a good example of LERP (though it's about co-routines, there is some good animation info in side) https://www.youtube.com/watch?v=tfGmjB72t0o&t=387s

As for the loops and how to move them. The loop is just the game loop itself (i.e _update > _draw).
In my case I'm always tracking an "active pattern", that changes when the player enters a room (or in this case a btn is pressed). This effectively means a new function is applied to each sprite to change it's position in some way until the pattern changes.

Pseudo code:

def _init()
 sprite = {
  x = 0,
  y = 0
 }

 sprites = {sprite}

 // [x,y]
 animation_positions_left_right = {
   [0,0],
   [127,0]
 }

 // [x,y]
 animation_positions_up_down = {
   [0,0],
   [0,127]
 }

 current_position = 1

 active_pattern = animation_positions_left_right

end

def _update()

  if btnp() then
   if active_pattern == animation_positions_left_right then
      active_pattern == animation_positions_up_down
   else
      active_pattern == animation_positions_up_down
   end

  end

  move_sprites_one(sprites, active_pattern, current_position)

  if current_position <= #active_pattern then
   current_position++;
  else
   current_position--;
  end
end

def _draw()
  draw sprite at sprite.x,sprite.y
end

def move_sprites_one(sprites, pattern, current_position)
  // Need a slowdown here of some sort as it will move quickly to each position (maybe LERP)
  for s in all(sprites) do
    s.x = pattern[current_position].x
    s.y = pattern[current_position].y
  end
end

def move_sprites_two(...)
end

HTH,
Mike



[Please log in to post a comment]