Displaying my first sprite

Show us what you're working on, (preferably with screenshots).
User avatar
Pegaz
Dynamite Dan
Posts: 1210
Joined: Mon Nov 13, 2017 1:44 pm

Re: Displaying my first sprite

Post by Pegaz »

Freespirit wrote: Wed Jul 15, 2020 10:27 am My first enemy. Not very aggressive or difficult, but i have something moving so its a start. Added a basic score and a mini Arnie at the bottom who moves along as you progress the level. No idea what the game will be yet other than very simple :D


Image
If you continue to progress like this, we won’t have to wait long for a new Sidewize. :)
Freespirit
Microbot
Posts: 105
Joined: Wed Jul 01, 2020 2:33 pm

Re: Displaying my first sprite

Post by Freespirit »

Joefish wrote: Wed Jul 15, 2020 4:31 pm
Freespirit wrote: Wed Jul 15, 2020 2:27 pm Wow, Great post Joefish, many thanks. So much valuable info. I was only thinking the other day about negative numbers, i had no idea how that was going to work. I was also thinking i really need fractions of a pixel, but just thought it wasnt possible. I was blind now i can see :D
Note that when you add 16-bit numbers, you can only add BC and DE onto HL. But that means you can also add BC or DE onto IX or IY as well, if you've got round to using those. And you can quickly EX DE,HL if you want to add BC to DE instead. e.g:
EX DE,HL
ADD HL,BC
EX DE,HL
ADD HL,DE
will first add BC onto DE, then add DE onto HL. So that's your acceleration (BC), velocity (DE) and position (HL) calculation done.

Most compilers will let you simply write in negative numbers, e.g. LD A,-1 or LD BC,-999 and they'll work out the complementary negative value for you. ADD A,-1 will do the same as SUB 1. Note that if you have an 8-bit number you're treating as 'signed', to extend it to a 16-bit number you have to sign-extend it. That is, fill the upper 8 bits with 0s for positive or 1s for negative. What you need to do is flood the upper 8 bits with a copy of the top bit of your 8-bit number, and here's a trick to do it. This sign-extends 'A' into 16 bits in 'BC':
LD C,A ; bottom 8 bits are the same as A
ADD A,A ; doubling A bumps its top bit into the CARRY flag
SBC A,A ; subtracting A from A gives you 0. Subtracting the CARRY flag on top of that gives you either 0s or all 1s (255)
LD B,A ; B is thus a whole byte of sign-extension

However, if you're using the 'fixed-decimal' method of fractions I described then if A is a whole number and you want to extend it downward by 8 bits to give it a fractional part, just set B to A and C to 0.

Also note that when you use ADD, the CARRY bit is set if the result overflows. If you then use ADC it will do an ADD, but also add-on the current carry bit. That lets you daisy-chain the adding of bigger numbers like 24-bits or 32-bits.
With 16-bit maths there is no SUB instruction, only SBC, so remember to wipe the carry bit first with a 'neutral' instruction like AND A before doing a 16-bit SBC. But it's often easier just to ADD a negative number than worry about SBC.
Thanks Joefish, i just started using IX a few days ago, very handy. I didn't know about the ld a,-1 i just tried it and it compiles fine. Interesting. I will have to go through some examples and your explanation to get my head around the calculations. Its amazing all the little tricks you can do.
Freespirit
Microbot
Posts: 105
Joined: Wed Jul 01, 2020 2:33 pm

Re: Displaying my first sprite

Post by Freespirit »

stupidget wrote: Wed Jul 15, 2020 8:11 pm If I had a hat I’d doff it to you @Freespirit this is an amazing job.

I wish I’d spent more time reading the spectrum BASIC book as a kid. I think I have one of those minds that simply can’t understand programming :oops:
If i can do it anyone can! I spent a lot of time programming in Basic as a kid. It's like anything, just start off small like printing a character on screen and work your way up. It's not difficult. Unlike finding a copy of Mire Mare, that's difficult! :D
Freespirit
Microbot
Posts: 105
Joined: Wed Jul 01, 2020 2:33 pm

Re: Displaying my first sprite

