init
This commit is contained in:
commit
f5b31ad2a6
7 files changed
+2491
No files matched your search
+326
@@ -0,0 +1,326 @@
|
||||
use crossterm::event::{Event, EventStream, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
||||
use futures::{FutureExt, StreamExt};
|
||||
use rand::random_range;
|
||||
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::{Clear, Paragraph};
|
||||
use ratatui::{
|
||||
DefaultTerminal, Frame,
|
||||
layout::{Constraint, Direction, Layout},
|
||||
widgets::Block,
|
||||
};
|
||||
|
||||
use tokio::time::{Duration, interval};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> color_eyre::Result<()> {
|
||||
color_eyre::install()?;
|
||||
let terminal = ratatui::init();
|
||||
let result = App::new().run(terminal).await;
|
||||
ratatui::restore();
|
||||
result
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
enum Gamestate {
|
||||
#[default]
|
||||
Playing,
|
||||
Paused,
|
||||
Won,
|
||||
Lost,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Player {
|
||||
x: f64,
|
||||
y: f64,
|
||||
}
|
||||
|
||||
impl Default for Player {
|
||||
fn default() -> Self {
|
||||
Player { x: 0.0, y: 5.0 }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Enemy {
|
||||
x: f64,
|
||||
y: f64,
|
||||
}
|
||||
|
||||
impl Default for Enemy {
|
||||
fn default() -> Self {
|
||||
Enemy { x: 49.0, y: 5.0 }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Ball {
|
||||
x: f64,
|
||||
y: f64,
|
||||
x_velocity: f64,
|
||||
y_velocity: f64,
|
||||
}
|
||||
|
||||
impl Default for Ball {
|
||||
fn default() -> Self {
|
||||
Ball {
|
||||
x: 25.0,
|
||||
y: 7.0,
|
||||
x_velocity: 0.5,
|
||||
y_velocity: 0.5,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Ball {
|
||||
pub fn update(&mut self) {
|
||||
self.x += self.x_velocity;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct App {
|
||||
running: bool,
|
||||
paused: bool,
|
||||
event_stream: EventStream,
|
||||
player: Player,
|
||||
player_score: i32,
|
||||
enemy_score: i32,
|
||||
enemy: Enemy,
|
||||
ball: Ball,
|
||||
state: Gamestate,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn restart(&mut self) {
|
||||
self.player_score = 0;
|
||||
self.enemy_score = 0;
|
||||
self.state = Gamestate::Playing;
|
||||
|
||||
self.player = Player::default();
|
||||
self.enemy = Enemy::default();
|
||||
self.ball = Ball::default();
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
if self.paused != true {
|
||||
self.ball.update();
|
||||
self.update_enemy();
|
||||
self.check_collisions();
|
||||
self.check_state();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_state(&mut self) {
|
||||
if self.player_score >= 5 {
|
||||
self.state = Gamestate::Won;
|
||||
} else if self.enemy_score >= 5 {
|
||||
self.state = Gamestate::Lost;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_enemy(&mut self) {
|
||||
let enemy_center = self.enemy.y + 2.0;
|
||||
|
||||
if self.ball.y > enemy_center {
|
||||
if self.enemy.y + 4.0 < 15.0 {
|
||||
self.enemy.y += 0.25;
|
||||
}
|
||||
} else if self.ball.y < enemy_center {
|
||||
if self.enemy.y > 0.0 {
|
||||
self.enemy.y -= 0.25;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_collisions(&mut self) {
|
||||
// Top/bottom walls
|
||||
if self.ball.y <= 0.0 || self.ball.y >= 15.0 {
|
||||
self.ball.y_velocity *= -1.0;
|
||||
}
|
||||
|
||||
if self.ball.x <= 0.0 {
|
||||
self.enemy_score += 1;
|
||||
self.ball.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
if self.ball.x >= 50.0 {
|
||||
self.player_score += 1;
|
||||
self.ball.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
// Player paddle
|
||||
if self.ball.x <= self.player.x + 1.0
|
||||
&& self.ball.y >= self.player.y
|
||||
&& self.ball.y <= self.player.y + 4.0
|
||||
&& self.ball.x_velocity < 0.0
|
||||
{
|
||||
self.ball.x_velocity *= -1.0;
|
||||
}
|
||||
|
||||
// Enemy paddle
|
||||
if self.ball.x >= self.enemy.x - 1.0
|
||||
&& self.ball.y >= self.enemy.y
|
||||
&& self.ball.y <= self.enemy.y + 4.0
|
||||
&& self.ball.x_velocity > 0.0
|
||||
{
|
||||
self.ball.x_velocity *= -1.0;
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run(mut self, mut terminal: DefaultTerminal) -> color_eyre::Result<()> {
|
||||
self.running = true;
|
||||
|
||||
let mut interval = interval(Duration::from_millis(50));
|
||||
|
||||
while self.running {
|
||||
terminal.draw(|frame| self.draw(frame))?;
|
||||
|
||||
tokio::select! {
|
||||
_ = interval.tick() => {
|
||||
self.update();
|
||||
}
|
||||
|
||||
_ = self.handle_crossterm_events() => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn draw(&mut self, frame: &mut Frame) {
|
||||
let window = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(100)])
|
||||
.split(frame.area());
|
||||
|
||||
let popup_block = Block::new();
|
||||
let centered_area = window[0].centered(Percentage(60), Percentage(20));
|
||||
|
||||
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])
|
||||
.marker(Marker::Block)
|
||||
.paint(|ctx| {
|
||||
ctx.draw(&Points {
|
||||
coords: &[(self.ball.x, self.ball.y)],
|
||||
color: ratatui::style::Color::Yellow,
|
||||
});
|
||||
ctx.draw(&Rectangle {
|
||||
x: self.player.x,
|
||||
y: self.player.y,
|
||||
width: 1.0,
|
||||
height: 4.0,
|
||||
color: ratatui::style::Color::Green,
|
||||
});
|
||||
ctx.draw(&Rectangle {
|
||||
x: self.enemy.x,
|
||||
y: self.enemy.y,
|
||||
width: 1.0,
|
||||
height: 4.0,
|
||||
color: ratatui::style::Color::Red,
|
||||
});
|
||||
});
|
||||
|
||||
frame.render_widget(canvas, window[0]);
|
||||
}
|
||||
Gamestate::Paused => {
|
||||
frame.render_widget(Clear, centered_area);
|
||||
let pause_message = Paragraph::new(format!(
|
||||
"Paused\nScore: {} - {}",
|
||||
self.player_score, self.enemy_score
|
||||
))
|
||||
.block(popup_block.clone())
|
||||
.alignment(Center);
|
||||
frame.render_widget(pause_message, centered_area);
|
||||
}
|
||||
Gamestate::Won => {
|
||||
frame.render_widget(Clear, centered_area);
|
||||
let win_message = Paragraph::new("You win!".green())
|
||||
.block(popup_block)
|
||||
.alignment(Center);
|
||||
frame.render_widget(win_message, centered_area);
|
||||
}
|
||||
Gamestate::Lost => {
|
||||
frame.render_widget(Clear, centered_area);
|
||||
let lose_message = Paragraph::new("You lose!".red())
|
||||
.block(popup_block)
|
||||
.alignment(Center);
|
||||
frame.render_widget(lose_message, centered_area);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_crossterm_events(&mut self) -> color_eyre::Result<()> {
|
||||
let event = self.event_stream.next().fuse().await;
|
||||
match event {
|
||||
Some(Ok(evt)) => match evt {
|
||||
Event::Key(key) if key.kind == KeyEventKind::Press => self.on_key_event(key),
|
||||
Event::Mouse(_) => {}
|
||||
Event::Resize(_, _) => {}
|
||||
_ => {}
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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::Esc) => match self.state {
|
||||
Gamestate::Playing => self.state = Gamestate::Paused,
|
||||
Gamestate::Paused => self.state = Gamestate::Playing,
|
||||
Gamestate::Won => self.restart(),
|
||||
Gamestate::Lost => self.restart(),
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_up(&mut self) {
|
||||
let new_position = self.player.y + 1.0;
|
||||
if new_position + 3.0 < 15.0 {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
fn quit(&mut self) {
|
||||
self.running = false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user