.include "constant.inc"
.include "header.inc"
.segment "ZEROPAGE"
player_x : .res 1
player_y : .res 1
player_dir_x : .res 1
player_dir_y : .res 1
pad_x : .res 1
pad_y : .res 1
pad_dir : .res 1
pad_ctrl : .res 1
.exportzp player_x, player_y, pad_x, pad_y, pad_ctrl
.segment "CODE"
.proc irq_handler
RTI
.endproc
.import read_controller1
.proc nmi_handler
lda #$00
sta oamaddr
lda #$02
sta oamdma
lda #$00
jsr detect_collision
jsr read_controller1
jsr update_pad
jsr update_player
jsr draw_player
jsr draw_pad
sta ppuscrl
sta ppuscrl
RTI
.endproc
.import reset_handler
.export main
.proc main
;palette address starts from 3f00
ldx ppustatus
ldx #$3f
stx ppuaddr
ldx #$00
stx ppuaddr
ldx #$00
load_pallettes:
lda pallettes, x
sta ppudata
inx
cpx #$20
bne load_pallettes
ldx #$00
lda #$c8
sta $0200
lda #$01
sta $0201
lda #$19
sta $0202
lda #$80
sta $0203
vblankwait:
bit $2002
bpl vblankwait
lda #%10010000
sta ppuctrl
lda #%00011110
sta ppumask
forever:
jmp forever
.endproc
.proc detect_collision
php
pha
txa
pha
tya
pha
lda player_x
cmp pad_x
bcc no_collision
lda player_x
clc
adc #$10
cmp pad_x
bcs no_collision
lda player_y
cmp #$cc
bne no_collision
lda #$00
sta player_dir_y
no_collision:
pla
tay
pla
tax
pla
plp
rts
.endproc
.proc update_player
PHP
PHA
TXA
PHA
TYA
PHA
LDA player_x
CMP #$e0
BCC not_at_right_edge_x
; if BCC is not taken, we are greater than $e0
LDA #$00
STA player_dir_x ; start moving left
JMP direction_set_x
not_at_right_edge_x:
LDA player_x
CMP #$08
BCS direction_set_x
; if BCS not taken, we are less than $10
LDA #$01
STA player_dir_x ; start moving right
direction_set_x:
; now update player_x
LDA player_dir_x
CMP #$01
BEQ move_right_x
DEC player_x
JMP not_at_bottom_edge_y ; skip Y direction update for now
move_right_x:
INC player_x
not_at_bottom_edge_y:
; Update Y direction
LDA player_y
CMP #$e0
BCC not_at_top_edge_y
; if BCC is not taken, we are greater than $e0
LDA #$00
STA player_dir_y ; start moving up
JMP direction_set_y
not_at_top_edge_y:
LDA player_y
CMP #$08
BCS direction_set_y
; if BCS not taken, we are less than $10
LDA #$01
STA player_dir_y ; start moving down
direction_set_y:
; now update player_y
LDA player_dir_y
CMP #$01
BEQ move_down_y
DEC player_y
JMP exit_subroutine
move_down_y:
INC player_y
jsr detect_collision
exit_subroutine:
; all done, clean up and return
PLA
TAY
PLA
TAX
PLA
PLP
RTS
.endproc
.proc update_pad
php
pha
txa
pha
tya
pha
lda pad_ctrl
and #btn_left
beq check_right
lda pad_x
cmp #$10
bcc done_chechking
dec pad_x
jmp done_chechking
check_right:
lda pad_ctrl
and #btn_right
beq done_chechking
lda pad_x
cmp #$e0
bcs done_chechking
inc pad_x
done_chechking:
pla
tay
pla
tax
pla
plp
rts
.endproc
.proc draw_player
php
pha
txa
pha
tya
pha
lda #$01;sprite number
sta $0201
lda #$00;sprite attribute
sta $0202
lda player_y
sta $0200;sprite y coordinate
lda player_x
sta $0203; sprite x coordinate
pla
tay
pla
tax
pla
plp
rts
.endproc
.proc draw_pad
php
pha
txa
pha
tya
pha
;drawing pad tile numbers
lda #$02
sta $0205
lda #$03
sta $0209
;drawing attribute
lda #$00
sta $0206
sta $020a
;first sprite position x
lda pad_x
sta $0207
lda pad_y
sta $0204
;second sprite position x+8
lda pad_x
clc
adc #$08
sta $020b
lda pad_y
sta $0208
pla
tay
pla
tax
pla
plp
rts
.endproc
.segment "VECTORS"
.addr nmi_handler, reset_handler, irq_handler
.segment "RODATA"
pallettes:
.byte $0f, $00, $10, $30
.byte $0f, $01, $21, $31
.byte $0f, $06, $16, $26
.byte $0f, $09, $19, $29
.byte $0f, $00, $10, $30
.byte $0f, $01, $21, $31
.byte $0f, $06, $16, $26
.byte $0f, $09, $19, $29
.segment "CHR"
.incbin "ball.chr"
so this is my code , but the collision detection is not working . (I want to make a brick breaker game)
.include "header.inc"
.segment "ZEROPAGE"
player_x : .res 1
player_y : .res 1
player_dir_x : .res 1
player_dir_y : .res 1
pad_x : .res 1
pad_y : .res 1
pad_dir : .res 1
pad_ctrl : .res 1
.exportzp player_x, player_y, pad_x, pad_y, pad_ctrl
.segment "CODE"
.proc irq_handler
RTI
.endproc
.import read_controller1
.proc nmi_handler
lda #$00
sta oamaddr
lda #$02
sta oamdma
lda #$00
jsr detect_collision
jsr read_controller1
jsr update_pad
jsr update_player
jsr draw_player
jsr draw_pad
sta ppuscrl
sta ppuscrl
RTI
.endproc
.import reset_handler
.export main
.proc main
;palette address starts from 3f00
ldx ppustatus
ldx #$3f
stx ppuaddr
ldx #$00
stx ppuaddr
ldx #$00
load_pallettes:
lda pallettes, x
sta ppudata
inx
cpx #$20
bne load_pallettes
ldx #$00
lda #$c8
sta $0200
lda #$01
sta $0201
lda #$19
sta $0202
lda #$80
sta $0203
vblankwait:
bit $2002
bpl vblankwait
lda #%10010000
sta ppuctrl
lda #%00011110
sta ppumask
forever:
jmp forever
.endproc
.proc detect_collision
php
pha
txa
pha
tya
pha
lda player_x
cmp pad_x
bcc no_collision
lda player_x
clc
adc #$10
cmp pad_x
bcs no_collision
lda player_y
cmp #$cc
bne no_collision
lda #$00
sta player_dir_y
no_collision:
pla
tay
pla
tax
pla
plp
rts
.endproc
.proc update_player
PHP
PHA
TXA
PHA
TYA
PHA
LDA player_x
CMP #$e0
BCC not_at_right_edge_x
; if BCC is not taken, we are greater than $e0
LDA #$00
STA player_dir_x ; start moving left
JMP direction_set_x
not_at_right_edge_x:
LDA player_x
CMP #$08
BCS direction_set_x
; if BCS not taken, we are less than $10
LDA #$01
STA player_dir_x ; start moving right
direction_set_x:
; now update player_x
LDA player_dir_x
CMP #$01
BEQ move_right_x
DEC player_x
JMP not_at_bottom_edge_y ; skip Y direction update for now
move_right_x:
INC player_x
not_at_bottom_edge_y:
; Update Y direction
LDA player_y
CMP #$e0
BCC not_at_top_edge_y
; if BCC is not taken, we are greater than $e0
LDA #$00
STA player_dir_y ; start moving up
JMP direction_set_y
not_at_top_edge_y:
LDA player_y
CMP #$08
BCS direction_set_y
; if BCS not taken, we are less than $10
LDA #$01
STA player_dir_y ; start moving down
direction_set_y:
; now update player_y
LDA player_dir_y
CMP #$01
BEQ move_down_y
DEC player_y
JMP exit_subroutine
move_down_y:
INC player_y
jsr detect_collision
exit_subroutine:
; all done, clean up and return
PLA
TAY
PLA
TAX
PLA
PLP
RTS
.endproc
.proc update_pad
php
pha
txa
pha
tya
pha
lda pad_ctrl
and #btn_left
beq check_right
lda pad_x
cmp #$10
bcc done_chechking
dec pad_x
jmp done_chechking
check_right:
lda pad_ctrl
and #btn_right
beq done_chechking
lda pad_x
cmp #$e0
bcs done_chechking
inc pad_x
done_chechking:
pla
tay
pla
tax
pla
plp
rts
.endproc
.proc draw_player
php
pha
txa
pha
tya
pha
lda #$01;sprite number
sta $0201
lda #$00;sprite attribute
sta $0202
lda player_y
sta $0200;sprite y coordinate
lda player_x
sta $0203; sprite x coordinate
pla
tay
pla
tax
pla
plp
rts
.endproc
.proc draw_pad
php
pha
txa
pha
tya
pha
;drawing pad tile numbers
lda #$02
sta $0205
lda #$03
sta $0209
;drawing attribute
lda #$00
sta $0206
sta $020a
;first sprite position x
lda pad_x
sta $0207
lda pad_y
sta $0204
;second sprite position x+8
lda pad_x
clc
adc #$08
sta $020b
lda pad_y
sta $0208
pla
tay
pla
tax
pla
plp
rts
.endproc
.segment "VECTORS"
.addr nmi_handler, reset_handler, irq_handler
.segment "RODATA"
pallettes:
.byte $0f, $00, $10, $30
.byte $0f, $01, $21, $31
.byte $0f, $06, $16, $26
.byte $0f, $09, $19, $29
.byte $0f, $00, $10, $30
.byte $0f, $01, $21, $31
.byte $0f, $06, $16, $26
.byte $0f, $09, $19, $29
.segment "CHR"
.incbin "ball.chr"
so this is my code , but the collision detection is not working . (I want to make a brick breaker game)
Statistics: Posted by shahriarrafsun — Sun Jan 14, 2024 10:37 pm — Replies 1 — Views 123