Lab3 -THE MAZE
In lab 3 we need create the game using 6502 and have to perform some task for that.
Here by I have created the game called Maze where we need to find the path to the end of the last part. So here we are using the arrow keys to move up and down and also across. At the start there will some instruction and also when u completed there will be comment that u have won .
More over the Maze code that created the maze is in the 05 and 01 which has this output.
Here the code
; zero-page variables
define ROW $20 ; current row
define COL $21 ; current column
define DRAWN_ROW $22 ; number of drawn rows
define MAZE_LENGTH $14 ; a pointer that points to where the maze will
define MAZE_HEIGHT $15 ; be drawn
define PLAYER_LENGTH $10 ; a pointer that points to the player in the
; screen
define PLAYER_HEIGHT $11
define TARGET_LENGTH $12 ; a pointer that points to the target position
define TARGET_HEIGHT $13 ; where the player wants to proceed
; constants
define PATH $03 ; path color
define PLAYER $09 ; player color
define HEIGHT 13 ; height of the maze
define WIDTH 21 ; width of the maze
; ROM routine
define SCINIT $ff81 ; initialize/clear screen
jsr printHelp
jsr drawMaze
jsr gameInit
jsr gameLoop
printHelp: ldy #$00 ; print instructions on the screen
pHelpLoop: lda help,y
beq done
sta $f000,y
iny
bne pHelpLoop
gameInit: lda #$01 ; initialize ROW, COL to make the player
sta ROW ; starting at $0221 of the screen
sta COL
rts
gameLoop: jsr updatePosition
jsr getkey
jsr checkCollision
ldx #$00 ; clear out the key buffer
stx $ff
jmp gameLoop
updatePosition: ldy ROW ; load PLAYER pointer with ROW
lda table_low,y
sta PLAYER_LENGTH
lda table_high,y
sta PLAYER_HEIGHT
ldy COL ; place the player at (POINTER + COL)
lda #PLAYER
sta (PLAYER_LENGTH),y
rts
getkey: lda $ff ; get the input key
cmp #$80 ; allow arrow keys only
bmi getkey
cmp #$84
bpl getkey
pha ; save the accumulator
lda #PATH ; set color of the current position to PATH
sta (PLAYER_LENGTH),y
pla ; restore accumulator
cmp #$80 ; check key is up
bne checkRight
dec ROW ; ... if yes, decrement ROW
rts
checkRight: cmp #$81 ; check if key is right
bne checkDown
inc COL ; ... if yes, increment COL
rts
checkDown: cmp #$82 ; check if key is down
bne checkLeft
inc ROW ; ... if yes, increment ROW
rts
checkLeft: cmp #$83 ; check if key is left
bne done
dec COL ; ... if yes, decrement COL
rts
done: rts ; break out of a loop or subroutine
checkCollision: ldy ROW ; load TARGET pointer with ROW
lda table_low,y
sta TARGET_LENGTH
lda table_high,y
sta TARGET_HEIGHT
ldy COL ; load the color from the target
lda (TARGET_LENGTH),y; at (POINTER + COL)
cmp #$01
beq done
cmp #$03
beq done
cmp #$0a
beq gameComplete
lda #$00
sta (TARGET_LENGTH),y
lda $ff
cmp #$80 ; if input key was up...
bne ifRight
inc ROW ; ... if yes, increment ROW
rts
ifRight: cmp #$81 ; if input key was right...
bne ifDown
dec COL ; ... if yes, decrement COL
rts
ifDown: cmp #$82 ; if input key was down...
bne ifLeft
dec ROW ; ... if yes, decrement ROW
rts
ifLeft: cmp #$83 ; if input key was left...
bne done
inc COL ; ... if yes, increment COL
rts
gameComplete: jsr SCINIT
ldy #$00 ; print game completion message on the screen
pGameComplete: lda complete,y
beq done
sta $f000,y
iny
bne pGameComplete
brk
drawMaze: lda #$21 ; a pointer pointing to the first pixel
sta MAZE_LENGTH ; of the screen
lda #$02
sta MAZE_HEIGHT
lda #$00 ; number of drawn rows
sta DRAWN_ROW
ldx #$00 ; maze data index
ldy #$00 ; column index
draw: lda maze_data,x
sta (MAZE_LENGTH), y
inx
iny
cpy #WIDTH ; compare with the number of WIDTH
bne draw ; if not, keep drawing the column
inc DRAWN_ROW ; increment the number of row
lda #HEIGHT
cmp DRAWN_ROW ; compare with the number of HEIGHT
beq done
lda MAZE_LENGTH
clc
adc #$20 ; add 32(0x0020) to increment the row
sta MAZE_LENGTH ; of the pixel
lda MAZE_HEIGHT
adc #$00
sta MAZE_HEIGHT
ldy #$00 ; reset the column index for the new row
beq draw
; help text message
help:
dcb "P","l","a","y",32,"w","i","t","h",32,"a","r","r","o","w"
dcb 00
; game complete message
complete:
dcb "Y","o","u",32,"W","O","N"
dcb 00
; maze map data
maze_data:
dcb 1,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
dcb 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5
dcb 5,5,5,5,5,1,5,5,5,5,5,5,5,5,5,5,5,1,5,1,5
dcb 5,1,1,1,1,1,5,1,1,1,1,1,1,1,1,1,5,1,5,1,5
dcb 5,1,5,5,5,5,5,5,5,5,5,1,5,5,5,5,5,1,5,1,5
dcb 5,1,5,1,5,1,1,1,1,1,5,1,5,1,1,1,5,1,5,1,5
dcb 5,1,5,1,5,1,5,1,5,1,5,1,5,5,1,5,5,1,5,1,5
dcb 5,1,5,1,1,1,5,1,5,1,5,1,5,1,1,1,5,1,5,1,5
dcb 5,1,5,1,5,1,5,1,5,1,5,1,5,5,1,5,5,1,5,1,5
dcb 5,1,5,1,5,1,5,1,5,1,5,1,5,1,1,1,5,1,5,1,5
dcb 5,5,5,5,5,1,5,1,5,1,5,1,5,5,1,5,5,1,1,1,5
dcb 5,1,1,1,1,1,5,1,5,1,1,1,1,1,1,1,1,1,5,1,5
dcb 5,5,10,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
; these two tables contain the high and low bytes
; of the addresses of the start of each row
table_high:
dcb $02,$02,$02,$02,$02,$02,$02,$02
dcb $03,$03,$03,$03,$03,$03,$03,$03
dcb $04,$04,$04,$04,$04,$04,$04,$04
dcb $05,$05,$05,$05,$05,$05,$05,$05
table_low:
dcb $00,$20,$40,$60,$80,$a0,$c0,$e0
dcb $00,$20,$40,$60,$80,$a0,$c0,$e0
dcb $00,$20,$40,$60,$80,$a0,$c0,$e0
dcb $00,$20,$40,$60,$80,$a0,$c0,$e0
The code runs and the Maze is working as per plan but I don't know why there is blank at the bottom so I thing I have some error.
The learning outcome is the code is very long but when we don in parts then any thing is possible .
The creating of the maze is very difficult part for me. But i have done using online sources .
Comments
Post a Comment