1- use crate :: { ppu, sprites, apu} ;
1+ use crate :: { ppu, sprites, apu, io } ;
22
33// statically allocated memory
44static 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
4144pub fn render ( ) {
@@ -73,15 +76,16 @@ fn get_rng() -> u8 {
7376const WIDTH : u8 = 224 ;
7477const HEIGHT : u8 = 208 ;
7578const BRICKS_WIDE : usize = 14 ;
76- const TOP_BRICK_MARGIN : usize = 2 ;
77- const BALL_DIAMETER : u8 = 6 ;
7879const BRICK_WIDTH : u8 = 16 ;
7980const 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 ;
8084const LEFT_MARGIN : u8 = 16 ;
8185const TOP_MARGIN : u8 = 16 ;
8286
8387struct 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 ) ]
8791enum 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+ // }
0 commit comments