Skip to content

Commit

Permalink
ショットを打てるようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoji Suzuki committed Dec 20, 2018
1 parent f0fe86c commit c6750ea
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 23 deletions.
18 changes: 13 additions & 5 deletions README.md
Expand Up @@ -9,7 +9,7 @@ NESのプログラミングを勉強するためシンプルなSTGを作って
- [x] スプライトで自機を表示
- [x] ジョイパッドのカーソルで自機を左右に移動
- [ ] 画面レイアウト(ギャラガ風にする)
- [ ] ジョイパッドのA/Bボタンでショットを発射
- [x] ジョイパッドのAボタンでショットを発射 (最大4連射)
- [ ] 敵機を登場させる
- [ ] 敵機をショットで破壊できるようにする
- [ ] スコアを表示
Expand All @@ -35,15 +35,23 @@ make
```
$00: プレイヤのX座標
$01: プレイヤのY座標
$02: ショットのindex (0, 4, 8, 12, 0...)
$03: ショットの発射禁止フラグ (0なら発射許可)
$04〜$07: ショット構造体 (f: 発射中, x: 座標, y: 座標, i: 未使用)
$08〜$0b: ショット構造体 (f: 発射中, x: 座標, y: 座標, i: 未使用)
$0c〜$0f: ショット構造体 (f: 発射中, x: 座標, y: 座標, i: 未使用)
$10〜$13: ショット構造体 (f: 発射中, x: 座標, y: 座標, i: 未使用)
```

> ショット構造体は0ページではなく別のWRAMに移すかも
### Sprite (DMA: $0300〜$03FF)

```
+$0000: sp_player1: プレイヤの左上
+$0004: sp_player2: プレイヤの右上
+$0008: sp_player3: プレイヤの左下
+$000c: sp_player4: プレイヤの右下
$0300: sp_player1: プレイヤの左上
$0304: sp_player2: プレイヤの右上
$0308: sp_player3: プレイヤの左下
$030c: sp_player4: プレイヤの右下
```

## License
Expand Down
Binary file modified screenshot.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified stg-sprite.bmp
Binary file not shown.
169 changes: 151 additions & 18 deletions stg.asm
Expand Up @@ -36,10 +36,17 @@ copy_pal:
dey
bne copy_pal

; write mask on the left top
lda #$20
sta $2006
lda #$00
sta $2006
sta $2007

; write string to the name table (SCORE)
lda #$20
sta $2006
lda #$25
lda #$45
sta $2006
ldx #$00
ldy #$16
Expand Down Expand Up @@ -93,18 +100,26 @@ copy_map2:
lda #$00
sta $2003

ldx #$00
lda #$00
clear_sprite_area:
sta $0300, x
inx
bne clear_sprite_area


; setup player variables
lda #$70
sta v_playerX
tax
lda #$d0
sta v_playerY
tay
tay

; setup player sprite (1: left-top)
tya
sta sp_player1
lda #$00
lda #$01
sta sp_player1 + 1
lda #%00100000
sta sp_player1 + 2
Expand All @@ -114,19 +129,21 @@ copy_map2:
; setup player sprite (2: right-top)
tya
sta sp_player2
lda #$01
lda #$02
sta sp_player2 + 1
lda #%00100000
sta sp_player2 + 2
txa
clc
adc #$08
sta sp_player2 + 3

; setup player sprite (3: left-bottom)
tya
clc
adc #$08
sta sp_player3
lda #$02
lda #$03
sta sp_player3 + 1
lda #%00100000
sta sp_player3 + 2
Expand All @@ -135,16 +152,40 @@ copy_map2:

; setup player sprite (4: right-bottom)
tya
clc
adc #$08
sta sp_player4
lda #$03
lda #$04
sta sp_player4 + 1
lda #%00100000
sta sp_player4 + 2
txa
clc
adc #$08
sta sp_player4 + 3

; setup shot sprites & variables
lda #$00
sta v_shot_idx
sta v_shot_ng
ldx #$00
ldy #$04
setup_shot_sprites:
lda #$00
sta v_shot0_f, x
sta sp_shot0, x
sta sp_shot0 + 3, x
lda #$04
sta sp_shot0 + 1, x
lda #%00100000
sta sp_shot0 + 2, x
txa
clc
adc #$04
tax
dey
bne setup_shot_sprites

; loop infinite
mainloop:
; clear joy-pad
Expand All @@ -155,6 +196,53 @@ mainloop:

moveloop_inputCheck:
lda $4016 ; A
and #$01
bne mainloop_addNewShot
; reset ng flag if not push A
lda #$00
sta v_shot_ng
jmp mainloop_endFireShot

mainloop_addNewShot:
lda v_shot_ng
bne mainloop_suppressFireShot
lda #$20
sta v_shot_ng

