8 Commits
Author SHA1 Message Date
nicknase27 7fc808b262 fix workflow file
CI/CD / clippy (push) Successful in 22s
CI/CD / test (push) Successful in 22s
CI/CD / Release (push) Successful in 44s
2026-09-30 12:08:48 +02:00
nicknase27 232377eae1 add restart keybind
CI/CD / clippy (push) Failing after 23s
CI/CD / test (push) Failing after 17s
CI/CD / Release (push) Skipped
2026-09-30 12:03:59 +02:00
nicknase27 57a486f459 Fix title position 2026-09-30 12:01:01 +02:00
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
4 changed files with 75 additions and 78 deletions

No files matched your search

-1
View File
@@ -7,7 +7,6 @@ on:
env:
CARGO_TERM_COLOR: always
jobs:
clippy:
name: clippy
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"] }
+72 -34
View File
@@ -1,20 +1,26 @@
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use futures::{FutureExt, StreamExt};
use rand::random_range;
use ratatui::layout::Alignment;
use ratatui::layout::Constraint::Percentage;
use ratatui::layout::HorizontalAlignment::Center;
use ratatui::style::Stylize;
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::{
DefaultTerminal, Frame,
layout::{Constraint, Direction, Layout},
widgets::Block,
};
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]
async fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
@@ -56,8 +62,8 @@ pub struct Ball {
impl Default for Ball {
fn default() -> Self {
Ball {
x: 25.0,
y: 7.0,
x: X_BOUNDS.1 - 1.5,
y: PADDLE_Y + 2.0,
x_velocity: 0.5,
y_velocity: 0.5,
}
@@ -70,11 +76,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 - 1.5;
self.y = paddle.y + 2.0;
self.x_velocity = -0.5;
self.y_velocity = 0.5;
}
}
}
@@ -95,8 +111,8 @@ impl App {
App {
running: true,
event_stream: EventStream::new(),
player: Paddle::new(0.0, 5.0),
enemy: Paddle::new(49.0, 5.0),
player: Paddle::new(PLAYER_X, PADDLE_Y),
enemy: Paddle::new(ENEMY_X, PADDLE_Y),
ball: Ball::default(),
state: Gamestate::Playing,
player_score: 0,
@@ -109,8 +125,8 @@ impl App {
self.enemy_score = 0;
self.state = Gamestate::Playing;
self.player = Paddle::new(0.0, 5.0);
self.enemy = Paddle::new(49.0, 5.0);
self.player = Paddle::new(PLAYER_X, PADDLE_Y);
self.enemy = Paddle::new(ENEMY_X, PADDLE_Y);
self.ball = Ball::default();
}
@@ -138,11 +154,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;
}
}
@@ -150,19 +166,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;
}
@@ -217,12 +233,16 @@ impl App {
match self.state {
Gamestate::Playing => {
let canvas = Canvas::default()
.block(Block::bordered().title(format!(
"PongRS | {} - {}",
self.player_score, self.enemy_score
)))
.x_bounds([0.0, 50.0])
.y_bounds([0.0, 15.0])
.block(
Block::bordered()
.title(format!(
" PongRS | {} - {} ",
self.player_score, self.enemy_score
))
.title_alignment(Alignment::Center),
)
.x_bounds([X_BOUNDS.0, X_BOUNDS.1])
.y_bounds([Y_BOUNDS.0, Y_BOUNDS.1])
.marker(Marker::Block)
.paint(|ctx| {
ctx.draw(&Points {
@@ -245,7 +265,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(net, window[0]);
}
Gamestate::Paused => {
frame.render_widget(Clear, centered_area);
@@ -291,29 +326,32 @@ 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,
Gamestate::Won => self.restart(),
Gamestate::Lost => self.restart(),
},
(_, KeyCode::Char('r')) => self.restart(),
_ => {}
}
}
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;
}
}