Refresh your game with a sprite!

Now is the time to spice up your game knowledge and get into something really cool: Sprites. Remember the old days of the Nintendo (NES) or the Super Nintendo (SNES) video game consoles? Most games you played back then were using sprites. Sprites are basically just an image you can move around and animate. In PPL sprites are pretty advanced. You can stretch them, tint them, tile them, automatically animate them, and so much more...

Let’s first start by loading an image from disk as a sprite:

MySprite$ = LoadSprite(AppPath$ + "mysprite.bmp", G_RGB(255, 0, 255), 4, 150, NULL);

You always need to retrieve the sprite handle from the LoadSprite() function to be able to access it later on. The sprite handle is just a unique integer value.

The first parameter of the LoadSprite() function is the pathname of the image you wish to use. The image file can be a bitmap (.bmp), a jpeg (.jpg), a Portable Image (.png) or a gif (.gif). The image file can be made of multiple images grouped together in one image file.

The second parameter is the transparent color to use. Sprites can be drawn on the screen with a transparent background to make them blend with the scenery.

The third parameter is the number of frames (images) the image file has. The images must be sequential (one after the other) horizontally within the file, and they all must be the same width and height in pixels.

The fourth parameter is the speed in milliseconds at which the animation will be played. The default animation will swap between each frame one after the other from the left to the right.

The last parameter is the sprite procedure to use for the sprite. We will get into more advanced sprite handling in a later article.

The image is by default visible on the screen at position (0, 0). You can hide or show the sprite using the following:

DelSpriteOption(MySprite$, SO_VISIBLE); // Hide the sprite by removing the SO_VISIBLE flag from the sprite's options.
AddSpriteOption(MySprite$, SO_VISIBLE); // Show the sprite by adding the SO_VISIBLE flag to the sprite's options.

Each sprite has a series of special options that can be removed or added at any time.

Now let's move our sprite on the screen, to move the sprite all you need to do is call the MoveSprite() function and pass a new coordinate. In our case we will move the sprite to where the stylus touches the screen.

In the MainProc of our code we will add a WM_LBUTTONDOWN event that will be triggered whenever the stylus touches the screen:

WM_LBUTTONDOWN:
MoveSprite (MySprite$, wParam$ - (SpriteWidth(MySprite$) / 2), lParam$ - (SpriteHeight(MySprite$) / 2));

Here we center the sprite MySprite$ - which we should have made global at the time of loading (LoadSprite) - around the stylus position. SpriteWidth() returns the width in pixels of the sprite and SpriteHeight() return its height in pixels.

The WM_LBUTTONDOWN uses the wParam$ variable to store the X coordinate position and the lParam$ variable for the Y coordinate position the stylus was pointing to. You can write code for the following events:

WM_LBUTTONDOWN: The stylus is pressed.
WM_LBUTTONUP: The stylus is released from the screen.
WM_MOUSEMOVE: The stylus is being moved around while pressed.

You can stretch a sprite's display by doing the following:

SetSpriteWidth (MySprite$, 100); // Makes the sprite 100 pixels wide.
SetSpriteHeight (MySprite$, 200); // Makes the sprite 200 pixels high.

You can change the animation speed and sequence of a sprite, lets say you have frames for jumping at frame 6 to 10:

SetSpriteFrames (MySprite$, 6, 10, 250, true);

This will set the current animation for MySprite$ from frames 6 to 10, animating at 250 milliseconds. The last parameter specifies if PPL should wait for the current animation timer to expire before going to the new animation frames or change right away.