Skip to content

Commit 8ef078a

Browse files
committed
paddle stuff
1 parent a952421 commit 8ef078a

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ codegen-units = 1
1414
[build-dependencies]
1515
cc = { version = "1.0"}
1616

17-
[dependencies]
18-
numtoa = "0.2.4"
19-
2017
[features]
2118
skip-legal = []
2219
stress = []

src/chr/tiles.chr

0 Bytes
Binary file not shown.

src/chr/tiles.png

251 Bytes
Loading

src/game.rs

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{ppu, sprites, apu};
1+
use crate::{ppu, sprites, apu, io};
22

33
// statically allocated memory
44
static mut STATE: Option<Game> = None;
@@ -36,6 +36,9 @@ pub fn frame() {
3636
game.step();
3737

3838
sprites::add(TOP_MARGIN + game.ball.x, LEFT_MARGIN + game.ball.y -1, 0x80, 0);
39+
for i in 0..game.paddle.width {
40+
sprites::add(TOP_MARGIN + game.paddle.x + (i * 8), LEFT_MARGIN + game.paddle.y -1, 0x87, 0);
41+
}
3942
}
4043

4144
pub fn render() {
@@ -73,15 +76,16 @@ fn get_rng() -> u8 {
7376
const WIDTH: u8 = 224;
7477
const HEIGHT: u8 = 208;
7578
const BRICKS_WIDE: usize = 14;
76-
const TOP_BRICK_MARGIN: usize = 2;
77-
const BALL_DIAMETER: u8 = 6;
7879
const BRICK_WIDTH: u8 = 16;
7980
const BRICK_HEIGHT: u8 = 8;
81+
const TOP_BRICK_MARGIN: usize = 2;
82+
const BALL_DIAMETER: u8 = 6;
83+
const BALL_RADIUS: u8 = BALL_DIAMETER / 2;
8084
const LEFT_MARGIN: u8 = 16;
8185
const TOP_MARGIN: u8 = 16;
8286

8387
struct Ball { x: u8, y: u8, dx: i8, dy: i8 }
84-
struct Paddle { x: u8, y: u8, width: u8, height: u8 }
88+
struct Paddle { x: u8, y: u8, width: u8 }
8589

8690
#[derive(Copy, Clone, PartialEq)]
8791
enum Brick { Empty, A, B, C }
@@ -125,7 +129,7 @@ impl Game {
125129
fn new() -> Self {
126130
let mut game = Self {
127131
ball: Ball { x: 0, y: HEIGHT / 2, dx: 2, dy: -1 },
128-
paddle: Paddle { x: WIDTH / 2, y: HEIGHT - 10, width: 8, height: 6 },
132+
paddle: Paddle { x: WIDTH / 2, y: HEIGHT - 10, width: 7 },
129133
bricks: [Brick::Empty; 140],
130134
destroyed: [None; 4],
131135
};
@@ -144,24 +148,62 @@ impl Game {
144148
}
145149

146150
fn step(&mut self) {
151+
152+
let buttons = io::controller_buttons();
153+
154+
if buttons & io::Left != 0 && self.paddle.x > 1 {
155+
self.paddle.x -= 2;
156+
} else if buttons & io::Right != 0 {
157+
self.paddle.x += 2;
158+
}
159+
160+
// collision
161+
let old_x = self.ball.x;
162+
let old_y = self.ball.y;
147163
self.ball.x = (self.ball.x as i8 + self.ball.dx) as u8;
148164
self.ball.y = (self.ball.y as i8 + self.ball.dy) as u8;
149165

166+
150167
// brick collision
151168
for (i, brick) in self.bricks.iter_mut().enumerate() {
152169
if *brick != Brick::Empty {
153170
let (brick_x, brick_y) = BRICKS_POS[i];
154171

155172
if self.ball.y > brick_y && self.ball.y < brick_y + BRICK_HEIGHT &&
156173
self.ball.x >= brick_x && self.ball.x <= brick_x + BRICK_WIDTH {
157-
*brick = Brick::Empty;
174+
175+
let brick_x = brick_x as i16;
176+
let brick_y = brick_y as i16;
177+
let x = self.ball.x as i16;
178+
let y = self.ball.y as i16;
179+
let r = BALL_RADIUS as i16;
180+
181+
let dist_left = (x + r) - brick_x;
182+
let dist_right = brick_x + BRICK_WIDTH as i16 - (x + r);
183+
let dist_top = (y + r) - brick_y;
184+
let dist_bottom = brick_y + BRICK_HEIGHT as i16 - (y + r);
185+
186+
let hit_left = dist_left < r;
187+
let hit_right = dist_right < r;
188+
let hit_top = dist_top < r;
189+
let hit_bottom = dist_bottom < r;
190+
191+
if hit_left || hit_right {
192+
self.ball.dx = -self.ball.dx;
193+
}
194+
if hit_top || hit_bottom {
195+
self.ball.dy = -self.ball.dy;
196+
}
158197

159198
let pos = self.destroyed.iter()
160199
.position(|&item| item == None)
161200
.unwrap_or(0);
162201

163202
self.destroyed[pos] = Some(i as u8);
164-
self.ball.dy = -self.ball.dy;
203+
*brick = Brick::Empty;
204+
// rollback if collide
205+
self.ball.x = old_x;
206+
self.ball.y = old_y;
165207
apu::play_sfx(apu::Sfx::MenuBoop);
166208
}
167209
}
@@ -178,3 +220,10 @@ impl Game {
178220
}
179221
}
180222
}
223+
224+
225+
// // Paddle collision
226+
// if self.ball.y >= self.paddle.y && self.ball.y <= self.paddle.y + self.paddle.height &&
227+
// self.ball.x >= self.paddle.x && self.ball.x <= self.paddle.x + self.paddle.width {
228+
// self.ball.dy = -self.ball.dy;
229+
// }

src/io.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ pub fn _set_chr_bank(bank: u8) {
1111
unsafe { *(0x8000 as *mut u8) = bank; }
1212
}
1313

14+
pub const Right: u8 = 0x01;
15+
pub const Left: u8 = 0x02;
16+
pub const Down: u8 = 0x04;
17+
pub const Up: u8 = 0x08;
18+
pub const Start: u8 = 0x10;
19+
pub const Select: u8 = 0x20;
20+
pub const B: u8 = 0x40;
21+
pub const A: u8 = 0x80;
22+
1423
static JOYPAD1: u16 = 0x4016;
1524
static mut BUTTONS: u8 = 0;
1625

0 commit comments

Comments
 (0)