Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Simple 6502 ASM Game!
- ; Created by Lewisk3 (Redxone)
- ;Define variables
- define player $06 ; Color of player 1b
- define key $ff ; Byte of memory that holds our key pressed information
- define up $77 ; ID Of W Key
- define down $73 ; ID Of S Key
- define left $61 ; ID Of A Key
- define right $64 ; ID Of D Key
- define scr $0200 ; Indicates Starting screen memory
- define pos $11 ; LSB Of Player location
- define ppos $12 ; MSB Of Player location
- define colflag $1F ; Location of collision flag
- define place $7A
- define break $78
- define blockplaced $01
- init:
- lda #$02 ; Starting location of player x
- sta $11 ; Store starting location X to player LSB
- lda #$03 ; Starting location of player y ( Must be greater than 02, or it will not be written to screen space! )
- sta $12 ; Store starting location Y to player MSB
- ; Place sample block
- lda #$01
- ldx #$05
- ldy #$04
- jsr nblock
- jsr draw ; Draw player
- jmp loop ; Start game Loop (check for inputs)
- getax:
- lda ppos ; Returns MSB of player location
- rts
- getay:
- lda pos ; Returns LSB of player location
- rts
- nblock:
- pha ; Push accumulator
- txa ; Move X into A
- sta $14 ; Store X into 14
- tya ; Move Y into A
- sta $15 ; Store Y into 15
- pla ; Pull A back
- ldx #$00 ; Setup indirect for screen placing
- sta ($14, X) ; Store A pixel into screen
- rts
- loadp:
- ldx #$00 ; Here is where things get a bit tricky. We load 0 Into X so we can call an indirect address
- sta (pos, X) ; Stores A into 2Bytes of player locations, (MSB and LSB) + 0. Indirect addressing takes 2 bytes
- ; If its given 1 byte, the second byte it uses is its immediate byte: sta (byte, X) = sta byte:byte+1
- ; Where byte and byte+1 become the address passed into STA.
- rts
- undraw:
- lda #$00 ; Load a blank pixel into A
- ldx #$00 ; Setup indirect addressing
- sta (pos, X) ; Store blank pixel into player location on screen
- rts
- negk:
- lda #$00 ; This routine has a simple purpose, clear the key from the memory address
- sta key ; This is just a precaution, as i noticed sometimes the key says and the input persists
- ; Result in a run away player.
- rts
- draw:
- lda #player ; Load the players pixel into A
- jsr loadp ; Jumps to the routine that places the pixel at A into the players screen memory location.
- rts
- blockUpHigh:
- dec y
- clc
- rts
- placePlayerBlock:
- pha
- lda pos ; Stores the LSB of player pos into A
- sec ; Sets the Carry flag, for later use to subtract multiple bytes.
- sbc #$20 ; Subtracts 20, from the LSB 20 = 1 Horizontal screen line of space, pushing the player up on the next line ; Stores the resulting LSB value into its memory location
- ldy $12
- bcc blockUpHigh ; If the carry flag was cleared then we want to move along the MSB once.
- tax ; Tranfer A to X
- pla ; Load block back from A
- jsr nblock ; Place
- rts
- checkCollision:
- lda #$00 ; Clear collision flag
- sta colflag
- ldx #$00 ; Setup indirect addressing
- lda (pos, X) ; Load player location into A
- cmp #$00 ; If next player location has something in it, set collision flag
- bne setcol ; If a block is found, set collision flag
- rts
- setcol:
- lda #$01
- sta colflag
- rts
- chkblock:
- lda key
- cmp #place
- beq pl
- lda key
- cmp #break
- beq br
- rts
- pl:
- lda #blockplaced
- jsr placePlayerBlock
- rts
- br:
- lda #$00
- jsr placePlayerBlock
- rts
- rts
- game:
- lda key ; Now for the key input checking, comparing the keys is easy, moving the player... not so much.
- cmp #up ; Compare the key at A [$ff] with the Up ($77, "w") keys value
- beq mu ; Jump to Up routine if up was pressed.
- cmp #down ; Compare A with the down keys value
- beq md ; Jump to Down routine if up was pressed.
- cmp #left ; Compare A with the left keys value
- beq ml ; Jump to Left routine if up was pressed.
- cmp #right ; Compare A with the right keys value
- beq mr ; Jump to Right routine if up was pressed.
- jsr chkblock
- rts
- mu:
- jsr undraw ; Moving Up
- muDraw:
- jsr getay ; Stores the LSB of player pos into A
- sec ; Sets the Carry flag, for later use to subtract multiple bytes.
- sbc #$20 ; Subtracts 20, from the LSB 20 = 1 Horizontal screen line of space, pushing the player up on the next line
- sta $11 ; Stores the resulting LSB value into its memory location
- bcc mua ; If the carry flag was cleared then we want to move along the MSB once, Jumps to mua routine.
- jsr negk ; Clears key stored in memory
- jsr checkCollision
- lda colflag
- cmp #$01
- beq mdDraw
- jsr draw ; Draws the player
- rts
- mua:
- dec $12 ; Decrement the MSB stored in $12 by 1
- jsr negk ; Clears the stored key
- jsr checkCollision
- lda colflag
- cmp #$01
- beq mdDraw
- jsr draw ; Draw player
- rts
- md: ; Moving Down
- jsr undraw
- mdDraw:
- jsr getay
- clc ; Clear the carry flag
- adc #$20 ; Add 20, Exactly 1 horizontal screen line to the playrs LSB
- sta $11 ; Store that into the LSBs memory location
- bcs mda ; If the number carried over 1 byte, then increment the next byte MSB
- jsr negk
- jsr checkCollision
- lda colflag
- cmp #$01
- beq muDraw
- jsr draw
- rts
- mda:
- inc $12 ; Increment $12 MSB player location by 1
- jsr negk
- jsr checkCollision
- lda colflag
- cmp #$01
- beq muDraw
- jsr draw
- rts
- ml: ; Moving Left
- jsr undraw
- mlDraw:
- dec $11 ; Decrement the players LSB by 1
- jsr negk
- jsr checkCollision
- lda colflag
- cmp #$01
- beq mrDraw
- jsr draw
- rts
- mr: ; Moving Right
- jsr undraw
- mrDraw:
- inc $11 ; Increment the players LSB by 1
- jsr negk
- jsr checkCollision
- lda colflag
- cmp #$01
- beq mlDraw
- jsr draw
- rts
- rts
- loop:
- jsr game ; Loop endlessly
- jmp loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement