That old X,Y coordinates generation using that ugly way was a real sh*t.

Now it is ways better (it simply does `random number` modulo `screen rows`
- or columns, depends).

There also was an error in comparing snakeX,Y with new piece of food's X,Y
Now really fixed (I hope, at least, since the bug is terribly difficult to
reproduce)

Also improved printing routine: now it can print general hexadecimal numbers,
and can locate them everywhere across first line (and beyond also, since it
is not checked, but if you do, you are a moron). Simply put byte to be print
in accumulator, column in Y index, and then jsr.
This commit is contained in:
giomba 2017-12-22 16:21:19 +01:00
parent 61a1c140f4
commit 207cbfd3cf

104
snake.asm
View File

@ -320,48 +320,69 @@ overEndCheck:
jsr calcTileMem jsr calcTileMem
lda (tileMem),y ; read content of head memory location lda (tileMem),y ; read content of head memory location
cmp foodTile cmp foodTile
bne checkSelfEat ; if memory does not contain food, then skip to self-eat test... beq foodEaten ; if memory does contain food, then perform foodEaten actions,
jmp checkSelfEat ; else just loooong jump to test if I ate myself
foodEaten:
ldx length ; else, increment snake length, ldx length ; else, increment snake length,
inx inx
stx length stx length
genFood: genFood:
ldx random ; increment random value ldx random
inx inx
stx random stx random
txa txa
sta calcTileX ; use it as tile X-coordinate genFoodX: ; calculate `random` modulo `screenW`
lsr ; divide it by 8; maximum expected is 32 (256/8) sec
lsr sbc screenW
lsr cmp screenW
cmp #18 ; test if it is still > 18 bcs genFoodX
bcc foodNoMod ; value must be < 18, sta calcTileX
; because it is the highest value v
; for which v * 40 + 256 < 1000 txa
; this ensures that piece if food will be placed genFoodY: ; calculate `random` modulo 22 (22 = screenH - 1)
; within visible screen sec
sec ; if value is > 18, then subtract 18; now it sbc #22
sbc #18 ; surely is less than 18 cmp #22
foodNoMod: bcs genFoodY
cmp #0 clc ; add 1 because 1st line can not be used
bne foodNoLow ; if it is 0, then set 1 (avoid first line) adc #1
lda #1 sta calcTileY
foodNoLow:
sta calcTileY ; use this new value as tile Y-coordinate
; Now I have X and Y coordinate for food stored in calcTileX, calcTileY ; Now I have X and Y coordinate for food stored in calcTileX, calcTileY
; and I must check it is not the location that I am going to overwrite ; and I must check it is not the location that I am going to overwrite
; with the head in draw snake head... ; with the head in draw snake head...
cmp snakeY lda calcTileX
bne foodOK
lda snakeX
cmp snakeX cmp snakeX
bne foodOK
lda calcTileY
cmp snakeY
beq genFood beq genFood
foodOK: foodOK:
; debug -- print choosen X,Y for food
ldy #$18
lda calcTileX
jsr printByte
ldy #$1b
lda calcTileY
jsr printByte
; debug -- print snake head X,Y
ldy #$1f
lda snakeX
jsr printByte
ldy #$22
lda snakeY
jsr printByte
ldy #0
jsr calcTileMem ; calc food address in memory jsr calcTileMem ; calc food address in memory
lda (tileMem),y ; check if memory is empty lda (tileMem),y ; check if memory is empty
cmp #$20 ; is there a space? cmp #$20 ; is there a space?
bne genFood ; if not, must generate another number bne genFood ; if not, must generate another number
lda foodTile ; else, just put that fucking piece of food there lda foodTile ; else, just put that fucking piece of food there
sta (tileMem),y sta (tileMem),y
lda #$d4 lda #$d4
clc clc
adc tileMem + 1 adc tileMem + 1
@ -369,7 +390,9 @@ foodOK:
lda foodColor lda foodColor
sta (tileMem),y sta (tileMem),y
jsr printScore ; print score ldy #$10
lda length
jsr printByte ; print score
jmp checkEndSelfEat jmp checkEndSelfEat
checkEndFood: checkEndFood:
@ -381,6 +404,7 @@ checkSelfEat:
checkEndSelfEat: checkEndSelfEat:
; Draw snake head ; Draw snake head
ldy #0
lda snakeX ; calc char address in video memory, and put snakeTile lda snakeX ; calc char address in video memory, and put snakeTile
sta calcTileX sta calcTileX
lda snakeY lda snakeY
@ -423,6 +447,11 @@ irqalways:
inx inx
stx random stx random
; debug -- print current random value
ldy #$14
lda random
jsr printByte
; Go to original system routine ; Go to original system routine
jmp $ea31 jmp $ea31
@ -501,17 +530,11 @@ calcTileEnd: ; now multiplication is ended, so add X
rts rts
; Print score ; Print a byte in hexadecimal
printScore: ; A input register for byte to print
; Push registers on stack (save) ; Y input register for printing colum (on first line)
pha printByte:
txa ; Copy parameter also in X
pha
tya
pha
; Take length in A and copy also in X
lda length
tax tax
lsr ; Take most significant nibble lsr ; Take most significant nibble
@ -519,19 +542,12 @@ printScore:
lsr lsr
lsr lsr
jsr printDigit jsr printDigit
sta $410 ; print msb char at $410 sta $400,y ; print msb char
txa ; Take least significant nibble (use previous copy) txa ; Take least significant nibble (use previous copy)
and #$0f and #$0f
jsr printDigit jsr printDigit
sta $411 ; print lsb char at $411 sta $401,y ; print lsb char
; Pull registers from stack (restore)
pla
tay
pla
tax
pla
rts rts