Playing around with the current game.

A quick compile, and when I say quick, it takes about 3-5 seconds. I run the example game on my favourite emulator and I am presented with moving enemies, that does not kill me, but I can kill the enemies, I can jump, and the jump has a gravity effect and I can move from screen to screen. The sprites are smooth and fast with no flicker. This is 4 enemy sprites and my main character (DogMole in this case).

This, of course, has to be all modified as it is not exactly what I need in this game. I want to move freely around the screen, I do not want any gravity effect, so jump has to be removed. Also, there is no down, so that has to be added. Left and right go from screen to screen, I don’t need that as each level will be a procedurally generated maze. I want diagonals to works as well. Navigation should also continue after releasing the direction, finally, there should be some sort of drag so the character will eventually stop. Of course, the player may not immediately realize that because they will be chased by the enemy (until the enemy is all gone.

So browsing around the code, the movement part is.

vx+= ax;
x+= vx;
if( vx+8>>3 )
ax= -vx>>3;
else
ax= vx= 0;
if( (unsigned int)x > scrw0 )
if( mapx < mapw-1 )
x= 0,
mapx++,
update_screen();
else
x= scrw<<12,
vx= 0;
else if( mapx )
x= scrw<>8;
if( vy>maxvy )
vy= maxvy;
else
vy+= ay+gconst;
if( (unsigned int)y <= 15<
y+= vy;
else
vy= 0,
y= 15<>8;
// motion of the protagonist
if( Input() & 0x01 )
ax= vx
else if( Input() & 0x02 )
ax= vx>-maxvx ? -40 : 0;
if( Input() & 0x08 )
{
if( (unsigned int)y == 15<
vy= -800;
}
if( Input() & 0x10 && !spacepressed && num_bullets
{ // Space
Sound(EFFX, 0);
bullets[num_bullets].x= sprites[0].x;
bullets[num_bullets].y= sprites[0].y;
i= Input() & 0x0f;
dirbul[num_bullets]= i ? i : 1;
num_bullets++;
}
spacepressed= Input() & 0x10;

Of course, everyone writes code differently and with a different style. Of course, none of this makes a darn difference to the compiler, but for my readability and understanding, I like to format things to make it easier for me to read. I also like to break chunks of code up into brackets. Just my way of understanding things.

So with that in mind, I am going to proceed to do that so I can understand the code better.

/horizontal movement
//calculation of player X axis
{
vx += ax;
x += vx;
if( vx + 8 >> 3 )
{
ax = -vx >> 3;
}
else
{
ax = vx = 0;
}
}
sprites[0].x = x >> 8;

This section seems to control the horizontal movement, and of course, I commented as such.
Since the vertical movement is not what I want, I am copying how the horizontal movement works, substituting the ax, ay, and x variables with the Y equivalent.

//vertical movement
//calculation of player Y axis
{
vy += ay;
y += vy;
if( vy + 8 >> 3 )
{
ay = -vy >> 3;
}
else
{
ay = vy = 0;
}
}
sprites[0].y = y >> 8;

Well, main.c is starting to get a bit bloated, it’s time to start moving the player movement function to a separate header and create a function.
So, let’s create a header file called player.h and create a function.

void player_handler(void)
{
}

and let’s move the code to that function.

void player_handler(void)
{
{
//horizontal movement
vx += ax;
x += vx;
//vertical movement
vy += ay;
y += vy;
//bitshift 3 positions = >>3 = /8
//calculation of player X axis
if( vx + 8 >> 3 )
{
ax = -vx >> 3;
}
else
{
ax = vx = 0;
}
//bitshift 3 positions = >>3 = /8
//calculation of player Y axis
if( vy + 8 >> 3 )
{
ay = -vy >> 3;
}
else
{
ay = vy = 0;
}
sprites[0].x = x >> 8;
sprites[0].y = y >> 8;
}
// motion of the protagonist
if( Input() & 0x01 ) // P - go right
{
ax = (vx  -maxvx) ? -40 : 0;
}
//now we have down doing the same as up for testing
if( Input() & 0x04 )// A go down
{
ay = (vy  -maxvy) ? -40 : -1;
}
//fire
if( Input() & 0x10 && !spacepressed && num_bullets
{ // Space (FIRE)
Sound(EFFX, 0);
bullets[num_bullets].x= sprites[0].x;
bullets[num_bullets].y= sprites[0].y;
i= Input() & 0x0f;
dirbul[num_bullets]= i ? i : 1;
num_bullets++;
}
spacepressed= Input() & 0x10;
}

We will call this from our main loop as

player_handler();

Compile and testing show the function works the same, and now my main loop is certainly cleaner.

I’m going to isolate and clean up the other bits of code in the main loop for enemy movement and bullets as well.
Performing this call seems to work seamlessly and cleans up my main loop.

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