@@ -15,6 +15,7 @@ pub struct Game {
1515 ball_velocity : Vector2 ,
1616 ball_speed : f32 ,
1717 ball_radius : f32 ,
18+ ball_position_history : [ Vector2 ; 10 ] ,
1819 player_position : Vector2 ,
1920 player_size : Vector2 ,
2021 player_speed : f32 ,
@@ -32,18 +33,19 @@ impl Game {
3233 Self {
3334 ball_position : /* center screen */ Vector2 :: new ( rl. get_screen_width ( ) as f32 / 2.0 , rl. get_screen_height ( ) as f32 / 2.0 ) ,
3435 ball_velocity : Vector2 :: UP + Vector2 :: LEFT * 0.5 ,
35- ball_speed : 1000 .0,
36+ ball_speed : 3000 .0,
3637 ball_radius : 10.0 ,
3738 player_position : Vector2 :: new ( 10.0 , rl. get_screen_height ( ) as f32 / 2.0 - 50.0 ) ,
3839 player_size : Vector2 :: new ( 10.0 , 100.0 ) ,
39- player_speed : 1000 .0,
40+ player_speed : 2000 .0,
4041 enemy_ai : AI :: new ( rl. get_screen_width ( ) as f32 , rl. get_screen_height ( ) as f32 ) ,
4142 score : ( 0 , 0 ) ,
4243 screen_width : rl. get_screen_width ( ) ,
4344 screen_height : rl. get_screen_height ( ) ,
4445 time_since_last_score : 0.0 ,
4546 paused : true ,
4647 game_state : GameState :: Credits ,
48+ ball_position_history : [ Vector2 :: new ( rl. get_screen_width ( ) as f32 / 2.0 , rl. get_screen_height ( ) as f32 / 2.0 ) ; 10 ] ,
4749 }
4850 }
4951
@@ -75,12 +77,12 @@ impl Game {
7577
7678 let delta_time = unsafe { time:: DELTA_TIME } ;
7779
80+ self . update_player_movement ( rl, delta_time) ;
81+
7882 if self . paused {
7983 return ;
8084 }
8185
82- self . update_player_movement ( rl, delta_time) ;
83-
8486 self . enemy_ai . update ( ) ;
8587
8688 // update ball position and velocity
@@ -90,6 +92,11 @@ impl Game {
9092 }
9193
9294 fn update_player_movement ( & mut self , rl : & mut RaylibHandle , delta_time : f32 ) {
95+ let position = & mut self . player_position ;
96+ // add position to history and remove oldest entry
97+ self . ball_position_history . rotate_right ( 1 ) ;
98+ self . ball_position_history [ 0 ] = self . ball_position ;
99+
93100 // check W and S keys
94101 if rl. is_key_down ( KeyboardKey :: KEY_W ) {
95102 self . player_position . y -= self . player_speed * delta_time;
@@ -100,8 +107,19 @@ impl Game {
100107 self . player_position . y = self . player_position . y . min ( self . screen_height as f32 - self . player_size . y ) ;
101108 }
102109
110+ // check arrow up and down keys
111+ if rl. is_key_down ( KeyboardKey :: KEY_UP ) {
112+ self . player_position . y -= self . player_speed * delta_time;
113+ self . player_position . y = self . player_position . y . max ( 0.0 ) ;
114+ }
115+ else if rl. is_key_down ( KeyboardKey :: KEY_DOWN ) {
116+ self . player_position . y += self . player_speed * delta_time;
117+ self . player_position . y = self . player_position . y . min ( self . screen_height as f32 - self . player_size . y ) ;
118+ }
119+
103120 // check gamepad
104121 if rl. is_gamepad_available ( 0 ) {
122+ // check left stick y axis
105123 let y = rl. get_gamepad_axis_movement ( 0 , GamepadAxis :: GAMEPAD_AXIS_LEFT_Y ) ;
106124 if y < -0.5 {
107125 self . player_position . y -= self . player_speed * delta_time;
@@ -111,6 +129,16 @@ impl Game {
111129 self . player_position . y += self . player_speed * delta_time;
112130 self . player_position . y = self . player_position . y . min ( self . screen_height as f32 - self . player_size . y ) ;
113131 }
132+
133+ // check d-pad up and down
134+ if rl. is_gamepad_button_down ( 0 , GamepadButton :: GAMEPAD_BUTTON_LEFT_FACE_UP ) {
135+ self . player_position . y -= self . player_speed * delta_time;
136+ self . player_position . y = self . player_position . y . max ( 0.0 ) ;
137+ }
138+ else if rl. is_gamepad_button_down ( 0 , GamepadButton :: GAMEPAD_BUTTON_LEFT_FACE_DOWN ) {
139+ self . player_position . y += self . player_speed * delta_time;
140+ self . player_position . y = self . player_position . y . min ( self . screen_height as f32 - self . player_size . y ) ;
141+ }
114142 }
115143 }
116144
@@ -135,6 +163,11 @@ impl Game {
135163 let time_since_last_score = time - self . time_since_last_score ;
136164 let countdown = 3.0 - time_since_last_score;
137165 if countdown > 0.0 {
166+ d. draw_circle ( self . screen_width as i32 / 2 ,
167+ self . screen_height as i32 / 2 ,
168+ 100.0 ,
169+ Color :: WHITE ) ;
170+
138171 // draw text with format 3 2 1
139172 let text = format ! ( "{:.0}" , countdown) ;
140173 let text_width = measure_text ( & text, 100 ) as f32 ;
@@ -144,7 +177,7 @@ impl Game {
144177 text_position. x as i32 ,
145178 text_position. y as i32 ,
146179 100 ,
147- Color :: WHITE ) ;
180+ Color :: BLACK ) ;
148181 }
149182 }
150183
@@ -324,8 +357,8 @@ fn check_ball_collision(ball_position: Vector2,
324357fn reflect_ball ( v_in : Vector2 , ball_position : Vector2 , paddle_center : Vector2 ) -> Vector2 {
325358 let mut v_out = v_in;
326359 // bounce off paddle Y velocity is the difference between the ball position and the paddle center normalized
327- v_out . y = ( ball_position. y - paddle_center. y ) . normalize ( 0.0 , 1.0 ) ;
328-
360+ let diff = ( ball_position - paddle_center) . normalized ( ) ;
361+ v_out . y = diff . y * - 1.0 ;
329362 v_out. x *= -1.0 ;
330363 v_out
331364}
0 commit comments