working, just need to revise scoring and tick counting

This commit is contained in:
gallant 2023-02-12 16:47:07 -06:00
commit 291f308d82
7 changed files with 240 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

73
Cargo.lock generated Normal file
View file

@ -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"

10
Cargo.toml Normal file
View file

@ -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"

BIN
paddle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
ping.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

17
src/ball.rs Normal file
View file

@ -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,
}
}
}

139
src/main.rs Normal file
View file

@ -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();
}