Post by Freespirit »

Pegaz wrote: Wed Jul 15, 2020 8:24 pm If you continue to progress like this, we won’t have to wait long for a new Sidewize. :)
I'd not heard of this one, had to look it up. Looks good. A standard to aim for.
User avatar
Joefish
Rick Dangerous
Posts: 2058
Joined: Tue Nov 14, 2017 10:26 am

Re: Displaying my first sprite

Post by Joefish »

Freespirit wrote: Thu Jul 16, 2020 9:47 am Thanks Joefish, i just started using IX a few days ago, very handy. I didn't know about the ld a,-1 i just tried it and it compiles fine. Interesting. I will have to go through some examples and your explanation to get my head around the calculations. Its amazing all the little tricks you can do.
Yep, IX and IY are interesting. Their operations run a bit slower than the HL equivalents so most people start to use them when they run out of other registers. It's kind of a hidden feature of the Z80 but most compilers will let you use instructions that address 8-bit halves of IX and IY. So most things you can do with H and L separately, you can also do with IXH, IXL, IYH and IYL, e.g. LD IXL,1

They're also good for looking things up in a table. Like in my Pac-Man maze generator I can point IY at a cell in the table, then use LD A,(IY+1) to read the cell to the right, or (IY-32) to read the cell above.

And for structured data; for example, if your sprite data is always "1 byte for sprite ID, 1 for X, 1 for Y, 1 for Vx, 1 for Vy and 1 for HP remaining" then you can point IX at the first byte, then read (for example) the hit-points remaining on your sprite with LD A,(IX+5). That way all your functions that deal with sprites just need to be given that root IX value and they all know how to fetch the relevant data to do anything with any sprite.
User avatar
Pegaz
Dynamite Dan
Posts: 1210
Joined: Mon Nov 13, 2017 1:44 pm

Re: Displaying my first sprite

Post by Pegaz »

Freespirit wrote: Thu Jul 16, 2020 10:09 am
Pegaz wrote: Wed Jul 15, 2020 8:24 pm If you continue to progress like this, we won’t have to wait long for a new Sidewize. :)
I'd not heard of this one, had to look it up. Looks good. A standard to aim for.
There’s also its sequel Crosswize, with great animation and effects:

https://spectrumcomputing.co.uk/entry/1 ... /Crosswize
Freespirit
Microbot
Posts: 105
Joined: Wed Jul 01, 2020 2:33 pm

Re: Displaying my first sprite

Post by Freespirit »

Joefish wrote: Thu Jul 16, 2020 11:08 am
Freespirit wrote: Thu Jul 16, 2020 9:47 am Thanks Joefish, i just started using IX a few days ago, very handy. I didn't know about the ld a,-1 i just tried it and it compiles fine. Interesting. I will have to go through some examples and your explanation to get my head around the calculations. Its amazing all the little tricks you can do.
Yep, IX and IY are interesting. Their operations run a bit slower than the HL equivalents so most people start to use them when they run out of other registers. It's kind of a hidden feature of the Z80 but most compilers will let you use instructions that address 8-bit halves of IX and IY. So most things you can do with H and L separately, you can also do with IXH, IXL, IYH and IYL, e.g. LD IXL,1

They're also good for looking things up in a table. Like in my Pac-Man maze generator I can point IY at a cell in the table, then use LD A,(IY+1) to read the cell to the right, or (IY-32) to read the cell above.

And for structured data; for example, if your sprite data is always "1 byte for sprite ID, 1 for X, 1 for Y, 1 for Vx, 1 for Vy and 1 for HP remaining" then you can point IX at the first byte, then read (for example) the hit-points remaining on your sprite with LD A,(IX+5). That way all your functions that deal with sprites just need to be given that root IX value and they all know how to fetch the relevant data to do anything with any sprite.
Thanks for this, I didn’t know about IXL and IXH. Yes, I’m using it for a table exactly as you say for x,y etc. Strangely I used IX+1 a few days ago just to see if it would work and I’m now using it with the table.
Freespirit
Microbot
Posts: 105
Joined: Wed Jul 01, 2020 2:33 pm

