5 Commits
Author SHA1 Message Date
nicknase27 d14b6c750c improve paddle hitbox
CI/CD / clippy (push) Successful in 22s
CI/CD / test (push) Successful in 21s
CI/CD / Release (push) Successful in 50s
2026-09-29 14:08:50 +02:00
nicknase27 7f7e0bb523 bug fixes, more constants 2026-09-29 13:37:25 +02:00
nicknase27 f798b63175 add constant bounds 2026-09-29 12:26:43 +02:00
nicknase27 c58247f84b added a center line 2026-09-29 12:12:20 +02:00
nicknase27 e9609e3cc4 use constants 2026-09-27 12:51:17 +02:00
3 changed files with 65 additions and 73 deletions

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" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f079e83a288787bcd14a6aea84cee5c87a67c5a3e660c30f557a3d24761b3527" 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]] [[package]]
name = "color-eyre" name = "color-eyre"
version = "0.6.5" version = "0.6.5"
@@ -234,15 +223,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "cpufeatures"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "critical-section" name = "critical-section"
version = "1.2.0" version = "1.2.0"
@@ -590,7 +570,6 @@ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
"r-efi 6.0.0", "r-efi 6.0.0",
"rand_core 0.10.1",
] ]
[[package]] [[package]]
@@ -1044,7 +1023,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d" checksum = "3c80231409c20246a13fddb31776fb942c38553c51e871f8cbd687a4cfb5843d"
dependencies = [ dependencies = [
"phf_shared", "phf_shared",
"rand 0.8.8", "rand",
] ]
[[package]] [[package]]
@@ -1082,7 +1061,6 @@ dependencies = [
"color-eyre", "color-eyre",
"crossterm", "crossterm",
"futures", "futures",
"rand 0.10.3",
"ratatui", "ratatui",
"tokio", "tokio",
] ]
@@ -1135,18 +1113,7 @@ version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c" checksum = "e058c7de0b26af77780c769414d6257830bb240f3c38477dbc2c16e5f54d6d4c"
dependencies = [ dependencies = [
"rand_core 0.6.4", "rand_core",
]
[[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",
] ]
[[package]] [[package]]
@@ -1155,12 +1122,6 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
[[package]]
name = "rand_core"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69"
[[package]] [[package]]
name = "ratatui" name = "ratatui"
version = "0.30.2" version = "0.30.2"
@@ -1389,7 +1350,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"cpufeatures 0.2.17", "cpufeatures",
"digest", "digest",
] ]
-1
View File
@@ -10,7 +10,6 @@ edition = "2024"
color-eyre = "0.6.5" color-eyre = "0.6.5"
crossterm = { version = "0.29.0", features = ["event-stream"] } crossterm = { version = "0.29.0", features = ["event-stream"] }
futures = "0.3.32" futures = "0.3.32"
rand = "0.10.3"
ratatui = "0.30.2" ratatui = "0.30.2"
tokio = { version = "1.52.3", features = ["full"] } tokio = { version = "1.52.3", features = ["full"] }
+62 -30
View File
@@ -1,20 +1,25 @@
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use futures::{FutureExt, StreamExt}; use futures::{FutureExt, StreamExt};
use rand::random_range;
use ratatui::layout::Constraint::Percentage; use ratatui::layout::Constraint::Percentage;
use ratatui::layout::HorizontalAlignment::Center; use ratatui::layout::HorizontalAlignment::Center;
use ratatui::style::Stylize; use ratatui::style::Stylize;
use ratatui::symbols::Marker; use ratatui::symbols::Marker;
use ratatui::widgets::canvas::{Canvas, Points, Rectangle}; use ratatui::widgets::canvas::{Canvas, Line, Points, Rectangle};
use ratatui::widgets::{Clear, Paragraph}; use ratatui::widgets::{Clear, Paragraph};
use ratatui::{ use ratatui::{
DefaultTerminal, Frame, DefaultTerminal, Frame,
layout::{Constraint, Direction, Layout}, layout::{Constraint, Direction, Layout},
widgets::Block, widgets::Block,
}; };
use tokio::time::{Duration, interval}; use tokio::time::{Duration, interval};
// Constants
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] #[tokio::main]
async fn main() -> color_eyre::Result<()> { async fn main() -> color_eyre::Result<()> {
color_eyre::install()?; color_eyre::install()?;
@@ -56,8 +61,8 @@ pub struct Ball {
impl Default for Ball { impl Default for Ball {
fn default() -> Self { fn default() -> Self {
Ball { Ball {
x: 25.0, x: X_BOUNDS.1 - 1.5,
y: 7.0, y: PADDLE_Y + 2.0,
x_velocity: 0.5, x_velocity: 0.5,
y_velocity: 0.5, y_velocity: 0.5,
} }
@@ -70,11 +75,21 @@ impl Ball {
self.y += self.y_velocity; self.y += self.y_velocity;
} }
pub fn reset(&mut self) { pub fn reset(&mut self, paddle: &Paddle, toward_enemy: bool) {
self.x = random_range(25.0..=40.0); // Player scored = true
self.y = 7.0; // Enemy scored = false
self.x_velocity = -0.5;
self.y_velocity = 0.5; 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 - 1.5;
self.y = paddle.y + 2.0;
self.x_velocity = -0.5;
self.y_velocity = 0.5;
}
} }
} }
@@ -95,8 +110,8 @@ impl App {
App { App {
running: true, running: true,
event_stream: EventStream::new(), event_stream: EventStream::new(),
player: Paddle::new(0.0, 5.0), player: Paddle::new(PLAYER_X, PADDLE_Y),
enemy: Paddle::new(49.0, 5.0), enemy: Paddle::new(ENEMY_X, PADDLE_Y),
ball: Ball::default(), ball: Ball::default(),
state: Gamestate::Playing, state: Gamestate::Playing,
player_score: 0, player_score: 0,
@@ -109,8 +124,8 @@ impl App {
self.enemy_score = 0; self.enemy_score = 0;
self.state = Gamestate::Playing; self.state = Gamestate::Playing;
self.player = Paddle::new(0.0, 5.0); self.player = Paddle::new(PLAYER_X, PADDLE_Y);
self.enemy = Paddle::new(49.0, 5.0); self.enemy = Paddle::new(ENEMY_X, PADDLE_Y);
self.ball = Ball::default(); self.ball = Ball::default();
} }
@@ -138,11 +153,11 @@ impl App {
let enemy_center = self.enemy.y + 2.0; let enemy_center = self.enemy.y + 2.0;
if self.ball.y > enemy_center { 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; self.enemy.y += 0.25;
} }
} else if self.ball.y < enemy_center { } 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; self.enemy.y -= 0.25;
} }
} }
@@ -150,19 +165,19 @@ impl App {
pub fn check_collisions(&mut self) { pub fn check_collisions(&mut self) {
// Top/bottom walls // 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; self.ball.y_velocity *= -1.0;
} }
if self.ball.x <= 0.0 { if self.ball.x >= X_BOUNDS.1 {
self.enemy_score += 1; self.player_score += 1;
self.ball.reset(); self.ball.reset(&self.player, true);
return; return;
} }
if self.ball.x >= 50.0 { if self.ball.x <= X_BOUNDS.0 {
self.player_score += 1; self.enemy_score += 1;
self.ball.reset(); self.ball.reset(&self.enemy, false);
return; return;
} }
@@ -221,8 +236,8 @@ impl App {
"PongRS | {} - {}", "PongRS | {} - {}",
self.player_score, self.enemy_score self.player_score, self.enemy_score
))) )))
.x_bounds([0.0, 50.0]) .x_bounds([X_BOUNDS.0, X_BOUNDS.1])
.y_bounds([0.0, 15.0]) .y_bounds([Y_BOUNDS.0, Y_BOUNDS.1])
.marker(Marker::Block) .marker(Marker::Block)
.paint(|ctx| { .paint(|ctx| {
ctx.draw(&Points { ctx.draw(&Points {
@@ -245,7 +260,22 @@ impl App {
}); });
}); });
let net = Canvas::default()
.x_bounds([X_BOUNDS.0, X_BOUNDS.1])
.y_bounds([Y_BOUNDS.0, Y_BOUNDS.1])
.marker(Marker::Dot)
.paint(|ctx| {
ctx.draw(&Line {
x1: X_BOUNDS.1 / 2.0,
y1: Y_BOUNDS.1 - 0.5,
x2: X_BOUNDS.1 / 2.0,
y2: Y_BOUNDS.0 + 0.5,
color: ratatui::style::Color::White,
});
});
frame.render_widget(canvas, window[0]); frame.render_widget(canvas, window[0]);
frame.render_widget(net, window[0]);
} }
Gamestate::Paused => { Gamestate::Paused => {
frame.render_widget(Clear, centered_area); frame.render_widget(Clear, centered_area);
@@ -291,8 +321,8 @@ impl App {
fn on_key_event(&mut self, key: KeyEvent) { fn on_key_event(&mut self, key: KeyEvent) {
match (key.modifiers, key.code) { match (key.modifiers, key.code) {
(_, KeyCode::Char('q')) | (KeyModifiers::CONTROL, KeyCode::Char('c')) => self.quit(), (_, KeyCode::Char('q')) | (KeyModifiers::CONTROL, KeyCode::Char('c')) => self.quit(),
(_, KeyCode::Up | KeyCode::Char('k')) => self.move_up(), (_, KeyCode::Up | KeyCode::Char('k') | KeyCode::Char('w')) => self.move_up(),
(_, KeyCode::Down | KeyCode::Char('j')) => self.move_down(), (_, KeyCode::Down | KeyCode::Char('j') | KeyCode::Char('s')) => self.move_down(),
(_, KeyCode::Esc) => match self.state { (_, KeyCode::Esc) => match self.state {
Gamestate::Playing => self.state = Gamestate::Paused, Gamestate::Playing => self.state = Gamestate::Paused,
Gamestate::Paused => self.state = Gamestate::Playing, Gamestate::Paused => self.state = Gamestate::Playing,
@@ -305,15 +335,17 @@ impl App {
pub fn move_up(&mut self) { pub fn move_up(&mut self) {
let new_position = self.player.y + 1.0; 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; self.player.y = new_position;
} }
} }
pub fn move_down(&mut self) { pub fn move_down(&mut self) {
let new_position = self.player.y - 1.0; let new_position = self.player.y - 1.0;
if new_position >= 0.0 { if new_position > Y_BOUNDS.0 {
self.player.y -= 1.0; self.player.y = new_position;
} else {
self.player.y = Y_BOUNDS.0;
} }
} }