6 scoring
It is time to add some gameplay.
I was thinking to make it a game where within some time you need to hit a character that randomly
appears on the screen. If not in time a block will form so the next time you need to go around
to get to the next character.
The first step would a a random position on the screen where a character would appear.
Although BASIC has a RND-function we will make our own routine for it.
Random on the ZX81 is not random at all. It is a function that uses a startposition, does a
calculation and set a new start. The start can be altered with the timer. We will do something
similar but a lot easier. Some games need a good random generator, most games however can do with
a less perfect randomizer. We will do to.
Our base for a random number will be the ROM-routine.
In reverse order we will read values from the ROM as a random number.
I choose reverse order because I will use the framecounter as well to keep the pointer in the ROM
changing as well. Frames decrease so an increase could be unaltered when frames decrease.
A double decrease will always alter the pointer.
Edit GAME1-006.asm and save to GAME1-007.asm.
If your latest version is 005 then make it 007 as well to synchronize.
I am at 006, but don't see a change to 006 in this topic.
Now add this routine somewhere in the source apart from the mainprogram.
Youo could insert it between the field-routine and the display file (after the RET).
Code: Select all
rnd ld de,0 ; get the pointer in ROM
ld hl,(frames) ; for randomness get framecounter
add hl,de ; add framecounter
dec hl ; when framecounter is unchanged make sure a change is done
ld a,h ; H can have any value, but ROM is #0000-#1FFF
and #0f ; only #0000-#0f00 is what we use
ld h,a ; set pointer back within ROM
ld (rnd+1),hl ; save current pointer (selfmodifying code!!!)
ld a,(hl) ; get value in ROM
range sub 20 ; we need 0-19 only
jr nc,range
adc a,20 ; undo last subtraction, range 1-20
ret ; back to mainprogram
The explanation of the code is added, perhaps I need to explain selfmodifying code a bit.
The first time this routine is called DE will have the value 0.
The second time the value has changed. This saves 3(!) bytes elsewhere where we would store it
elsewhere in memory. 2 bytes for the savelocation and 1 byte for the opcode LD DE,(RAMPLACE).
Now alter the start of the game a bit.
We need it likes this when we are playing the game later.
Code: Select all
; free codeable memory
gamecode ld bc,#0101 ; B=1, C=1, first position on the screen
newdot push bc ; save xy player
call rnd ; get a random number
ld b,a ; set as Y
call rnd ; get a next random number
ld c,a ; st as X
call field
ld (hl),27+128 ; place an inverted dot
pop bc ; get xy player
moveloop call field
ld (hl),"O"-27+128 ; we write a "O" INVERTED on the screen
Save, compile and run.
See what happens when you go to the dot.