Micro Mart


Return Of The Bedroom Programmer Part 10

 
Published in Micro Mart #1127

After experimenting with the Sinclair ZX Spectrum's quirky 'beeper' last week, Shaun gives us an example of creating simple graphics.

Retro Mart: Return Of The Bedroom Programmer Part 10

Let's be honest, most of you that have been following this series will have in the back of their minds creating a 'commercial-quality' game for the old Sinclair ZX Spectrum, using '100% machine code', and if you look at the software released for the 8-bit home computer, not just during the 1980s but to date, then the vast majority of it falls into this category. So the most obvious thing to do is to draw, animate and control graphics. I've covered writing text to the screen fairly extensively, so with any luck you've had a program proclaiming that you are ace, or perhaps a similar message in a 'scrolly text'. Maybe you've played about with last week's example too and played a tune along with your message.

Now you can add to the mix some user-defined graphics. Let's have a look at this little routine:

org $9000             ; Our program will start at 9*4096 (36864)
ld hl,GFX             ; Points the HL register at the area of memory called GFX
ld (23675),hl         ; Initialized system for user defined graphics
ld a,2                ; We want channel two (upper screen)
call 5633             ; Open channel two (i.e. write to screen)
ld a,12
ld (XCOORD),a         ; We'll put our graphic character on row 12
LOOP
call INITXY           ; This will set up the X (row) and Y (column) co-ordinates
ld a,144
rst 16                ; Display character 144 (our UDG)
call DELAY            ; Let's slow things down a little
ld hl,XCOORD          ; Get the current position and store into HL
dec (hl)              ; Decrease HL by one
ld a,(XCOORD)         ; Store new co-ordinate
cp 255                ; Is it at the top line of the screen yet?
jr nz,LOOP            ; If not, then jump back to LOOP
ret
DELAY
ld b,12               ; Delay length
DELAYLOOP
halt                  ; Stop everything temporarily
dhnz DELAYLOOP        ; Decrease B by one and repeat until B is zero
ret
INITXY
ld a,22
rst 16                ; Calls the Sinclair PRINT AT routine
ld a,(XCOORD)         ; Gets the X co-ordinate
rst 16
ld a,(YCOORD)         ; and the Y co-ordinate
rst 16                ; So essentially PRINT AT X,Y;
like in BASIC
ret
XCOORD
defb 0
YCOORD
defb 15
GFX                   ; Here is our character-cell redefined graphic
defb %00111100
defb %01011010
defb %01111110
defb %01011010
defb %01100110
defb %00111100
defb %00000000

Once it's compiled, run it and see what happens. As you will notice, the redefined character cell is represented in binary at the end of the program, with each '1' representing the bits that are 'on', and '0' [the bits that] are off. There is a deliberate fault in the program, though, which you will realise soon enough. How can this be fixed? And can you make your UDG travel on the horizontal and vertical plane, or start from a different part of the screen? How about replacing the delay routine with a piece of code that will play a note, or perhaps wait for a key to be pressed on the keyboard before continuing with the main routine? The more you experiment, the more you will learn, so now is a good time to get your hands dirty by trying to tape together previous examples with this one.

Don't worry about errors, because it's all part of the process. If you would like some theory as to what is happening and why, then pop along to the Micro Mart forums (the specific thread is at tinyurl.com/Speccy-Coding), or e-mail me with any questions via shaun@micromart.co.uk. See you next week.

Shaun Bebbington