SGDK: Adding Enemies
Adding Enemies Tutorial base.zip – A base template to follow along with the Adding Enemies tutorial via the Pigsy’s Retro Dev Tutorial video .
Adding Enemies Tutorial Complete.zip – The completed project for you to use as a comparison once you’re done with the tutorial.
Fly and Snail enemy sprites from Little Nemo the Dream Master for NES. Feel free to pull assets from other tutorials on the site to increase your enemy assets.
Note: This tutorial is done with SGDK version 2.0. The video tutorial uses a newer version.
The steps for this are nearly identical to what we did in the Header File tutorial. Only this time we’re adding a resource file named res_enemies.res and a folder named enemy_assets. Naturally, the snail and fly png files go in the enemy_assets folder.
Generating the res_enemies.res file should generate a corresponding res_enemies.h file once you’ve declared your assets in the .res file and done a save/clean/compile. If the /s/c/c/ doesn’t create that corresponding res_enemies.h file then simply copy the format from your res_player.h file but update it to match your declarations for your enemy assets rather than your player assets.
My res_enemies.h file looked like this but yours may be slightly different.

As a reminder, the format for declaring your assets in the
.res file for a SPRITE looks like this.
SPRITE [identifier] [“path.file extension” for the
asset] [width] [height] [compression type] [animation speed]
Your Explorer bar should look like this.

To save you a little bit of trouble, size of fly sprite is 4 x 4 and snail sprite is 4 x 3. Furthermore, I’ve used animation values of 6 and 8 respectively. With that said, go ahead and attempt to declare your sprites in the res_enemies.res file yourself and see if you do it properly.
Your declaration may look a tad different from what I’ve used but it should look similar to this.

If you’ve sourced other assets to include in this tutorial that’s fantastic! Pigsy uses a total of 4 enemy assets in his tutorial so by all means aim for that number of assets. Just remember to be mindful of the limited number of palettes in SGDK when picking your assets. There are four palettes in total. PAL0 to PAL3.
The next thing we’re going to do is code the createEnemies() function into the enemies.h and corresponding enemies.c files.
First, we’re going to right-click on the inc
folder and add a file named enemies.h. Then we’re
going to make sure that the header file has the mandatory information in it. We’re
going to reference the player.h file to make sure the
format we’re using for enemies.h is correct
Our sprite declarations are going to be made in our enemies.h file. We’ll also add our function(s) here as
well. So far, here’s what it looks like.

![]()
![]()
Within our enemies.c folder is where we’re going to finish writing the createEnemies() function. Pigsy starts by adding the assets in the function and assigning fixed integer values for the x & y positions of all the individual assets on screen. Thus, this potentially bloats the code so he then introduces the usage of the for() statement.
I’m only using two enemy assets but a for()
statement is still viable. First, we’ll move our Sprite* declarations over from
enemies.h into enemies.c
but making sure to leave out the extern from the enemies.h
file.
Below this, we’re going to declare some more variables to handle sprite position (and also movement). Specifically for the x & y positioning, we’re going to use the following variables:
fix32 enemy_x; and fix32 enemy_y. We’ll also add a fix32_velx that we’ll use later
in the tutorial. To use a for() statement to play my fly sprite at x=50 and my
snail sprite at x=30, I’ll need to have my fix32 enemy_x
variable to make an array for these two objects. Because Pigsy
had 4 enemy assets his array was set to 4. As for my array, it looks like the
following:

