Skip to content
Check out how to use the Sprites functions!

Sprites

Simple Usage Examples

Create Sprite

Creating a Player Sprite

The following code is an example of using Create Sprite to create a sprite of a 2D game character and draw it on a SplashKit window.

#include "splashkit.h"
int main()
{
window start = open_window("create_sprite", 600, 600);
// Load the bitmap for creating a sprite
bitmap player = load_bitmap("player_bitmap", "player.png");
// Create the player sprite
sprite player_sprite = create_sprite(player);
sprite_set_x(player_sprite, 300);
sprite_set_y(player_sprite, 300);
clear_screen(COLOR_BLACK);
draw_sprite(player_sprite);
refresh_screen();
delay(5000);
close_all_windows();
return 0;
}

Output:

create_sprite-1-simple example


Draw Sprite

Drawing a Player Sprite

The following code is an example of using Draw Sprite to create a sprite of a 2D game character and draw it on a SplashKit window.

#include "splashkit.h"
int main()
{
window start = open_window("draw_sprite", 600, 600);
load_bitmap("player", "player.png");
sprite player_sprite = create_sprite(bitmap_named("player"));
sprite_set_x(player_sprite, 300);
sprite_set_y(player_sprite, 300);
clear_screen(COLOR_BLACK);
// Draw the sprite
draw_sprite(player_sprite);
refresh_screen();
delay(5000);
close_all_windows();
return 0;
}

Output:

draw_sprite-1-simple example


Free Sprite

Freeing a Sprite

The following code is an example of using Free Sprite to free a sprite when the UP key is typed and after the window is closed.

#include "splashkit.h"
int main()
{
open_window("free_sprite", 600, 600);
bitmap player = load_bitmap("player", "player.png");
sprite player_sprite = create_sprite(bitmap_named("player"));
sprite_set_x(player_sprite, 300);
sprite_set_y(player_sprite, 300);
bool sprite_exists = true; // Track if the sprite exists
while (!quit_requested())
{
process_events();
clear_screen(COLOR_BLACK);
if (sprite_exists)
{
draw_sprite(player_sprite);
update_sprite(player_sprite);
}
refresh_screen();
// If UP key is typed, the sprite is removed
if (sprite_exists && key_typed(UP_KEY))
{
free_sprite(player_sprite);
sprite_exists = false; // Set bool to false to stop drawing/updating
}
}
// Clean up
if (sprite_exists)
{
free_sprite(player_sprite);
}
close_all_windows();
return 0;
}

Output:

free_sprite-1-simple example


Sprite Set Position

Setting Sprite Position

The following code is an example of using Sprite Set Position to set a new location of a Sprite on a SplashKit window.

#include "splashkit.h"
int main()
{
open_window("sprite_set_position", 600, 600);
load_bitmap("player", "player-run.png");
sprite player_sprite = create_sprite(bitmap_named("player"));
sprite_set_x(player_sprite, 200);
sprite_set_y(player_sprite, 300);
clear_screen(COLOR_BLACK);
draw_sprite(player_sprite);
refresh_screen();
delay(2000);
// Create point_2d object which stores the new coordinates
point_2d pos = point_at(450, 300);
// Set the new sprite position
sprite_set_position(player_sprite, pos);
clear_screen(COLOR_BLACK);
draw_sprite(player_sprite);
refresh_screen();
delay(5000);
close_all_windows();
return 0;
}

Output:

sprite_set_position-1-simple example


Sprite Set Velocity

Setting Velocity of Sprite

The following code is an example of using Sprite Set Velocity to set a small horizontal velocity (moving to the right) for a sprite on a window.

#include "splashkit.h"
int main()
{
open_window("sprite-set-velocity", 600, 600);
load_bitmap("player", "player-run.png");
sprite player_sprite = create_sprite(bitmap_named("player"));
sprite_set_x(player_sprite, 300);
sprite_set_y(player_sprite, 300);
// Create vector for sprite's velocity
vector_2d vel = vector_to(0.01,0);
while (!quit_requested())
{
process_events();
// Set sprite velocity and update sprite
sprite_set_velocity(player_sprite, vel);
update_sprite(player_sprite);
clear_screen(COLOR_BLACK);
draw_sprite(player_sprite);
refresh_screen();
}
close_all_windows();
return 0;
}

Output:

sprite_set_velocity-1-simple example


Sprite Set X

Setting Sprite x-coordinate

The following code is an example of using Sprite Set X which sets the x-coordinate of a Sprite on a SplashKit window.

#include "splashkit.h"
int main()
{
open_window("sprite_set_x", 600, 600);
load_bitmap("player", "player-run.png");
sprite player_sprite = create_sprite(bitmap_named("player"));
// Setting the x coordinate in refrence to the window
sprite_set_x(player_sprite, 300);
clear_screen(COLOR_BLACK);
draw_sprite(player_sprite);
refresh_screen();
delay(5000);
close_all_windows();
return 0;
}

Output:

sprite_set_x-1-simple example


Sprite Set Y

Setting Sprite y-coordinate

The following code is an example of using Sprite Set Y which sets the y-coordinate of a Sprite on a SplashKit window.

#include "splashkit.h"
int main()
{
open_window("sprite_set_x", 600, 600);
load_bitmap("player", "player-run.png");
sprite player_sprite = create_sprite(bitmap_named("player"));
// Setting the y coordinate in refrence to the window
sprite_set_y(player_sprite, 300);
clear_screen(COLOR_BLACK);
draw_sprite(player_sprite);
refresh_screen();
delay(5000);
close_all_windows();
return 0;
}

Output:

sprite_set_y-1-simple example