How To Create A Sprite

This beginner level how to explains how to load and draw a sprite to the game window in SwinGame.

howtocreateasprite

Level

Intermediate

Read these first

How To Draw A Bitmap

Details

Sprites in essence are images. In SwinGame, turning your image into a sprite allows you to apply procedures and functions that can handle collisions, moving, animation and much more. The Sprites API explains these procedures in greater detail. 

To create a Sprite you need to declare a variable of a Sprite type. Next you will need to LoadBitmapNamed into the project. Your next step will be to create the sprite, linking the bitmap you loaded with your Sprite variable. The Source code below shows how this performed.



Source Code Widget

  • program HowToCreateASprite;
    uses SwinGame,sgTypes;
    
    procedure Main();
    var
        ball : Sprite;
    begin
        OpenGraphicsWindow('Create a Sprite' ,150 ,150 );
        ClearScreen(ColorWhite );
        LoadBitmapNamed('ball' ,'ball_small.png' );
        ball := CreateSprite(BitmapNamed('ball' )  );
        SpriteSetX(ball ,60 );
        SpriteSetY(ball ,60 );
        DrawSprite(ball );
        UpdateSprite(ball );
        RefreshScreen();
        Delay(5000 );
        FreeSprite(ball );
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        sprite  ball;
        open_graphics_window("Create a Sprite" ,150 ,150 );
        clear_screen(color_white );
        load_bitmap_named("ball" ,"ball_small.png" );
        ball = create_sprite(bitmap_named("ball" ) ) ;
        sprite_set_x(ball ,60 );
        sprite_set_y(ball ,60 );
        draw_sprite(ball );
        update_sprite(ball );
        refresh_screen();
        delay(5000 );
        free_sprite(ball );
        release_all_resources();
        return 0;
    }
    
    Download Now"
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon


Possibilities

  • Try moving your sprite