feat: added paddles and input for the paddles. Clears everything on screen instead of individual (faster and easier)

This commit is contained in:
DarthMooMancer
2025-08-30 16:20:50 -04:00
parent c2b99a82df
commit fe8e4346a2
8 changed files with 113 additions and 29 deletions
+1
View File
@@ -8,6 +8,7 @@ set(SOURCES
src/main.cpp src/main.cpp
src/input.cpp src/input.cpp
src/window.cpp src/window.cpp
src/elements.cpp
) )
add_executable(pong ${SOURCES}) add_executable(pong ${SOURCES})
+27
View File
@@ -0,0 +1,27 @@
#ifndef ELEMENTS_HPP
#define ELEMENTS_HPP
#include "globals.hpp"
#include <array>
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<std::array<char, COL>, 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
+2 -1
View File
@@ -1,10 +1,11 @@
#ifndef INPUT_HPP #ifndef INPUT_HPP
#define INPUT_HPP #define INPUT_HPP
#include "elements.hpp"
struct Input { struct Input {
int m_current_key = 0; int m_current_key = 0;
char m_getch = ' '; char m_getch = ' ';
void get_input(bool &r); void get_input(bool &r, Paddle &a, Paddle &b);
}; };
#endif // INPUT_HPP #endif // INPUT_HPP
+4 -2
View File
@@ -3,10 +3,12 @@
#include <array> #include <array>
#include "globals.hpp" #include "globals.hpp"
#include "elements.hpp"
struct Window { struct Window {
Window(); // Window();
std::array<std::array<char, COL>, ROW> m_board; std::array<std::array<char, COL>, ROW> m_board;
void update_display(); // void update_display();
void update_display(Paddle &a, Paddle &b);
}; };
#endif #endif
+33
View File
@@ -0,0 +1,33 @@
#include <iostream>
#include "elements.hpp"
Point::Point() {
m_col = 0;
m_row = 0;
}
void Point::clear(std::array<std::array<char, COL>, 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
}
}
+24 -10
View File
@@ -1,25 +1,39 @@
#include "globals.hpp"
#include "input.hpp" #include "input.hpp"
#include <ncurses.h> #include <ncurses.h>
void Input::get_input(bool &running) { void Input::get_input(bool &running, Paddle &a, Paddle &b) {
while(running) { while(running) {
initscr(); initscr();
cbreak(); cbreak();
noecho(); noecho();
m_getch = getch(); m_getch = getch();
endwin(); endwin();
// if(m_current_key != 1 && (m_getch == 'a' || m_getch == 'D')) { if(m_getch == 'w') {
// direction = m_current_key = -1; if(a.m_nodes[0].m_row > 0) {
// } else if(m_current_key != - 1 && (m_getch == 'd' || m_getch == 'C')) { a.m_nodes[0].m_row--;
// direction = m_current_key = 1; m_getch = ' ';
// } else if(m_current_key != -2 && (m_getch == 's' || m_getch == 'B')) { }
// direction = m_current_key = 2; } else if(m_getch == 's') {
// } else if(m_current_key != 2 && (m_getch == 'w' || m_getch == 'A')) { if(a.m_nodes[3].m_row < ROW - 1) {
// direction = m_current_key = -2; 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') { if(m_getch == 'q') {
running = false; running = false;
} }
} }
} }
+10 -6
View File
@@ -2,15 +2,19 @@
#include <thread> #include <thread>
#include "input.hpp" #include "input.hpp"
#include "window.hpp" #include "window.hpp"
#include "elements.hpp"
bool running = true;
Input input;
Window window;
std::thread input_thread(&Input::get_input, &input, std::ref(running));
int main() { 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) { while (running) {
window.update_display(); window.update_display(left, right);
left.get_new_pos(1);
right.get_new_pos(18);
} }
input_thread.join(); input_thread.join();
+12 -10
View File
@@ -3,13 +3,16 @@
#include <chrono> #include <chrono>
#include <thread> #include <thread>
Window::Window() { // Window::Window() {
for(std::array<char, COL> &row : m_board) { row.fill('.'); } // for(std::array<char, COL> &row : m_board) { row.fill('.'); }
} // }
void Window::update_display() { void Window::update_display(Paddle &a, Paddle &b) {
// // Update symbols of positions // // 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'; // m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X';
// for(int i = 0; i < body.m_body.size(); i++) { // 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'; // 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 std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); // 1000 / fps; 200ms = 5fps
// // Clear old spaces // // Clear old spaces
// for(int i = 0; i < body.m_body.size(); i++) { // for(int i = 0; i < body.m_body.size(); i++) {
// body.m_body.at(i).m_tail_pos.clear(m_board); // body.m_body.at(i).m_tail_pos.clear(m_board);
// } // }
// head.m_head.clear(m_board); for(std::array<char, COL> &row : m_board) { row.fill('.'); }
// fruit.m_point.clear(m_board);
} }