@@ -104,6 +104,23 @@ struct Game {
104104 destroyed : [ Option < u8 > ; 4 ] ,
105105}
106106
107+ const BRICKS_POS : [ ( u8 , u8 ) ; 140 ] = {
108+ let mut pos = [ ( 0u8 , 0u8 ) ; 140 ] ;
109+
110+ let mut brick_index = 0 ;
111+ while brick_index < 140 {
112+ let i = brick_index as u8 ;
113+ let brick_y = i / BRICKS_WIDE as u8 ;
114+ let brick_x = i % BRICKS_WIDE as u8 ;
115+ let brick_y = ( brick_y * BRICK_HEIGHT ) + ( TOP_BRICK_MARGIN as u8 * BRICK_HEIGHT ) ;
116+ let brick_x = ( brick_x as u16 * BRICK_WIDTH as u16 ) as u8 ;
117+ pos[ brick_index] = ( brick_x, brick_y) ;
118+
119+ brick_index += 1 ;
120+ }
121+ pos
122+ } ;
123+
107124impl Game {
108125 fn new ( ) -> Self {
109126 let mut game = Self {
@@ -133,11 +150,7 @@ impl Game {
133150 // brick collision
134151 for ( i, brick) in self . bricks . iter_mut ( ) . enumerate ( ) {
135152 if * brick != Brick :: Empty {
136- let i = i as u8 ;
137- let brick_y = i / BRICKS_WIDE as u8 ;
138- let brick_x = i % BRICKS_WIDE as u8 ;
139- let brick_y = ( brick_y * BRICK_HEIGHT ) + ( TOP_BRICK_MARGIN as u8 * BRICK_HEIGHT ) ;
140- let brick_x = ( brick_y * BRICK_WIDTH ) ;
153+ let ( brick_x, brick_y) = BRICKS_POS [ i] ;
141154
142155 if self . ball . y > brick_y && self . ball . y < brick_y + BRICK_HEIGHT &&
143156 self . ball . x >= brick_x && self . ball . x <= brick_x + BRICK_WIDTH {
@@ -147,7 +160,7 @@ impl Game {
147160 . position ( |& item| item == None )
148161 . unwrap_or ( 0 ) ;
149162
150- self . destroyed [ pos] = Some ( i) ;
163+ self . destroyed [ pos] = Some ( i as u8 ) ;
151164 self . ball . dy = -self . ball . dy ;
152165 apu:: play_sfx ( apu:: Sfx :: MenuBoop ) ;
153166 }
@@ -165,57 +178,3 @@ impl Game {
165178 }
166179 }
167180}
168-
169- // struct Brick { x: u8, y: u8, width: u8, height: u8, is_destroyed: bool }
170- // struct Game { ball: Ball, paddle: Paddle, bricks: Vec<Brick> }
171-
172- // impl Game {
173- // fn new() -> Game {
174- // let mut bricks = Vec::new();
175- // for i in 0..50 {
176- // for j in 0..30 {
177- // bricks.push(Brick { x: i * 8 + 1, y: j * 3 + 1, width: 6, height: 2, is_destroyed: false });
178- // }
179- // }
180- // Game {
181- // ball: ,
182- // paddle: Paddle { x: 36, y: 48, width: 8, height: 1 },
183- // bricks: bricks
184- // }
185- // }
186-
187- // fn update(&mut self) {
188- // self.ball.x = (self.ball.x as i8 + self.ball.dx) as u8;
189- // self.ball.y = (self.ball.y as i8 + self.ball.dy) as u8;
190-
191- // // Paddle collision
192- // if self.ball.y >= self.paddle.y && self.ball.y <= self.paddle.y + self.paddle.height &&
193- // self.ball.x >= self.paddle.x && self.ball.x <= self.paddle.x + self.paddle.width {
194- // self.ball.dy = -self.ball.dy;
195- // }
196-
197- // // Brick collision
198- // let mut destroyed = HashSet::new();
199- // for (i, brick) in self.bricks.iter().enumerate() {
200- // if !brick.is_destroyed &&
201- // self.ball.y >= brick.y && self.ball.y <= brick.y + brick.height &&
202- // self.ball.x >= brick.x && self.ball.x <= brick.x + brick.width {
203- // destroyed.insert(i);
204- // self.ball.dy = -self.ball.dy;
205- // }
206- // }
207-
208- // // Remove destroyed bricks
209- // for i in destroyed {
210- // self.bricks[i].is_destroyed = true;
211- // }
212-
213- // // Screen collision
214- // if self.ball.x == 0 || self.ball.x == 79 {
215- // self.ball.dx = -self.ball.dx;
216- // }
217- // if self.ball.y == 0 {
218- // self.ball.dy = -self.ball.dy;
219- // }
220- // }
221- // }
0 commit comments