From fe8e4346a265b3d46f5d23ae8ef49323db71bd9f Mon Sep 17 00:00:00 2001 From: DarthMooMancer Date: Sat, 30 Aug 2025 16:20:50 -0400 Subject: [PATCH] feat: added paddles and input for the paddles. Clears everything on screen instead of individual (faster and easier) --- CMakeLists.txt | 1 + include/elements.hpp | 27 +++++++++++++++++++++++++++ include/input.hpp | 3 ++- include/window.hpp | 6 ++++-- src/elements.cpp | 33 +++++++++++++++++++++++++++++++++ src/input.cpp | 34 ++++++++++++++++++++++++---------- src/main.cpp | 16 ++++++++++------ src/window.cpp | 22 ++++++++++++---------- 8 files changed, 113 insertions(+), 29 deletions(-) create mode 100644 include/elements.hpp create mode 100644 src/elements.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9620c6b..351889e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ set(SOURCES src/main.cpp src/input.cpp src/window.cpp + src/elements.cpp ) add_executable(pong ${SOURCES}) diff --git a/include/elements.hpp b/include/elements.hpp new file mode 100644 index 0000000..bb3d39c --- /dev/null +++ b/include/elements.hpp @@ -0,0 +1,27 @@ +#ifndef ELEMENTS_HPP +#define ELEMENTS_HPP +#include "globals.hpp" +#include + +struct Point { + int m_row; + int m_col; + Point(); + Point(int row, int col) : m_row(row), m_col(col) {}; + void to_string(); + void to_string(int index); + void clear(std::array, ROW> &m_board); + // void set_new_point(int row, int col); +}; + +struct Paddle { + Paddle(int col); + Point m_nodes[4]; + void get_new_pos(int col); +}; + +struct Ball { + +}; + +#endif diff --git a/include/input.hpp b/include/input.hpp index 6184d02..5b35a7a 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -1,10 +1,11 @@ #ifndef INPUT_HPP #define INPUT_HPP +#include "elements.hpp" struct Input { int m_current_key = 0; char m_getch = ' '; - void get_input(bool &r); + void get_input(bool &r, Paddle &a, Paddle &b); }; #endif // INPUT_HPP diff --git a/include/window.hpp b/include/window.hpp index 4124807..c871afa 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -3,10 +3,12 @@ #include #include "globals.hpp" +#include "elements.hpp" struct Window { - Window(); + // Window(); std::array, ROW> m_board; - void update_display(); + // void update_display(); + void update_display(Paddle &a, Paddle &b); }; #endif diff --git a/src/elements.cpp b/src/elements.cpp new file mode 100644 index 0000000..5daf342 --- /dev/null +++ b/src/elements.cpp @@ -0,0 +1,33 @@ +#include +#include "elements.hpp" + +Point::Point() { + m_col = 0; + m_row = 0; +} + +void Point::clear(std::array, ROW> &m_board) { + m_board[m_row][m_col] = '.'; +} + +void Point::to_string() { + std::cout << "(" << m_row << ", " << m_col << ")"; +} + +void Point::to_string(int index) { + std::cout << "Index " << index << ": (" << m_row << ", " << m_col << ")"; +} + +Paddle::Paddle(int col) { + for(int i = 0; i < 4; i++) { + m_nodes[i].m_col = col; + m_nodes[i].m_row = i + 3; + } +} + +void Paddle::get_new_pos(int col) { + for(int i = 1; i < 4; i++) { + m_nodes[i].m_row = m_nodes[0].m_row + i; + m_nodes[i].m_col = col; // Should fix the weird bug that happens when spamming keys + } +} diff --git a/src/input.cpp b/src/input.cpp index b36d190..fc5a387 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,25 +1,39 @@ +#include "globals.hpp" #include "input.hpp" #include -void Input::get_input(bool &running) { +void Input::get_input(bool &running, Paddle &a, Paddle &b) { while(running) { initscr(); cbreak(); noecho(); m_getch = getch(); + endwin(); - // if(m_current_key != 1 && (m_getch == 'a' || m_getch == 'D')) { - // direction = m_current_key = -1; - // } else if(m_current_key != - 1 && (m_getch == 'd' || m_getch == 'C')) { - // direction = m_current_key = 1; - // } else if(m_current_key != -2 && (m_getch == 's' || m_getch == 'B')) { - // direction = m_current_key = 2; - // } else if(m_current_key != 2 && (m_getch == 'w' || m_getch == 'A')) { - // direction = m_current_key = -2; + if(m_getch == 'w') { + if(a.m_nodes[0].m_row > 0) { + a.m_nodes[0].m_row--; + m_getch = ' '; + } + } else if(m_getch == 's') { + if(a.m_nodes[3].m_row < ROW - 1) { + a.m_nodes[0].m_row++; + m_getch = ' '; + } + } if(m_getch == 'A') { + if(b.m_nodes[0].m_row > 0) { + b.m_nodes[0].m_row--; + m_getch = ' '; + } + } else if(m_getch == 'B') { + if(b.m_nodes[3].m_row < ROW - 1) { + b.m_nodes[0].m_row++; + m_getch = ' '; + } + } if(m_getch == 'q') { running = false; } } - } diff --git a/src/main.cpp b/src/main.cpp index a3bfd33..1abe108 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,15 +2,19 @@ #include #include "input.hpp" #include "window.hpp" - -bool running = true; -Input input; -Window window; -std::thread input_thread(&Input::get_input, &input, std::ref(running)); +#include "elements.hpp" int main() { + bool running = true; + Input input; + Window window; + Paddle left(1); + Paddle right(18); + std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(left), std::ref(right)); while (running) { - window.update_display(); + window.update_display(left, right); + left.get_new_pos(1); + right.get_new_pos(18); } input_thread.join(); diff --git a/src/window.cpp b/src/window.cpp index 0c1ea2e..6cd3a2c 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3,13 +3,16 @@ #include #include -Window::Window() { - for(std::array &row : m_board) { row.fill('.'); } -} +// Window::Window() { +// for(std::array &row : m_board) { row.fill('.'); } +// } -void Window::update_display() { +void Window::update_display(Paddle &a, Paddle &b) { // // Update symbols of positions -// m_board[head.m_head.m_row][head.m_head.m_col] = '*'; + for(int i = 0; i < 4; i++) { + m_board[a.m_nodes[i].m_row][a.m_nodes[i].m_col] = '*'; + m_board[b.m_nodes[i].m_row][b.m_nodes[i].m_col] = '*'; + } // m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X'; // for(int i = 0; i < body.m_body.size(); i++) { // m_board[body.m_body.at(i).m_tail_pos.m_row][body.m_body.at(i).m_tail_pos.m_col] = '0'; @@ -25,9 +28,8 @@ void Window::update_display() { } std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); // 1000 / fps; 200ms = 5fps // // Clear old spaces -// for(int i = 0; i < body.m_body.size(); i++) { -// body.m_body.at(i).m_tail_pos.clear(m_board); -// } -// head.m_head.clear(m_board); -// fruit.m_point.clear(m_board); + // for(int i = 0; i < body.m_body.size(); i++) { + // body.m_body.at(i).m_tail_pos.clear(m_board); + // } + for(std::array &row : m_board) { row.fill('.'); } }