How To Use Animation With Multiple Sprites

This article will show you how to move multiple sprites with animation at the same time on the game screen. In most games it is important to show multiple animated sprites on the screen at the same time.

Level

Advanced

Details

SwinGame allows you to create and draw as many sprites as you want on the screen. To make an animation out of it, you need to follow these steps:

  1. Create two different sprites (Create Sprite) and place them in different positions (Sprite Set X and Sprite Set Y) of the screen
  2. Start both animations (Sprite Start Animation)
  3. In game loop update (Update Sprite) and draw (Draw Sprite) the sprites followed by refresh the screen (Refresh Screen)

The code below shows a small program that demonstrates all these steps in making an animation with two sprites. Similarly it is possible to make lots of animation with different sprites simulteniously from one script or more.

Source Code Widget

  • program HowToUseAnimationWithMultipleSprites;
    uses SwinGame,sgTypes;
    
    procedure Main();
    var
        myLizard : Sprite;
        myFrog : Sprite;
    begin
        OpenAudio();
        OpenGraphicsWindow('Dance' ,800 ,600 );
        LoadResourceBundle('dance_bundle.txt' );
        myFrog := CreateSprite(BitmapNamed('FrogBmp' )  ,AnimationScriptNamed('WalkingScript' )  );
        SpriteStartAnimation(myFrog ,'Dance' );
        SpriteSetX(myFrog ,496 );
        SpriteSetY(myFrog ,250 );
        myLizard := CreateSprite(BitmapNamed('LizardBmp' )  ,AnimationScriptNamed('WalkingScript' )  );
        SpriteStartAnimation(myLizard ,'Dance' );
        SpriteSetX(myLizard ,238 );
        SpriteSetY(myLizard ,272 );
        repeat
            ClearScreen(ColorWhite );
            DrawSprite(myFrog );
            DrawSprite(myLizard );
            RefreshScreen(60 );
            UpdateSprite(myFrog );
            UpdateSprite(myLizard );
            ProcessEvents();
        
        until WindowCloseRequested();
        FreeSprite(myLizard );
        FreeSprite(myFrog );
        CloseAudio();
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        sprite  my_lizard;
        sprite  my_frog;
        open_audio();
        open_graphics_window("Dance" ,800 ,600 );
        load_resource_bundle("dance_bundle.txt" );
        my_frog = create_sprite(bitmap_named("FrogBmp" ) ,animation_script_named("WalkingScript" ) ) ;
        sprite_start_animation(my_frog ,"Dance" );
        sprite_set_x(my_frog ,496 );
        sprite_set_y(my_frog ,250 );
        my_lizard = create_sprite(bitmap_named("LizardBmp" ) ,animation_script_named("WalkingScript" ) ) ;
        sprite_start_animation(my_lizard ,"Dance" );
        sprite_set_x(my_lizard ,238 );
        sprite_set_y(my_lizard ,272 );
        do 
        {
            clear_screen(color_white );
            draw_sprite(my_frog );
            draw_sprite(my_lizard );
            refresh_screen(60 );
            update_sprite(my_frog );
            update_sprite(my_lizard );
            process_events();
        }
        while ( ! (window_close_requested() ) );
        free_sprite(my_lizard );
        free_sprite(my_frog );
        close_audio();
        release_all_resources();
        return 0;
    }
    
    Download Now"
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon

If there is a useful hint you will find it in a box like this

Possibilities

Suggested projects will appear here.