1
- use crate :: { ppu, sprites, apu} ;
1
+ use crate :: { ppu, sprites, apu, io } ;
2
2
3
3
// statically allocated memory
4
4
static mut STATE : Option < Game > = None ;
@@ -36,6 +36,9 @@ pub fn frame() {
36
36
game. step ( ) ;
37
37
38
38
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
+ }
39
42
}
40
43
41
44
pub fn render ( ) {
@@ -73,15 +76,16 @@ fn get_rng() -> u8 {
73
76
const WIDTH : u8 = 224 ;
74
77
const HEIGHT : u8 = 208 ;
75
78
const BRICKS_WIDE : usize = 14 ;
76
- const TOP_BRICK_MARGIN : usize = 2 ;
77
- const BALL_DIAMETER : u8 = 6 ;
78
79
const BRICK_WIDTH : u8 = 16 ;
79
80
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 ;
80
84
const LEFT_MARGIN : u8 = 16 ;
81
85
const TOP_MARGIN : u8 = 16 ;
82
86
83
87
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 }
85
89
86
90
#[ derive( Copy , Clone , PartialEq ) ]
87
91
enum Brick { Empty , A , B , C }
@@ -125,7 +129,7 @@ impl Game {
125
129
fn new ( ) -> Self {
126
130
let mut game = Self {
127
131
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 } ,
129
133
bricks : [ Brick :: Empty ; 140 ] ,
130
134
destroyed : [ None ; 4 ] ,
131
135
} ;
@@ -144,24 +148,62 @@ impl Game {
144
148
}
145
149
146
150
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 ;
147
163
self . ball . x = ( self . ball . x as i8 + self . ball . dx ) as u8 ;
148
164
self . ball . y = ( self . ball . y as i8 + self . ball . dy ) as u8 ;
149
165
166
+
150
167
// brick collision
151
168
for ( i, brick) in self . bricks . iter_mut ( ) . enumerate ( ) {
152
169
if * brick != Brick :: Empty {
153
170
let ( brick_x, brick_y) = BRICKS_POS [ i] ;
154
171
155
172
if self . ball . y > brick_y && self . ball . y < brick_y + BRICK_HEIGHT &&
156
173
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
+ }
158
197
159
198
let pos = self . destroyed . iter ( )
160
199
. position ( |& item| item == None )
161
200
. unwrap_or ( 0 ) ;
162
201
163
202
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;
165
207
apu:: play_sfx ( apu:: Sfx :: MenuBoop ) ;
166
208
}
167
209
}
@@ -178,3 +220,10 @@ impl Game {
178
220
}
179
221
}
180
222
}
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