ldx v_shot_idx
lda #$01
sta v_shot0_f, x
lda v_playerX
clc
adc #$04
sta v_shot0_x, x
lda v_playerY
adc #$FF
sta v_shot0_y, x

; initialize sprite of shot
sta sp_shot0, x
lda #$05
sta sp_shot0 + 1, x
lda #%00100000
sta sp_shot0 + 2, x
lda v_shot0_x, x
sta sp_shot0 + 3, x

inx
inx
inx
inx
txa
and #$0f
sta v_shot_idx

mainloop_suppressFireShot:
ldx v_shot_ng
dex
stx v_shot_ng

mainloop_endFireShot:
lda $4016 ; B
lda $4016 ; SELECT
lda $4016 ; START
Expand All @@ -177,21 +265,23 @@ mainloop_moveUp:
sta v_playerY
sta sp_player1
sta sp_player2
adc #$07 ; NOTE: 原因不明だが上移動時は+8すると1pxズレるので+7にしておく
clc
adc #$08
sta sp_player3
sta sp_player4
jmp mainloop_inputCheck_LR

mainloop_moveDown:
ldx v_playerY
cpx #$E0
bcs mainloop_moveEnd ; do not move if 224 <= y
cpx #$D8
bcs mainloop_moveEnd ; do not move if 216 <= y
inx
inx
txa
sta v_playerY
sta sp_player1
sta sp_player2
clc
adc #$08
sta sp_player3
sta sp_player4
Expand All @@ -215,7 +305,8 @@ mainloop_moveLeft:
sta v_playerX
sta sp_player1 + 3
sta sp_player3 + 3
adc #$07 ; NOTE: 原因不明だが左移動時は+8すると1pxズレるので+7にしておく
clc
adc #$08
sta sp_player2 + 3
sta sp_player4 + 3
jmp mainloop_moveEnd
Expand All @@ -230,18 +321,46 @@ mainloop_moveRight:
sta v_playerX
sta sp_player1 + 3
sta sp_player3 + 3
clc
adc #$08
sta sp_player2 + 3
sta sp_player4 + 3

mainloop_moveEnd:

ldx #$00
mainloop_moveShot:
; check flag
lda v_shot0_f, x
beq mainloop_moveShot_erase
lda v_shot0_y, x
clc
adc #$FA
cmp #$08
bcc mainloop_moveShot_erase
; store Y
sta v_shot0_y, x
sta sp_shot0, x
jmp mainloop_moveShot_next
mainloop_moveShot_erase:
lda #$00
sta v_shot0_f, x
sta sp_shot0, x
sta sp_shot0 + 1, x
sta sp_shot0 + 3, x
mainloop_moveShot_next:
txa
clc
adc #$04
tax
and #$0f
bne mainloop_moveShot

mainloop_sprite_DMA:; WRAM $0300 ~ $03FF -> Sprite
lda $2002
bpl mainloop_sprite_DMA
bpl mainloop_sprite_DMA ; wait for vBlank
lda #$3
sta $4014

jmp mainloop
.endproc

Expand Down Expand Up @@ -270,14 +389,28 @@ string_score:
.byte "SC 0000000 HI 0000000"

.org $0000
v_playerX: .byt $00
v_playerY: .byt $00
v_playerX: .byte $00 ; 自機のX座標
v_playerY: .byte $00 ; 自機のY座標
v_shot_idx: .byte $00 ; ショットのindex
v_shot_ng: .byte $00 ; ショットの発射禁止フラグ (0の時のみ発射許可)

v_shot0_f: .byte $00 ; ショットの生存フラグ
v_shot0_x: .byte $00 ; ショットのX座標
v_shot0_y: .byte $00 ; ショットのY座標
v_shot0_i: .byte $00 ; 未使用 (バウンダリ)
.byte $00, $00, $00, $00
.byte $00, $00, $00, $00
.byte $00, $00, $00, $00

.org $0300 ; Y TILE ATTR X description
sp_player1: .byt $00, $00, $00, $00 ; player (left-top)
sp_player2: .byt $00, $00, $00, $00 ; player (right-top)
sp_player3: .byt $00, $00, $00, $00 ; player (left-bottom)
sp_player4: .byt $00, $00, $00, $00 ; player (right-bottom)
sp_player1: .byte $00, $00, $00, $00 ; player (left-top)
sp_player2: .byte $00, $00, $00, $00 ; player (right-top)
sp_player3: .byte $00, $00, $00, $00 ; player (left-bottom)
sp_player4: .byte $00, $00, $00, $00 ; player (right-bottom)
sp_shot0: .byte $00, $00, $00, $00 ; shot (0)
sp_shot1: .byte $00, $00, $00, $00 ; shot (1)
sp_shot2: .byte $00, $00, $00, $00 ; shot (2)
sp_shot3: .byte $00, $00, $00, $00 ; shot (3)

.segment "VECINFO"
.word $0000
Expand Down

0 comments on commit c6750ea

Please sign in to comment.