# 20. Platformer again

I decided not to use a split screen. I was really nervous that we were going to get slowdown once we added enemies and coins, which would (with my setup) make the screen wildly go back and forth, and look terrible.

I left the split screen out, and we don’t need to worry about slowdown (having more game logic than can fit in a frame).

Now that we put the music and sound fx in, let’s add some enemies and coins to our platformer. We can use our coin sfx for collision with coins, and noise sfx for collision with enemies.

I drew some simple enemies in yy chr, and made some metasprite definitions. I could have used NES Screen Tool, but since these are fairly simple, I just cut and pasted and edited the tile # from another similar metasprite.

![25\_YY](https://nesdoug.files.wordpress.com/2018/09/25_yy.png?w=924)

It would have been nice to use an array of structs to define everything, but the 6502 is too slow. The faster way is to use a lot of char arrays, all smaller than 256 bytes, like this.

\#define MAX\_COINS 16\
unsigned char coin\_x\[MAX\_COINS];\
unsigned char coin\_y\[MAX\_COINS];\
unsigned char coin\_active\[MAX\_COINS];\
unsigned char coin\_room\[MAX\_COINS];\
unsigned char coin\_actual\_x\[MAX\_COINS];

I even sped up the object initial code, to point out how you can make faster code by only putting 1 array per line of code. The 6502 processor only can do 1 pointer at a time, so the compiler shoves the other one in the c stack, and then does a whole dance around it, 5 times slower.

So instead of…

coin\_y\[index] = pointer\[index2];

I did this…

temp1 = pointer\[index2]; // get a byte of data\
coin\_y\[index] = temp1;

Believe it or not, this compiles to faster code. We have to think about speed optimization, if we want a scrolling game with multiple moving objects. (And no slow down).

So, every frame, I check to see if each object is in range (on screen), and mark them “active”. I only move them if active, I only check collision if active, and I only draw them to screen if active.

check\_spr\_objects() checks each object to see if they are on screen, and marks them active, if true.

Right now the movement code is terribly simple. The chasers just see if the hero X is greater than theirs, and heads toward the hero. I will try to make this a little better next time.

Currently, I am using y=0xff as “doesn’t exist” (TURN\_OFF). So, turning off an enemy is handled by putting their Y to 0xff. this could be handled differently, but that’s what I chose. It works as long as we don’t need enemies jumping in from below the screen.

One more thing I started to do, was add game states. I added Pause mode, which changes the song, and nothing moves, but I am darkening the screen by setting the color emphasis bits. This was something built into the NES PPU. Flipping a color emphasis will emphasize a color by darkening the other colors. The red bit will darken blue and green. If you set them all, it darkens all the color, just a little bit.

color\_emphasis(COL\_EMP\_NORMAL);

![25\_platformer5](https://nesdoug.files.wordpress.com/2018/09/25_platformer5.png?w=924)

color\_emphasis(COL\_EMP\_DARK);

![25\_platformer5b](https://nesdoug.files.wordpress.com/2018/09/25_platformer5b.png?w=924)

[https://github.com/nesdoug/25\_Platform5/blob/master/platformer5.c](https://github.com/nesdoug/25_Platform5/blob/master/platformer5.c)

[https://github.com/nesdoug/25\_Platform5](https://github.com/nesdoug/25_Platform5)

Now that we have the it basically working, we will make some real levels. Next time… Sorry the link below doesn’t go to the next page, try this link…

[https://nesdoug.com/2018/09/11/21-finished-platformer/](https://nesdoug.com/2018/09/11/21-finished-platformer/)
