Trending
Opinion: How will Project 2025 impact game developers?
The Heritage Foundation's manifesto for the possible next administration could do great harm to many, including large portions of the game development community.
Coding enemies is a lot like cooking. Answering the question of "How should this enemy behave?" is similar to answering "What should this sandwich taste like?" For both questions, the secret is in the ingredients.
Coding enemies is a lot like cooking. Answering the question of "How should this enemy behave?" is similar to answering "What should this sandwich taste like?" For both questions, the secret is in the ingredients.
This is a chicken. It is a creature lovingly and internally referred to as a Thwomp, like the enemy of the same name from the beloved Super Mario Bros. It will probably seem edible to some players, and I must admit it has made me rather hungry sometimes.
Most of the time, it just hangs in the air, and it will stay there as long as we can control our hunger-pangs.
But sometimes, this happens.
Oops, this can be dangerous. Let's see how these monsters work.
Let's start with the basics. We will make a list, which will consist of all possible actions of the chicken. Our chicken should be able to:
Wait for its victim in the air
Attack the victim
Rise to the starting position
We have several sprites that animate the chicken. Let's output these sprites as a sequence:
spr=oam_meta_spr(chickenX, chickenY, spr,
seqence_chicken_idle[frame]);
Now our chicken can hang in the air. Look at the first image again to see his delicious turkey jive.
Suppose our chicken has spotted prey. Now it needs to rush to the enemy, and try to squash it. Perhaps it will also stain you with fat in your game. It all depends on your imagination.
All we need to do is change the animation and increase the position of the chicken along the Y axis each frame. I think 4 pixels per frame will be ok, but you can experiment on your own.
chickenY+=4.
But something has to stop the chicken. Otherwise, it will fly through the entire screen like an asteroid in a looping way, and this will not make any sense, so you have to add a condition that will limit attack. This may be the presence of the enemy or the floor under the chicken.
Stopping it might look like this (I will use pseudocode):
if (chickenY < someObjectY)
chickenY+=4;
Then we have to get the chicken back. We will raise it by 1 pixel for each frame.
if (chickenY > basicPositionY)
--chickenY;
And now we will collect everything in one cycle:
while (gameLoop) {
switch (chickenStatus) {
CHICKEN_IDLE:
// print sprite
spr=oam_meta_spr(chickenX, chickenY, spr,
seqence_chicken_idle[frame]);
// Do not forget about the condition sufficient to start the attack:
if (playerX==chickenX)
chickenStatus=CHICKEN_FALL;
break;
// next state - attack
CHICKEN_FALL:
spr=oam_meta_spr(chickenX, chickenY, spr,
seqence_chicken_fall[frame]);
// maybe there is some ground here
if (isGround(chickenY))
// and if so
chickenStatus=CHICKEN_BACK;
else
chickenY+=4;
break;
// and back over again:
CHICKEN_BACK:
spr=oam_meta_spr(chickenX, chickenY, spr,
seqence_chicken_back[frame])
// if Y = basic position Y
if (chickenY==basicPosY)
// back it to idle mode
chickenStatus=CHICKEN_IDLE;
else
--chickenY;
break;
}
}
Let's consider another enemy. It’s a ghost. Our character cannot destroy this ghost on his own, but he can use his own supernatural thermokinesis ability to light a candle. This will destroy the specter.
Again, we need to provide states for this enemy - the ingredients of his behavior:
Ghost patrols the platform;
Ghost attacks the player if the distance between the player and the ghost is 4 cells or less;
Ghost dies if there is a lit candle within two cells.
This same process can also be performed with bosses. Just describe how the boss should behave. It could be something like this:
Hunting - Boss patrols platforms at the same height as the player
Attack - Boss throws a hatchet at the player
Use telekinesis to throw the cleaver back!
Coding enemies and populating your game with dangerous, fun, and tasty creatures is possible if you skillfully use the state and parameters of objects. Try it yourself!
– – –
Want more tips straight from the retro pros? Welcome to our Discord!
Get mad meat on The Meating page now!
You May Also Like