We can also use a for() statement for the y coordinates as well hence the enemy_y variable I mentioned above. I used y values of 80 and 40 for the fly and snail enemies. If you’re going to stick with those same values, then your enemy_y array is going to look like this:
![]()
Once you’ve made those declarations, it’s time to write the void createEnemies() function. To start, we’ll use PAL_setPalette(); using PAL2. If you were to look in Aesprite/LibreSprite, you’ll notice that the palette used for the Nemo (player), fly, and snail (enemy) sprites are identical. Ideally, you would try and keep color management in mind when making your game but for this tutorial to be more in-line with Pigsy’s tutorial I’m keeping everything on PAL2. As an experiment, see what happens when you try to declare PAL3 using what was referenced in the player.h lesson and see what happens. Can you get it to work? Another lesson for another time, perhaps?
The sprite we’re going to call for creating PAL2 is the
snail sprite and the memory access we’re using is DMA. If you need help remembering how to fill out
the various SGDK functions, you can always hover your mouse over the function and it should provide you with an explanation.
Below is what you should see when hovering the mouse over the PAL_setPalette() function.
void PAL_setPalette(u16 numPal,
const u16 *pal, TransferMethod tm)
Set a complete palette (16 colors) into CRAM.
Parameters:
numPal – Palette number: PAL0, PAL1, PAL2 or
PAL3
pal – Source palette.
tm – Transfer method.<br> Accepted values
are:<br> - CPU<br>
- DMA<br> - DMA_QUEUE<br>
- DMA_QUEUE_COPY<br>
Once we’ve finished filling out PAL_setPalette(),
we’ll want to declare our sprites using the SPR_addSprite()
function. Note: Due to use using integers of enemy_x and enemy_y rather than
fixed values, we’ll need to make use of fix32ToInt(). Furthermore, because
we’re using an array, we’ll need to add the [ ] brackets to indicate the usage
of the array we declared earlier. These brackets are to be added after each
usage of enemy_x and enemy_y.
Since the ordering of my function was to declare the fly first then the snail,
my createEnemies() functions looks
like this.
Now that we have a proper createEnemies() function, we’ll just double-check our main.c file and make sure it gets called in the main() function just below our createPlayer(); function.
Putting the enemy sprite on screen is the first step. Next, we want to write a function that manages how our enemies behave on screen. Therefore, we’re going to write a function called manageEnemies() and add is below our createEnemies() function in the enemies.c file.
Previously, I mentioned the enemy_velx
variable that we would use later. Well, John Cena, the time is now. Unlike John
Cena, who as we all know is invisible, the enemy_velx
variable and corresponding manageEnemies() function
is not. If you haven’t added it as a placeholder variable, you will now add the
fix32 enemy_velx variable as well as add [ ] brackets
to establish an array to create enemy movement.
Note: Positive values move across the screen from left to right. Negative
values move across the screen from right to left. In my tutorial, my enemies
are going to moonwalk moving left to right. Play around with your values and
+/- signs to your heart’s content. If you’re feeling ambitious you can even
reference some other tutorials to practice enemy movement.
As it stands, we’re going to use values of 1.5 and 0.5 for
the fly and snail movement. Using your enemy_x and enemy_y declarations above see if you can write your enemy_velx declaration without assistance.
If you’ve managed to do it correctly, it should look like this.
![]()
Now that we have our enemy_velx
declared with the proper array, we can start writing our manageEnemies()
function.
Jump to enemies.h and ensure that we not only have
our extern in there for the fix32 enemy_velx[2]; Then
we need to at the extern for our void manageEnemies()
function. You can place it below the createEnemies(); function. When done, our enemies.h
file is going to look like this.

Yours might look slightly different but for the most part the format is going to look the same.
Hop back into enemies.c and let’s finish writing this function and wrap this tutorial up. Inside the curly braces {} of our void manageEnemies() function, we’re going to add our for() statement that I mentioned previously.
Withing the () parentheses of the for() statement, we’re going to use for(u8 i = 0; I < 2; i++). This is telling the array to count from 0 and then stop at 2. Next we define the variable[array] we want to use inside the curly braces { } of the for() statement.
For this we’re going to use enemy_x[i] += enemy_velx[i]; If you’ve gone through some of the other tutorials on this site or on the Pigsy/OHSAT pages, you’re probably familiar with using a for() statements and incrementing velocity.
The last thing we need to do is use the SPR_setPosition() functions to handle enemy positioning on screen. The order we’re continuing to use is the fly then the snail. Try seeing if you can use the references in SGDK for SPR_setPosition() to try and fill out the function.
If you’re stuck it’s possible you’re forgetting to use fix32ToInt to convert your fix32 variable to an integer.
When you’re done, our manageEnemies() functions should look like the following:

Sprite positioning functions are typically placed within the while(1) loop as the screen is constantly redrawn when objects move across the screen.
I’ve left some commented code in there regarding player health and a Boolean statement as it’s likely to be used in the upcoming lessons on Pigsy’s YouTube page.
For now, keep coding and good luck.