bug fixes, more constants
This commit is contained in:
3 files changed
+40
-70
No files matched your search
+37
-27
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user