# Testing charcter placement

12-14-2017

After drawing a blank for the last 2 days (in my defense, I haven’t been up to the silly challenge), I made a bit of a breakthrough on the main character placement.

All due to a silly error.

It seems I am doing my testing for placement in the wrong area.

Let me explain.

The main portion of the game uses variables x and y for a number of things, such as your placement, enemy placement, bullets…

So I am trying to measure the placement of your character’s X and Y placement.

I had earlier made a routine to detect vertical edges, for good or bad the function seems to be working. It really needs reprogramming and that will be done in the future and during a future post, but for now, let’s work with it.

The vertical code looks like this

``````//Check vertical border / edges
void verticalEdge (short vertical[])
{
//mapx is char
//mapy is char
//vertical[0] = vy;
//vertical[1] = ay;
//vertical[2] = y;

short vY1 = vertical[0];
short aY1 = vertical[1];
short Y1 = vertical[2];

vY1 += aY1;
Y1 += vY1;

if( vY1 + 8 >> 3 )
{
aY1 = -vY1 >> 3;
}
else
{
aY1 = vY1 = 0;
}

if( (unsigned int)Y1 > scrh < 0 )
{
if( mapy < maph - 1 )
{
Y1 = scrh << 12;
vY1= 0;
zx_border(3);
}
}

//top
if( vY1 < 0 )
{
if( mapy  scrh)
{
Y1 = scrh << 4;
zx_border(6);
}
}

//returns
vertical[0] = vY1;
vertical[1] = aY1;
vertical[2] = Y1;
}
``````

Don’t look at it too hard, it just might embarrass me.

What’s embarrassing? I’m checking for edges in an odd manner and I also have a map flipping routine that I carried over, the code could be done in easily half the amount. To be fixed later.

This entry is not really about that code, however, it’s about finding the correct way to show your position numerically on the screen.

Let’s start at the top again, this is rehashed from an earlier article.

You’ll need

``````#include <stdio.h>//being used to convert ints to strings
``````

At the very top. So you can print out the player’s position.

We’re going to create a global variable, defined next to all the other global variables, let’s call it bob.

``unsigned char bob = 0;//used to print to screen``

You’ll also need a function.

``````void printtester(unsigned char info)
{
char text[10];
PrintStr("          ", 321);
sprintf(text, "%d", info);
PrintStr(text, 321);
}``````

So you can print. Notice the

``PrintStr("          ", 321);``

in the code.

This ensures that the string sent to the display will blank out. So, a bothersome flash will happen, no worries, this is for the programmer’s eyes only.

Finally, let’s send the coordinates to the function, using our nice little global variable – bob.

``````bob = (unsigned char)(Y1 >> 8);
printtester(bob);``````

You may ask, why I am doing a recast on the Y1 variable and not just define a short going to the printtester function.

Don’t judge me, I might get all weepy or explode!

Not really, it’s because I’m just testing, the truth is I’ve been testing a number of variables and just found it easier to recast to a char. I don’t see any reason to stop it.

Back to subject. You may also ask why there this a bitshift (>>8). This is just a fast way to divide by 256, a speedup trick.

Compile and test on the Y-axis. I move the character up and down and the number changes like a boss!

The Y-axis is showing from 0 at the very top to 159 at the very bottom. The bottom 12 pixels, you cannot move your character into which is fine because that’s the sync bar used by FASE.

Remove it from the verticalEdge function and place it in the horizontalEdge routine to test the X-Axis.

Oh and change the calling function slightly.

``````bob = (unsigned char)(X1 >> 8);
printtester(bob);``````

Compile and run again.

X runs from 0 to 240. Again, I wouldn’t worry too much about not going all the way to 255 as the sprite is probally drawn from 0 to 16 left to right and top to bottom. The sprite is 16×16 so 240 + 16 would be 256 which sounds correct for your resolution on the Speccy.

What does this all mean?

We now have an accurate way to determine the position of our sprite in X and Y, so we can do things like collision detection.

Betcha, the same thing can happen with enemy sprites, but I’m not delving into that just yet. But a way to determine the position of enemy sprites will be the start of enemy AI. That’s a long ways off for now.