Skip to content

Commit c9f3c81

Browse files
committed
restarting, and working game
1 parent c612d9f commit c9f3c81

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
a proof of concept NES game built with Rust
1+
a template for writing NES games in Rust
2+
3+
includes an example game that uses sprites, background tiles, sfx, and controller input
24

35
## building
46

57
```bash
68
node src/chr/convert.js src/chr
79
docker pull mrkits/rust-mos
8-
docker run -it --name rustmos --entrypoint bash -v ${HOME}/tetris/rust:/hostfiles mrkits/rust-mos
10+
docker run -it --name rustmos --entrypoint bash -v ${HOME}/rust-nes-template:/hostfiles mrkits/rust-mos
911
docker container exec -it rustmos /bin/bash
1012
cargo rustc --release
1113
```

src/game.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,18 @@ impl Game {
148148
}
149149

150150
fn step(&mut self) {
151-
152151
let buttons = io::controller_buttons();
153152

153+
if self.ball.dy == 0 {
154+
// dead
155+
if buttons & io::START != 0 {
156+
ppu::disable_nmi();
157+
init();
158+
ppu::enable_nmi();
159+
}
160+
return
161+
}
162+
154163
if buttons & io::LEFT != 0 && self.paddle.x > 1 {
155164
self.paddle.x -= 2;
156165
} else if buttons & io::RIGHT != 0 && self.paddle.x + self.paddle.width * 8 < 0xe0 {
@@ -189,9 +198,8 @@ impl Game {
189198
let hit_bottom = dist_bottom < r;
190199

191200
if hit_left || hit_right {
192-
// self.ball.dx = -self.ball.dx;
193-
}
194-
if hit_top || hit_bottom {
201+
self.ball.dx = -self.ball.dx;
202+
} else if hit_top || hit_bottom {
195203
self.ball.dy = -self.ball.dy;
196204
}
197205

@@ -220,7 +228,7 @@ impl Game {
220228
}
221229
// paddle collision
222230
if self.ball.y + BALL_DIAMETER >= self.paddle.y {
223-
if self.ball.x > self.paddle.x && self.ball.x + BALL_DIAMETER < self.paddle.x + (self.paddle.width * 8) {
231+
if self.ball.x + BALL_RADIUS > self.paddle.x && self.ball.x + BALL_RADIUS < self.paddle.x + (self.paddle.width * 8) {
224232
self.ball.dy = -self.ball.dy;
225233
apu::play_sfx(apu::Sfx::Lock);
226234
} else {

src/link.ld

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
/* default load chain */
2-
/* ld.lld: /usr/local/bin/../mos-platform/nes-cnrom/lib/crt0.o */
3-
/* ld.lld: /usr/local/bin/../mos-platform/nes/lib/libcrt0.a */
4-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/libcrt.a */
5-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/libc.a */
6-
/* ld.lld: /usr/local/bin/../mos-platform/nes-cnrom/lib/link.ld */
7-
/* ld.lld: /usr/local/bin/../mos-platform/nes-cnrom/lib/common.ld */
8-
/* ld.lld: /usr/local/bin/../mos-platform/nes/lib/nes.ld */
9-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/imag-regs.ld */
10-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/text-sections.ld */
11-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/rodata-sections.ld */
12-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/data-sections.ld */
13-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/zp-data-sections.ld */
14-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/zp-bss-sections.ld */
15-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/zp-bss-symbols.ld */
16-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/zp-noinit.ld */
17-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/zp-noinit-sections.ld */
18-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/bss-sections.ld */
19-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/bss-symbols.ld */
20-
/* ld.lld: /usr/local/bin/../mos-platform/common/lib/noinit-sections.ld */
21-
/* ld.lld: /usr/local/bin/../mos-platform/nes/lib/prg-ram-fixed.ld */
22-
/* ld.lld: /usr/local/bin/../mos-platform/nes/lib/prg-rom-fixed.ld */
23-
/* ld.lld: /usr/local/bin/../mos-platform/nes/lib/dpcm.ld */
24-
/* ld.lld: /usr/local/bin/../mos-platform/nes/lib/ines.ld */
25-
/* ld.lld: /usr/local/bin/../mos-platform/nes/lib/ines-header.ld */
26-
/* ld.lld: /usr/local/bin/../mos-platform/nes/lib/ines-roms.ld */
27-
/* ld.lld: /usr/local/bin/../mos-platform/nes/lib/c-in-ram.ld */
2+
/* ld.lld: .../mos-platform/nes-cnrom/lib/crt0.o */
3+
/* ld.lld: .../mos-platform/nes/lib/libcrt0.a */
4+
/* ld.lld: .../mos-platform/common/lib/libcrt.a */
5+
/* ld.lld: .../mos-platform/common/lib/libc.a */
6+
/* ld.lld: .../mos-platform/nes-cnrom/lib/link.ld */
7+
/* ld.lld: .../mos-platform/nes-cnrom/lib/common.ld */
8+
/* ld.lld: .../mos-platform/nes/lib/nes.ld */
9+
/* ld.lld: .../mos-platform/common/lib/imag-regs.ld */
10+
/* ld.lld: .../mos-platform/common/lib/text-sections.ld */
11+
/* ld.lld: .../mos-platform/common/lib/rodata-sections.ld */
12+
/* ld.lld: .../mos-platform/common/lib/data-sections.ld */
13+
/* ld.lld: .../mos-platform/common/lib/zp-data-sections.ld */
14+
/* ld.lld: .../mos-platform/common/lib/zp-bss-sections.ld */
15+
/* ld.lld: .../mos-platform/common/lib/zp-bss-symbols.ld */
16+
/* ld.lld: .../mos-platform/common/lib/zp-noinit.ld */
17+
/* ld.lld: .../mos-platform/common/lib/zp-noinit-sections.ld */
18+
/* ld.lld: .../mos-platform/common/lib/bss-sections.ld */
19+
/* ld.lld: .../mos-platform/common/lib/bss-symbols.ld */
20+
/* ld.lld: .../mos-platform/common/lib/noinit-sections.ld */
21+
/* ld.lld: .../mos-platform/nes/lib/prg-ram-fixed.ld */
22+
/* ld.lld: .../mos-platform/nes/lib/prg-rom-fixed.ld */
23+
/* ld.lld: .../mos-platform/nes/lib/dpcm.ld */
24+
/* ld.lld: .../mos-platform/nes/lib/ines.ld */
25+
/* ld.lld: .../mos-platform/nes/lib/ines-header.ld */
26+
/* ld.lld: .../mos-platform/nes/lib/ines-roms.ld */
27+
/* ld.lld: .../mos-platform/nes/lib/c-in-ram.ld */
2828

2929
__chr_rom_size = 16;
30-
/* __prg_rom_size = 32; */
3130

3231
/* Provide imaginary (zero page) registers. */
3332
__rc0 = 0x00;
@@ -54,8 +53,11 @@ ASSERT(__chr_nvram_size == 0 ||
5453
MEMORY {
5554
zp : ORIGIN = __rc31 + 1, LENGTH = 0x100 - (__rc31 + 1)
5655

56+
/* reserved for inline asm usage */
57+
_inline_nmi: ORIGIN = 0x80, LENGTH = 1
58+
5759
/* reserved for sprites */
58-
sprites : ORIGIN = 0x0200, LENGTH = 0x0100
60+
_sprites : ORIGIN = 0x0200, LENGTH = 0x0100
5961

6062
ram : ORIGIN = 0x0300, LENGTH = 0x0500
6163

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod sprites;
99
mod game;
1010

1111
// fixed memory usage;
12-
// 0x80 - nmi check bit (usually free)
12+
// 0x80 - nmi check bit
1313
// 0x200 - OAM (reserved in linker)
1414

1515
#[start]

0 commit comments

Comments
 (0)