bug fixes, more constants

This commit is contained in:
nicknase27 committed 2026-09-29 13:37:25 +02:00
1 parent f798b63175
commit 7f7e0bb523
3 files changed
+40 -70

No files matched your search

Generated
+3 -42
View File
@@ -164,17 +164,6 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527"
[[package]]
name = "chacha20"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06"
dependencies = [
"cfg-if",
"cpufeatures 0.3.1",
"rand_core 0.10.1",
]
[[package]]
name = "color-eyre"
version = "0.6.5"
@@ -234,15 +223,6 @@ dependencies = [
"libc",
]
[[package]]
name = "cpufeatures"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566"
dependencies = [
"libc",
]
[[package]]
name = "critical-section"
version = "1.2.0"
@@ -590,7 +570,6 @@ dependencies = [
"cfg-if",
"libc",
"r-efi 6.0.0",
"rand_core 0.10.1",
]
[[package]]
@@ -1044,7 +1023,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d"
dependencies = [
"phf_shared",
"rand 0.8.8",
"rand",
]
[[package]]
@@ -1082,7 +1061,6 @@ dependencies = [
"color-eyre",
"crossterm",
"futures",
"rand 0.10.3",
"ratatui",
"tokio",
]
@@ -1135,18 +1113,7 @@ version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c"
dependencies = [
"rand_core 0.6.4",
]
[[package]]
name = "rand"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65c9fb96cbc91e3478eaae79a69fcd3f1ae4ad052e471fe6732fff548984b4af"
dependencies = [
"chacha20",
"getrandom 0.4.3",
"rand_core 0.10.1",
"rand_core",
]
[[package]]
@@ -1155,12 +1122,6 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
[[package]]
name = "rand_core"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
[[package]]
name = "ratatui"
version = "0.30.2"
@@ -1389,7 +1350,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
dependencies = [
"cfg-if",
"cpufeatures 0.2.17",
"cpufeatures",
"digest",
]
-1
View File
@@ -10,7 +10,6 @@ edition = "2024"
color-eyre = "0.6.5"
crossterm = { version = "0.29.0", features = ["event-stream"] }
futures = "0.3.32"
rand = "0.10.3"
ratatui = "0.30.2"
tokio = { version = "1.52.3", features = ["full"] }
+37 -27
View File
@@ -1,7 +1,5 @@
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use futures::{FutureExt, StreamExt};
use tokio::time::{Duration, interval};
use rand::random_range;
use ratatui::layout::Constraint::Percentage;
use ratatui::layout::HorizontalAlignment::Center;
use ratatui::style::Stylize;
@@ -13,14 +11,14 @@ use ratatui::{
layout::{Constraint, Direction, Layout},
widgets::Block,
};
use tokio::time::{Duration, interval};
// Constants
const PLAYER_X: f64 = 0.0;
const ENEMY_X: f64 = 49.0;
const PADDLE_Y: f64 = 5.0;
const X_BOUNDS: (f64, f64) = (0.0, 50.0);
const Y_BOUNDS: (f64, f64) = (0.0, 15.0);
const PLAYER_X: f64 = X_BOUNDS.0;
const ENEMY_X: f64 = X_BOUNDS.1 - 1.0;
const PADDLE_Y: f64 = Y_BOUNDS.1 / 2.0;
#[tokio::main]
async fn main() -> color_eyre::Result<()> {
@@ -63,8 +61,8 @@ pub struct Ball {
impl Default for Ball {
fn default() -> Self {
Ball {
x: 25.0,
y: 7.0,
x: X_BOUNDS.0 + 2.0,
y: PADDLE_Y + 2.0,
x_velocity: 0.5,
y_velocity: 0.5,
}
@@ -77,11 +75,21 @@ impl Ball {
self.y += self.y_velocity;
}
pub fn reset(&mut self) {
self.x = random_range(25.0..=40.0);
self.y = 7.0;
self.x_velocity = -0.5;
self.y_velocity = 0.5;
pub fn reset(&mut self, paddle: &Paddle, toward_enemy: bool) {
// Player scored = true
// Enemy scored = false
if toward_enemy == true {
self.x = paddle.x + 2.0;
self.y = paddle.y + 2.0;
self.x_velocity = 0.5;
self.y_velocity = 0.5;
} else {
self.x = paddle.x - 2.0;
self.y = paddle.y + 2.0;
self.x_velocity = -0.5;
self.y_velocity = 0.5;
}
}
}
@@ -145,11 +153,11 @@ impl App {
let enemy_center = self.enemy.y + 2.0;
if self.ball.y > enemy_center {
if self.enemy.y + 4.0 < 15.0 {
if self.enemy.y + 4.0 < Y_BOUNDS.1 {
self.enemy.y += 0.25;
}
} else if self.ball.y < enemy_center {
if self.enemy.y > 0.0 {
if self.enemy.y > Y_BOUNDS.0 {
self.enemy.y -= 0.25;
}
}
@@ -157,19 +165,19 @@ impl App {
pub fn check_collisions(&mut self) {
// Top/bottom walls
if self.ball.y <= 0.0 || self.ball.y >= 15.0 {
if self.ball.y <= Y_BOUNDS.0 || self.ball.y >= Y_BOUNDS.1 {
self.ball.y_velocity *= -1.0;
}
if self.ball.x <= 0.0 {
self.enemy_score += 1;
self.ball.reset();
if self.ball.x >= X_BOUNDS.1 {
self.player_score += 1;
self.ball.reset(&self.player, true);
return;
}
if self.ball.x >= 50.0 {
self.player_score += 1;
self.ball.reset();
if self.ball.x <= X_BOUNDS.0 {
self.enemy_score += 1;
self.ball.reset(&self.enemy, false);
return;
}
@@ -313,8 +321,8 @@ impl App {
fn on_key_event(&mut self, key: KeyEvent) {
match (key.modifiers, key.code) {
(_, KeyCode::Char('q')) | (KeyModifiers::CONTROL, KeyCode::Char('c')) => self.quit(),
(_, KeyCode::Up | KeyCode::Char('k')) => self.move_up(),
(_, KeyCode::Down | KeyCode::Char('j')) => self.move_down(),
(_, KeyCode::Up | KeyCode::Char('k') | KeyCode::Char('w')) => self.move_up(),
(_, KeyCode::Down | KeyCode::Char('j') | KeyCode::Char('s')) => self.move_down(),
(_, KeyCode::Esc) => match self.state {
Gamestate::Playing => self.state = Gamestate::Paused,
Gamestate::Paused => self.state = Gamestate::Playing,
@@ -327,15 +335,17 @@ impl App {
pub fn move_up(&mut self) {
let new_position = self.player.y + 1.0;
if new_position + 3.0 < 15.0 {
if new_position + 3.0 < Y_BOUNDS.1 {
self.player.y = new_position;
}
}
pub fn move_down(&mut self) {
let new_position = self.player.y - 1.0;
if new_position >= 0.0 {
self.player.y -= 1.0;
if new_position > Y_BOUNDS.0 {
self.player.y = new_position;
} else {
self.player.y = Y_BOUNDS.0;
}
}