5 movement (4)
Now we only move on the first line we want to move over the entire playfield.
It is time to add an equivalent for the PRINT AT command from BASIC.
I normally use B and C registers as equivalent for the BASIC-variables Y and X.
We already used a CALL-subroutine to a ROM-program, but now we will make a routine
that will calculate the location on the screen. The advantage of 2 registers holding the coordinates is also that we can easily test if we are out of range.
Our coordinates go from 1,1 to 20,20
Each line has 21 bytes so to get to positition 2,2 we need to add 1 full line and 1 position.
For position 1,1 we don't need to add anything to screen, however it is easier to
start 1 line and 1 column too low and then add a full line and 1 column, therefore
we set the start at SCREEN-21-1.
I added vertical movement and coordinates in the new code
Now replace the code from gamecode to dfile with the folowing code:
Compile and run, we can now move all over the screen with QAOP.
Code: Select all
gamecode
ld bc,#0101 ; B=1, C=1, first position on the screen
moveloop
call field ; find PRINT AT position
ld (hl),"O"-27 ; we write a "O" on the screen
push hl
ld hl,frames ; make hl point to the timecounter
ld a,(hl) ; get the timecounter in A
sub 2 ; take of 2, the number we want to test
wfr cp (hl) ; test if the value is the same
jr nz,wfr ; NOW WE WAIT UNTIL FRAMES MATCHES A!
pop hl
push bc ; NOW WE NEED TO SAVE BC!
wkey ld bc,(lastk) ; get key information
ld a,c ; copy C in A to keep C
inc a ; Test if NOKEY pressed
jr z,wkey ; NO KEY, wait for key
ld (hl),128 ; we pressed a key, so we erase the old position
call #7bd ; BC holds info about key, ROM can translated this here
pop bc
ld a,(hl) ; get character
cp "O"-27 ; did we press the "O"?
jr nz,right ; we didn't go left
left dec c
jr nz,okmove
inc c
jr okmove
right cp "P"-27 ; did we press "P", right
jr nz,up
inc c ; move right
ld a,c ; get new position
cp 21 ; test out of screen
jr nz,okmove ; if not, move allowed
dec c
jr okmove
up cp "Q"-27
jr nz,down
dec b
jr nz,okmove
inc b
jr okmove
down cp "A"-27 ; did we press "A", down
jr nz,okmove
inc b ; move down
ld a,b ; get new position
cp 21 ; test out of screen
jr nz,okmove ; if not, move allowed
dec b
okmove jr moveloop ; stay in playloop
; BC = XY, return HL=field on screen
field push bc ; save Y and X
ld hl,screen-21-1 ; always 1xY and 1xX added, line is 20+HALT
ld de,21 ; the size of 1 line (20 + HALT)
frow add hl,de ; add rows until B=0
djnz frow ; DEC B, JR NZ frow
add hl,bc ; B now 0, C is X, so HL now field on screen
pop bc ; get back original Y X
ret