Invaders Progress

The author of CPM Tetris and Snake.
User avatar
productiondave
Posts: 117
Joined: Tue Mar 28, 2023 10:01 pm

Invaders Progress

Post by productiondave »

I know that many of you are looking forward to a space invaders clone on the Nabu. I am working on it, but it's slow going. I am writing this game in raw Z80 assembly. My main reason for doing so is speed. I am not convinced of the z88dk compiler ability to generate really fast code. ( I am also not convinced of my own ability to generate really fast assembly code - but feel I'm in with a fighting chance.)

Here is my todo list with some of the items already crossed off:

DONE:
  • animate aliens back and forth across the screen until at least one alien hits the edge of the screen before changing direction and dropping down 1 tile row.. (This took me a long time to figure out.)
  • draw shields on the screen
  • controllable player cannon
  • bullets (player shooting) - shoot event, bullet animation, clear bullet logic when bullet hits an invader or flies off the top of the screen.
  • detect bullets colliding with aliens ( This was also very tricky to get right. See the post script of this post for a bug that kept me up at nights)
  • delete aliens from the game area when bullets hit them.
TO DO:
  • implement game-over detection (player has defeated all the aliens)
  • alien death animation / explosion
  • implement shield damage from player bullets
  • implement shield defense and damage from alien bombs
  • let aliens drop bombs - all code as per bullets but going down and with a different sprite pattern and colour.
  • detect collision between bomb sprites and player sprite (player dies)
  • implement 3 lives system
  • implement scoring system and score display text
  • implement ufo
  • sound effects
  • game music
  • implement start menu - including ability to play again etc.
  • implement high score system.
  • implement game levels. Options include increased number of alien bombs at once, faster aliens etc.
So as you can see, there is still much to do. I am enjoying the work so far. I don't want to make any promises or commit to any deadlines on this project. If I did, I would definitely fail to meet them.

Thanks for you patience
~ Dave

PS:

Bullet detection between sprites and aliens is a matter of finding the tile position on the screen under the bullet sprite. Then find the pattern byte of that tile that matches the bullet's Y position within the tile. Then use the bullet's X Position within the tile to bit test the pattern byte for a pixel. If a pixel is on (1) then we have hit the alien. If the pixel is off (0) we have not hit the alien. Maybe we are just under it's armpit or something. In this case, the bullet is moved up the screen by the number of pixels determined by it's speed. I was moving my bullets 4 pixels each frame.

The bug I was having trouble with was that for the bottom row of aliens, I would often be aligned with their eyes. I would not match a pixel on the first frame. Then the next frame would put the bullet directly over it's eyes (which are empty pixels) and, of course, not match. Then the bullet would move another 4 pixels and be above the alien.

The solution turned out to be quite simple. Just move the bullets 3 pixels each frame which (based on the alien tile-set I am using) will always guarantee a hit if the bullet is over the body of the alien within the tile. The bullets will never coincide with the eyes.

I am not over exaggerating when I say this took me a week to figure out. I was only able to solve it by stepping through the code using the Mame debug tools and a notepad and pen to validate all the calculations and bit matching.
allinretro
Posts: 14
Joined: Mon Apr 03, 2023 5:37 pm

Re: Invaders Progress

Post by allinretro »

Thanks for sharing Dave. I'm Jealous of your programming skills. I haven't written much more than One-Off scripts as required for Network Admin Duties over the last 20 years. 40+ years ago had a blast writing Yahtzee and Stock Ticker Games on Commodore PET BASIC then ported to Apple II BASIC. Then life got way to serious.

Keep on rockin' the Invaders Game. I am looking forward to playing this one.

If all goes well I will have my Sanwa Arcade stick built and ready for the NABU.

Cheers!

-Alan
User avatar
DJ Sures
Posts: 347
Joined: Tue Mar 28, 2023 8:36 pm
Location: nabu.ca
Contact:

Re: Invaders Progress

Post by DJ Sures »

Right on - looking forward to seeing your game. While you're right, vanilla C isn't great for fast screen processing, but game logic is fine. That's why I prefer to merge the two, assembly and C. The ASM and ENDASM of z88dk are great for that. Considering the game logic is merely several embedded conditions, that's what makes the simplicity of C so appealing for rapid development. Throw the video/screen updates in assembly and perform the game logic in C.

