added fps counter; fixed schtuff; ball has random direction; need to fix a lot
This commit is contained in:
parent
3749ff6217
commit
e8a1740ed9
55
Cargo.lock
generated
55
Cargo.lock
generated
|
@ -23,6 +23,17 @@ version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getrandom"
|
||||||
|
version = "0.2.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
"wasi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
|
@ -40,9 +51,47 @@ name = "ping"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cat-box",
|
"cat-box",
|
||||||
|
"rand",
|
||||||
|
"rand_core",
|
||||||
"sdl2",
|
"sdl2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ppv-lite86"
|
||||||
|
version = "0.2.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand"
|
||||||
|
version = "0.8.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"rand_chacha",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_chacha"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||||
|
dependencies = [
|
||||||
|
"ppv-lite86",
|
||||||
|
"rand_core",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rand_core"
|
||||||
|
version = "0.6.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||||
|
dependencies = [
|
||||||
|
"getrandom",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sdl2"
|
name = "sdl2"
|
||||||
version = "0.35.2"
|
version = "0.35.2"
|
||||||
|
@ -71,3 +120,9 @@ name = "version-compare"
|
||||||
version = "0.1.1"
|
version = "0.1.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29"
|
checksum = "579a42fc0b8e0c63b76519a339be31bed574929511fa53c1a3acae26eb258f29"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasi"
|
||||||
|
version = "0.11.0+wasi-snapshot-preview1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||||
|
|
|
@ -8,3 +8,5 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cat-box = "22.6.21"
|
cat-box = "22.6.21"
|
||||||
sdl2 = "0.35.2"
|
sdl2 = "0.35.2"
|
||||||
|
rand = "0.8.5"
|
||||||
|
rand_core = "0.6.4"
|
||||||
|
|
16
src/ball.rs
16
src/ball.rs
|
@ -1,17 +1,25 @@
|
||||||
use cat_box::Sprite;
|
use cat_box::Sprite;
|
||||||
|
use rand::{self};
|
||||||
|
|
||||||
pub struct Ball {
|
pub struct Ball {
|
||||||
pub sprite: Sprite,
|
pub sprite: Sprite,
|
||||||
pub vol_x: i32,
|
pub vel_x: i32,
|
||||||
pub vol_y: i32,
|
pub vel_y: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ball {
|
impl Ball {
|
||||||
pub fn new(x: i32, y: i32) -> Ball {
|
pub fn new(x: i32, y: i32) -> Ball {
|
||||||
|
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;}
|
||||||
|
|
||||||
Ball {
|
Ball {
|
||||||
sprite: Sprite::from_bytes(include_bytes!("../ping.png"), x, y).unwrap(),
|
sprite: Sprite::from_bytes(include_bytes!("../ping.png"), x, y).unwrap(),
|
||||||
vol_x: -1,
|
vel_x: x_a,
|
||||||
vol_y: 0,
|
vel_y: y_a,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
43
src/main.rs
43
src/main.rs
|
@ -27,13 +27,17 @@ fn main() {
|
||||||
let mut dir_enemy = Direction::Still;
|
let mut dir_enemy = Direction::Still;
|
||||||
|
|
||||||
let mut now = Instant::now();
|
let mut now = Instant::now();
|
||||||
|
|
||||||
|
// FPS COUNTER
|
||||||
|
let mut now2 = Instant::now();
|
||||||
|
let mut counter = 0;
|
||||||
game.run(|ctx| {
|
game.run(|ctx| {
|
||||||
ctx.set_background_colour(0, 0, 0);
|
ctx.set_background_colour(0, 0, 0);
|
||||||
|
|
||||||
let (x_player, y_player) = paddle.position().into();
|
let (x_player, y_player) = paddle.position().into();
|
||||||
let (x_enemy, y_enemy) = paddle_enemy.position().into();
|
let (x_enemy, y_enemy) = paddle_enemy.position().into();
|
||||||
|
|
||||||
let (_, y_ball) = ball.sprite.position().into();
|
let (x_ball, y_ball) = ball.sprite.position().into();
|
||||||
|
|
||||||
let keys = get_keyboard_state(ctx).keys;
|
let keys = get_keyboard_state(ctx).keys;
|
||||||
for key in &keys {
|
for key in &keys {
|
||||||
|
@ -60,34 +64,39 @@ fn main() {
|
||||||
dir_enemy = Direction::Up;
|
dir_enemy = Direction::Up;
|
||||||
}
|
}
|
||||||
|
|
||||||
if now.elapsed().as_millis() >= 90 {
|
if now.elapsed().as_millis() >= 110 {
|
||||||
if check_for_collision(&paddle, &ball.sprite) {
|
if check_for_collision(&paddle, &ball.sprite) {
|
||||||
if dir == Direction::Up {
|
if dir == Direction::Up {
|
||||||
ball.vol_y += 1;
|
ball.vel_y += 1;
|
||||||
}
|
}
|
||||||
if dir == Direction::Down {
|
if dir == Direction::Down {
|
||||||
ball.vol_y -= 1;
|
ball.vel_y -= 1;
|
||||||
}
|
}
|
||||||
ball.vol_x *= -1;
|
ball.vel_x *= -1;
|
||||||
ball.vol_y *= -1;
|
ball.vel_y *= -1;
|
||||||
}
|
}
|
||||||
if check_for_collision(&paddle_enemy, &ball.sprite) {
|
if check_for_collision(&paddle_enemy, &ball.sprite) {
|
||||||
if dir_enemy == Direction::Up {
|
if dir_enemy == Direction::Up {
|
||||||
ball.vol_y += 1;
|
ball.vel_y += 1;
|
||||||
}
|
}
|
||||||
if dir_enemy == Direction::Down {
|
if dir_enemy == Direction::Down {
|
||||||
ball.vol_y -= 1;
|
ball.vel_y -= 1;
|
||||||
}
|
}
|
||||||
ball.vol_x *= -1;
|
ball.vel_x *= -1;
|
||||||
ball.vol_y *= -1;
|
ball.vel_y *= -1;
|
||||||
}
|
}
|
||||||
if y_ball == 0 || y_ball == 500
|
if y_ball == 0 || y_ball == 500
|
||||||
{
|
{
|
||||||
|
|
||||||
ball.vol_y *= -1;
|
ball.vel_y *= -1;
|
||||||
|
}
|
||||||
|
if x_ball == 0 || x_ball == 500
|
||||||
|
{
|
||||||
|
ball.sprite.set_position((250,250));
|
||||||
|
ball.vel_x *= -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ball.sprite.translate((ball.vol_x, ball.vol_y));
|
ball.sprite.translate((ball.vel_x, ball.vel_y));
|
||||||
}
|
}
|
||||||
if now.elapsed().as_millis() >= 120 {
|
if now.elapsed().as_millis() >= 120 {
|
||||||
if (y_enemy - CORNER) < 0 {
|
if (y_enemy - CORNER) < 0 {
|
||||||
|
@ -129,11 +138,19 @@ fn main() {
|
||||||
}
|
}
|
||||||
now = Instant::now();
|
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();
|
ball.sprite.draw(ctx).unwrap();
|
||||||
paddle.draw(ctx).unwrap();
|
paddle.draw(ctx).unwrap();
|
||||||
paddle_enemy.draw(ctx).unwrap();
|
paddle_enemy.draw(ctx).unwrap();
|
||||||
|
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue