1
1
use crate :: control:: GameControlArg ;
2
- use crate :: matrix:: { GameState , Grid , State , HEIGHT , LEDS , WIDTH } ;
3
-
4
- use heapless:: Vec ;
2
+ use crate :: matrix:: { GameState , Grid , State , HEIGHT , WIDTH } ;
5
3
6
4
const PADDLE_WIDTH : usize = 5 ;
7
5
@@ -23,13 +21,14 @@ struct Ball {
23
21
24
22
#[ derive( Clone ) ]
25
23
pub struct PongState {
24
+ // TODO: Properly calculate score and display it
26
25
score : Score ,
27
26
ball : Ball ,
28
27
paddles : ( usize , usize ) ,
29
28
pub speed : u64 ,
30
29
}
31
30
32
- pub fn start_game ( state : & mut State , random : u8 ) {
31
+ pub fn start_game ( state : & mut State , _random : u8 ) {
33
32
state. game = Some ( GameState :: Pong ( PongState {
34
33
score : Score { upper : 0 , lower : 0 } ,
35
34
ball : Ball {
@@ -95,31 +94,18 @@ fn hit_paddle(ball: Position, paddles: (usize, usize)) -> Option<usize> {
95
94
}
96
95
}
97
96
98
- pub fn game_step ( state : & mut State , random : u8 ) {
97
+ pub fn game_step ( state : & mut State , _random : u8 ) {
99
98
if let Some ( GameState :: Pong ( ref mut pong_state) ) = state. game {
100
99
pong_state. ball . pos = {
101
100
let ( vx, vy) = pong_state. ball . direction ;
102
- let ( x, y) = pong_state. ball . pos ;
103
101
let ( x, y) = add_velocity ( pong_state. ball . pos , pong_state. ball . direction ) ;
104
- let x = if x < 0 {
105
- 0
106
- } else if x > WIDTH - 1 {
107
- WIDTH - 1
108
- } else {
109
- x
110
- } ;
102
+ let x = if x > WIDTH - 1 { WIDTH - 1 } else { x } ;
111
103
if x == 0 || x == WIDTH - 1 {
112
104
// Hit wall, bounce back
113
105
pong_state. ball . direction = ( -vx, vy) ;
114
106
}
115
107
116
- let y = if y < 0 {
117
- 0
118
- } else if y > HEIGHT - 1 {
119
- HEIGHT - 1
120
- } else {
121
- y
122
- } ;
108
+ let y = if y > HEIGHT - 1 { HEIGHT - 1 } else { y } ;
123
109
let ( x, y) = if let Some ( paddle_hit) = hit_paddle ( ( x, y) , pong_state. paddles ) {
124
110
// Hit paddle, bounce back
125
111
// TODO: Change vy direction slightly depending on where the paddle was hit
0 commit comments