From 291f308d820e6ceb3e072b34353b698bf667803d Mon Sep 17 00:00:00 2001 From: gallant Date: Sun, 12 Feb 2023 16:47:07 -0600 Subject: [PATCH] working, just need to revise scoring and tick counting --- .gitignore | 1 + Cargo.lock | 73 +++++++++++++++++++++++++++ Cargo.toml | 10 ++++ paddle.png | Bin 0 -> 1386 bytes ping.png | Bin 0 -> 1815 bytes src/ball.rs | 17 +++++++ src/main.rs | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 240 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 paddle.png create mode 100644 ping.png create mode 100644 src/ball.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..0ed019a --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,73 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cat-box" +version = "22.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "718fced54b0fc65b4c154bad8cbd2826447a91a2c2d3980a94cd7e2beadcb73b" +dependencies = [ + "sdl2", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + +[[package]] +name = "ping" +version = "0.1.0" +dependencies = [ + "cat-box", + "sdl2", +] + +[[package]] +name = "sdl2" +version = "0.35.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7959277b623f1fb9e04aea73686c3ca52f01b2145f8ea16f4ff30d8b7623b1a" +dependencies = [ + "bitflags", + "lazy_static", + "libc", + "sdl2-sys", +] + +[[package]] +name = "sdl2-sys" +version = "0.35.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3586be2cf6c0a8099a79a12b4084357aa9b3e0b0d7980e3b67aaf7a9d55f9f0" +dependencies = [ + "cfg-if", + "libc", + "version-compare", +] + +[[package]] +name = "version-compare" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..86757c8 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ping" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +cat-box = "22.6.21" +sdl2 = "0.35.2" diff --git a/paddle.png b/paddle.png new file mode 100644 index 0000000000000000000000000000000000000000..0e475362b61034fbd42988916d57f7459844df01 GIT binary patch literal 1386 zcmbVMO>5LZ7>?FME%f41JcJZM!6cLHN6pZ<)J^R!m|EHeD|m2rGT9B?WMVSg&F)dW zc<>L1S3y1a2R!I+@Q?J;iy+iD*=!K0TAR;zW}bQ8&v{?%?rg8FTwjqSX|>yN_TZn( zH&i$Dh9_a^Ts@MLP%b>G^5{akaeqHzayK-G2?5o2ASOeWf-rtxH;89}8OnW9MFkaYY-YWuzcxdPZX1&`|DG0rY5vi^s{RQ0`HkjabMc zkpkAJun;KMt>9RR6`3lsdM3iCiR&OY zF+y{w3&r3OLCy!}Uf@sv3H2!Uc@h$cQxK8?)8lARXmefMjZ#4(%DRrNfT>y#c-W(? z-h?Vs4YNvG&WkUBICk^|U wccbfU_U^u>KTc0uOVQVt+fi>;+;5%+((;F;ZU4Q0E$@citsUpn{bw(J0z$&C=l}o! literal 0 HcmV?d00001 diff --git a/ping.png b/ping.png new file mode 100644 index 0000000000000000000000000000000000000000..ecc050db774dc3bf5e47b630d11f8c15b68792b7 GIT binary patch literal 1815 zcmcIl&x_2-$%n@&nrkj=^C7`{rSa*aCy1f|DaJ6eM=rMEWh7AQYXJ4f`YeHT^94QA~wrqN(Kr>@WE5p>Wtf~X1 zY`|5g^}Gw7GK0@~!OOM*tT|vos5l=U?GodLXnWzg2I^_kO za-%AzU9>nRmTPEbh=tbE84}31L=P>e+6qq)-Gtxcc!4}73^g^E&Ya` zJtO(RPb|lDP=;|L<+Ks3EEWnK=J~#Fh1?yAe=_+EW_-TnduiE>mQxNZsG&W7Ial-S zre6HdmdZaEw_4Y(P$9bSS`HEb%s|MAOh*pOZuk9H=( zm+Y^{kGHS?_}PQ`x8Hqz*Zt~~*MGbB>aWT-53j#*>!U|hZe8QOgV7J~eEj*}3+guL literal 0 HcmV?d00001 diff --git a/src/ball.rs b/src/ball.rs new file mode 100644 index 0000000..7665740 --- /dev/null +++ b/src/ball.rs @@ -0,0 +1,17 @@ +use cat_box::Sprite; + +pub struct Ball { + pub sprite: Sprite, + pub vol_x: i32, + pub vol_y: i32, +} + +impl Ball { + pub fn new(x: i32, y: i32) -> Ball { + Ball { + sprite: Sprite::from_bytes(include_bytes!("../ping.png"), x, y).unwrap(), + vol_x: -1, + vol_y: 0, + } + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..edd55b2 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,139 @@ +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 (_, 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() >= 90 { + if check_for_collision(&paddle, &ball.sprite) { + if dir == Direction::Up { + ball.vol_y += 1; + } + if dir == Direction::Down { + ball.vol_y -= 1; + } + ball.vol_x *= -1; + ball.vol_y *= -1; + } + if check_for_collision(&paddle_enemy, &ball.sprite) { + if dir_enemy == Direction::Up { + ball.vol_y += 1; + } + if dir_enemy == Direction::Down { + ball.vol_y -= 1; + } + ball.vol_x *= -1; + ball.vol_y *= -1; + } + if y_ball == 0 || y_ball == 500 + { + + ball.vol_y *= -1; + } + + ball.sprite.translate((ball.vol_x, ball.vol_y)); + } + if now.elapsed().as_millis() >= 120 { + 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, 25)); + } + Direction::Down => { + paddle_enemy.translate((0, -25)); + } + _ => (), + }; + } + if now.elapsed().as_millis() >= 120 { + 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, 25)); + } + Direction::Down => { + paddle.translate((0, -25)); + } + _ => (), + }; + } + now = Instant::now(); + } + + + ball.sprite.draw(ctx).unwrap(); + paddle.draw(ctx).unwrap(); + paddle_enemy.draw(ctx).unwrap(); + }) + .unwrap(); +}