From 94711d37ca2283c16df4caf42ce9f86ea64af056 Mon Sep 17 00:00:00 2001 From: gallant Date: Sun, 12 Feb 2023 21:52:04 -0600 Subject: [PATCH] the game ever --- src/.main.rs.rustfmt | 148 +++++++++++++++++++++++++++++++++++++++++++ src/ball.rs | 32 +++++++++- src/main.rs | 52 ++++----------- 3 files changed, 191 insertions(+), 41 deletions(-) create mode 100644 src/.main.rs.rustfmt diff --git a/src/.main.rs.rustfmt b/src/.main.rs.rustfmt new file mode 100644 index 0000000..c7457cf --- /dev/null +++ b/src/.main.rs.rustfmt @@ -0,0 +1,148 @@ +use cat_box::{get_keyboard_state, physics::check_for_collision, Game, Sprite}; +use sdl2::keyboard::Scancode; +use std::time::Instant; + +mod ball; + +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +enum Direction { + Up, + Down, + Still, +} +const CORNER: i32 = 37 / 2; + +fn main() { + let game = Game::new("pingPong", 500, 500); + + let mut ball = ball::Ball::new(250, 250); + + let paddle_bytes = include_bytes!("../paddle.png"); + let mut paddle = Sprite::from_bytes(paddle_bytes, 37, 250 - CORNER).unwrap(); + let mut paddle_enemy = Sprite::from_bytes(paddle_bytes, 500 - 37, 250 - CORNER).unwrap(); + + let mut held = true; + + let mut dir = Direction::Still; + let mut dir_enemy = Direction::Still; + + let mut now = Instant::now(); + + game.run(|ctx| { + ctx.set_background_colour(0, 0, 0); + + let (x_player, y_player) = paddle.position().into(); + let (x_enemy, y_enemy) = paddle_enemy.position().into(); + + let (x_ball, y_ball) = ball.sprite.position().into(); + + let keys = get_keyboard_state(ctx).keys; + for key in &keys { + match key { + Scancode::W | Scancode::Up => { + dir = Direction::Up; + held = true; + } + Scancode::S | Scancode::Down => { + dir = Direction::Down; + held = true; + } + _ => { + dir = Direction::Still; + held = false; + } + }; + } + + if y_enemy < y_ball { + dir_enemy = Direction::Down; + } + if y_enemy > y_ball { + dir_enemy = Direction::Up; + } + + if now.elapsed().as_millis() >= 10 { + //BALL + if check_for_collision(&paddle, &ball.sprite) { + if dir == Direction::Up { + ball.vel_y += 1; + } + if dir == Direction::Down { + ball.vel_y += 1; + } + ball.vel_x *= -1; + ball.vel_y *= -1; + } + if check_for_collision(&paddle_enemy, &ball.sprite) { + if dir_enemy == Direction::Up { + ball.vel_y += 1; + } + if dir_enemy == Direction::Down { + ball.vel_y -= 1; + } + ball.vel_x *= -1; + ball.vel_y *= -1; + } + if y_ball < 0 || y_ball > 500 + { + + ball.vel_y *= -1; + } + if x_ball < 0 || x_ball > 500 + { + ball.sprite.set_position((250,250)); + ball::Ball::rand_angle(&mut ball); + + } + + ball.sprite.translate((ball.vel_x, ball.vel_y)); + + //ENEMY LOGIC + if (y_enemy - CORNER) < 0 { + dir_enemy = Direction::Still; + paddle_enemy.set_position((x_enemy, 1 + CORNER)); + } else if (y_player + CORNER) > 500 { + dir_enemy = Direction::Still; + paddle_enemy.set_position((x_enemy, 500 - CORNER)); + } + + match dir_enemy { + Direction::Up => { + paddle_enemy.translate((0, 2)); + } + Direction::Down => { + paddle_enemy.translate((0, -2)); + } + _ => (), + }; + + //PLAYEW LOGIC + if (y_player - CORNER) < 0 { + dir = Direction::Still; + paddle.set_position((x_player, 1 + CORNER)); + } else if (y_player + CORNER) > 500 { + dir = Direction::Still; + paddle.set_position((x_player, 500 - CORNER)); + } + if held { + match dir { + Direction::Up => { + paddle.translate((0, 2)); + } + Direction::Down => { + paddle.translate((0, -2)); + } + _ => (), + }; + } + now = Instant::now(); + } + + + ball.sprite.draw(ctx).unwrap(); + paddle.draw(ctx).unwrap(); + paddle_enemy.draw(ctx).unwrap(); + + }) + .unwrap(); +} diff --git a/src/ball.rs b/src/ball.rs index 2115b6e..ced1799 100644 --- a/src/ball.rs +++ b/src/ball.rs @@ -13,8 +13,16 @@ impl Ball { let ybool = rand::random(); let x_a: i32; let y_a: i32; - if xbool { x_a = 1;} else { x_a = -1;} - if ybool { y_a = 1;} else { y_a = -1;} + if xbool { + x_a = 1; + } else { + x_a = -1; + } + if ybool { + y_a = 1; + } else { + y_a = -1; + } Ball { sprite: Sprite::from_bytes(include_bytes!("../ping.png"), x, y).unwrap(), @@ -22,4 +30,24 @@ impl Ball { vel_y: y_a, } } + + pub fn rand_angle(&mut self) { + let xbool = rand::random(); + let ybool = rand::random(); + let x_a: i32; + let y_a: i32; + if xbool { + x_a = 1; + } else { + x_a = -1; + } + if ybool { + y_a = 1; + } else { + y_a = -1; + } + + self.vel_x = x_a; + self.vel_y = y_a; + } } diff --git a/src/main.rs b/src/main.rs index 5d181ec..83a56b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,10 +27,7 @@ fn main() { let mut dir_enemy = Direction::Still; let mut now = Instant::now(); - - // FPS COUNTER - let mut now2 = Instant::now(); - let mut counter = 0; + game.run(|ctx| { ctx.set_background_colour(0, 0, 0); @@ -65,42 +62,28 @@ fn main() { } if now.elapsed().as_millis() >= 10 { + //BALL if check_for_collision(&paddle, &ball.sprite) { - if dir == Direction::Up { - ball.vel_y += 1; - } - if dir == Direction::Down { - ball.vel_y -= 1; - } + ball.vel_y += (ball.vel_y * 1); ball.vel_x *= -1; ball.vel_y *= -1; } if check_for_collision(&paddle_enemy, &ball.sprite) { - if dir_enemy == Direction::Up { - ball.vel_y += 1; - } - if dir_enemy == Direction::Down { - ball.vel_y -= 1; - } + ball.vel_y += (ball.vel_y * 1); ball.vel_x *= -1; ball.vel_y *= -1; } - if y_ball < 0 || y_ball > 500 - { - + if y_ball < 0 || y_ball > 500 { ball.vel_y *= -1; } - if x_ball < 0 || x_ball > 500 - { - ball.sprite.set_position((250,250)); - ball.vel_x = ball.vel_x * -1 - 1; - ball.vel_y = ball.vel_y * -1 - 1; - + if x_ball < 0 || x_ball > 500 { + ball.sprite.set_position((250, 250)); + ball::Ball::rand_angle(&mut ball); } ball.sprite.translate((ball.vel_x, ball.vel_y)); - } - if now.elapsed().as_millis() >= 10 { + + //ENEMY LOGIC if (y_enemy - CORNER) < 0 { dir_enemy = Direction::Still; paddle_enemy.set_position((x_enemy, 1 + CORNER)); @@ -118,8 +101,8 @@ fn main() { } _ => (), }; - } - if now.elapsed().as_millis() >= 10 { + + //PLAYEW LOGIC if (y_player - CORNER) < 0 { dir = Direction::Still; paddle.set_position((x_player, 1 + CORNER)); @@ -137,22 +120,13 @@ fn main() { } _ => (), }; - } + } now = Instant::now(); } - // FPS COUNTER STUFF - if now2.elapsed().as_millis() >= 1000 - { - println!("{counter}"); - counter = 0; - now2 = Instant::now(); - } - counter += 1; ball.sprite.draw(ctx).unwrap(); paddle.draw(ctx).unwrap(); paddle_enemy.draw(ctx).unwrap(); - }) .unwrap(); }