Re: Displaying my first sprite

Post by Freespirit »

Pegaz wrote: Thu Jul 16, 2020 11:49 am There’s also its sequel Crosswize, with great animation and effects:

https://spectrumcomputing.co.uk/entry/1 ... /Crosswize
They really improved their game for the sequel it looks a lot better. Very impressive.
Freespirit
Microbot
Posts: 105
Joined: Wed Jul 01, 2020 2:33 pm

Re: Displaying my first sprite

Post by Freespirit »

Jetpac meets Flappy bird. It's the game 'Ultimate' always wanted to make.


Image
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: Displaying my first sprite

Post by Ast A. Moore »

Very impressive. Horizontal scrollers are the hardest to code on the Spectrum.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
robdc
Drutt
Posts: 5
Joined: Fri Jul 10, 2020 2:32 pm

Re: Displaying my first sprite

Post by robdc »

Looking really cool [mention]Freespirit[/mention]
I'm in similar place as you, started learning Z80 assembly a few weeks ago, using flappy bird as my learning project coincidentally.
Will post a clip of my progress later
Freespirit
Microbot
Posts: 105
Joined: Wed Jul 01, 2020 2:33 pm

Re: Displaying my first sprite

Post by Freespirit »

Well, 'Jetflap' is complete (sort of). It's probably the greatest 10 second game ever (released today) and A LOT could be improved. It took me to my limits rather than the other way around. :D

https://1drv.ms/u/s!At9gHWg9x_GpjgiWn5T ... 1?e=6UPV3f
User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: Displaying my first sprite

Post by Ast A. Moore »

Could you also provide a regular TAP/TZX file please? (I generally avoid loading snapshot, because they mess up my emulator’s settings.)
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
dfzx
Manic Miner
Posts: 682
Joined: Mon Nov 13, 2017 6:55 pm
Location: New Forest, UK
Contact:

Re: Displaying my first sprite

Post by dfzx »

Freespirit wrote: Tue Jul 21, 2020 2:47 pm Well, 'Jetflap' is complete (sort of).
An extra point for the name, I think. :lol:
Derek Fountain, author of the ZX Spectrum C Programmer's Getting Started Guide and various open source games, hardware and other projects, including an IF1 and ZX Microdrive emulator.
Freespirit
Microbot
Posts: 105
Joined: Wed Jul 01, 2020 2:33 pm

Re: Displaying my first sprite

Post by Freespirit »

User avatar
Ast A. Moore
Rick Dangerous
Posts: 2641
Joined: Mon Nov 13, 2017 3:16 pm

Re: Displaying my first sprite

Post by Ast A. Moore »

Freespirit wrote: Tue Jul 21, 2020 5:54 pm Here is the tzx version:

https://1drv.ms/u/s!At9gHWg9x_GpjguV18a ... b?e=APX6w4
Thanks!

The game looks great. Really good for a debut. If I could suggest one improvement that would have the greatest impact on playability, it would be the scroll rate. Right now, it’s inconsistent and speeds up and slows down unpredictably. Slowing down is fine, but speeding up leaves you no time to maneuver.

Other than that, you could implement a simple keyboard debounce routine upon entering the various menu and info screens. That way they won’t flick by when the game ends. One of the simples ways to do it is this:

Code: Select all

1$		call $28e		;poll keyboard upon entry
		inc e			;if E=FFh (no key is pressed), INC E will result in 0
		jr nz,1$		;and we can go on; else, we wait until no key is pressed
It’s sometimes a good idea to put this little check before your check for a valid keypress.

For example, after a player dies and the game ends, he normally keesp pressing one of the control keys (well, just the one in your case). If you then immediately display a game-over screen and wait for a keypress to dismiss it, it’ll disappear in the next frame, because the play will likely not have released the key in that short time. The code above will make sure that doesn’t happen.
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
Freespirit
Microbot
Posts: 105
Joined: Wed Jul 01, 2020 2:33 pm

Re: Displaying my first sprite

Post by Freespirit »

Thanks for the feedback. Yes, there are a few scroll speed issues. The keyboard routine will be very useful, thanks for that. :D
Post Reply