One tip is global C variables are accessible in ASM by pretending the _ underscore character. That makes the global variable work just like an assembly label. It's fast and funky :D

But if you're doing this to see if you can make a game entirely in assembly, ignore my advice!
User avatar
productiondave
Posts: 117
Joined: Tue Mar 28, 2023 10:01 pm

Re: Invaders Progress

Post by productiondave »

So in the interests of actually releasing something this century, I have gone back to C and NABU-LIB. Turns out, I am really not much good at z80 assembly. 6502 Assembly is so much easier with much nicer addressing modes. Even if I one needs to use the zero page for 16 bit variables.

Here is a little video of progress so far:




(with a bit of regression too. _ I am using tile level collisions - no pixel level collisions as I can't get it working good enough.)

DONE:
  • animate aliens back and forth across the screen until at least one alien hits the edge of the screen before changing direction and dropping down 1 tile row.. (This took me a long time to figure out.)
  • draw shields on the screen
  • controllable player cannon
  • bullets (player shooting) - shoot event, bullet animation, clear bullet logic when bullet hits an invader or flies off the top of the screen.
  • detect bullets colliding with aliens - Tile level collision only.
  • implement game-over detection (player has defeated all the aliens)
  • implement shield damage from player bullets
  • implement shield defense and damage from alien bombs
  • let aliens drop bombs - all code as per bullets but going down and with a different sprite pattern and colour.
TO DO:
  • alien death animation / explosion
  • detect collision between bomb sprites and player sprite (player dies)
  • implement 3 lives system
  • implement scoring system and score display text
  • implement ufo
  • sound effects
  • game music
  • implement start menu - including ability to play again etc.
  • implement high score system.
  • implement game levels. Options include increased number of alien bombs at once, faster aliens etc.
User avatar
DJ Sures
Posts: 347
Joined: Tue Mar 28, 2023 8:36 pm
Location: nabu.ca
Contact:

Re: Invaders Progress

Post by DJ Sures »

Wow that is looking really good!! I’m really excited to play if :) thanks so much for your effort
User avatar
productiondave
Posts: 117
Joined: Tue Mar 28, 2023 10:01 pm

Re: Invaders Progress

Post by productiondave »

Today I learned.

Code: Select all

if ((pixels & set_bit_mask[bulletx % 8]) == 0) {
            return false;
}
is not the same as

Code: Select all

if ( pixels & set_bit_mask[bulletx % 8] == 0) {
            return false;
}
So now I have bullets hitting aliens with pixel level accuracy. Shields go through 3 stages of damage. I just need to work on those tile patterns a little better.
User avatar
DJ Sures
Posts: 347
Joined: Tue Mar 28, 2023 8:36 pm
Location: nabu.ca
Contact:

Re: Invaders Progress

Post by DJ Sures »

Hahaha yes. The order of operation is important to remember. Although you’ll notice in my code that I have a habit of bracketing comparisons in a condition for that reason

I bet it was a good gotcha! Did it take a while to figure out?

Also careful with the modulus if you don’t need it. It’s an expensive calculation on z80.
User avatar
productiondave
Posts: 117
Joined: Tue Mar 28, 2023 10:01 pm

Re: Invaders Progress

Post by productiondave »

For powers of 2 mod is the same as logical and. So cheap as chips mate.

Code: Select all

uint8_t xo = bulletx % 8;
Is the same as:

Code: Select all

uint8_t xo = (bulletx & 0x07);
And yes, it took a long time to figure out.
User avatar
DJ Sures
Posts: 347
Joined: Tue Mar 28, 2023 8:36 pm
Location: nabu.ca
Contact:

Re: Invaders Progress

Post by DJ Sures »

Ah yeah I guess so! That’s good to know
User avatar
productiondave
Posts: 117
Joined: Tue Mar 28, 2023 10:01 pm

Re: Invaders Progress

Post by productiondave »

Assuming z88dk does the right thing of course. 🤔
Post Reply