I’m feeling a movement part 4 – Hurry up and slow it down

12-31-2017
As we last stood, our enemy sprites, move at one speed toward the player. However, most games will over time start to speed up the enemy movements. Doing this is rather simple, but it needs to be done in order to increasingly change the difficulty.

We don’t really want to change how the main game loop runs, what we want to concentrate on is how the enemies move. Quick hint, it will not be with a pause statement of any sort, that will just freeze everything with each run of the loop.

What we want to do is vary the number of times that the enemy is called to pursue you.

Let’s start off with some global variables.

char enemySlowDown = 0;
char enemySpeedUp = 20;

I’m sure I mentioned this before, one of the nice things about higher level languages is that you can make a variable name rather descriptive. There is no penalty for doing this as the compiler will ignore your long names and do something more assembler friendly. There should not be much of an excuse for not using a real descriptive variable name.

You will also notice that instead of unsigned char, I decided to use char, which is the same as saying signed char. The reasoning is quite straightforward, in signed char, you can go into the negatives, whereas unsigned char, you cannot, much like an absolute value.

This will come in important when you have changed the enemySpeed up to 0. When you subtract one more from 0, in signed char, you will get -1, whereas, in unsigned char, you will get a 1. We want to have the enemySpeedUp variable to stay at 0, when it hits that point. Can you do it with an unsigned char, yes you can, but it’s slightly more difficult and there is no reason to do it that way.

In a prior blog, we had written about reading the game array, so that we can mark the obstacles. I had also written a blog on pacing your main character. These are bot functions called by the main loop. They will be called every time a new level starts. Sounds like a perfect place to change up our level speed.

The section in the main loop looks kinda like this

//we need to read level into array
if (isLevelRead == 0)
{
    //readScreenTiles(x1,y1,tileAttribute);// still broken and will eventually fix.			
    //now that we have read our tiles, we need to select our starting level
    levelPositioning[2] = ((short)level);//recast level to fit properly in array
    startingPosition((short)levelPositioning);//call the function				
    x = levelPositioning[0];
    y = levelPositioning[1];
				
    enemyStart(level);
		
    isLevelRead = 1;//level has now been read

    EnemySlowDown = 0;
    enemySpeedUp --;    
    if (enemySpeedUp < 0) enemySpeedUp = 0;
    //this assures that the enemySpeedUp does not venture below 0
}

The last three statements are what we are using to adjust our level speed. Whenever the isLevelRead equals 0 (which only happens once per level), we set the EnemySlowDown variable to 0, we then will reduce the enemySpeedUp variable. That is until we hit 0. 0 would then be the max speed of the enemy sprites.

This is only a change of the speed level, but it’s not where the speed up actually occurs. That happens next.

if (enemySlowDown == 0)
{
    enemyAI();
}
enemySlowDown ++;
if (enemySlowDown == enemySpeedUp) enemySlowDown = 0;
//enemySpeedUp will count down with each new level 

The logic behind this is really simple. When the enemySlowDown variable equates to 0, then we will run the enemyAI() function. We will then immediately increase the enemySlowDown variable by one, which during the next loop will not call the enamyAI() function.

The next statement will be that if the enemySlowDown and enemySpeedUp match, then we start the counter over again at 0, which moves your enemy. Remember that enemySpeedUp is read and decreased when the level is read into the array from earlier. So as a result as the two numbers get nearer to one another, the more times your enemyAI() function is called. The end result: your enemies will move faster toward you.

Happy New Year’s eve.  Don’t celebrate too hard, you’ll still have to deal with the hangover the next morning.

Advertisements

Author: andydansby

I'm a hobbyist coder working with the ZX Spectrum. Living in New York state near the Syracuse area. I grew up in Virgina. The first computer my parents bought for me was a Timex Sinclair 2068.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s