feat: added paddle, point and ball (collision and movement)

This commit is contained in:
DarthMooMancer
2025-09-01 03:07:46 -04:00
parent 6cb55e0e1a
commit 0fc5a08ab2
9 changed files with 132 additions and 23 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ set(SOURCES
src/main.cpp
src/input.cpp
src/window.cpp
# src/elements.cpp
src/elements.cpp
)
add_executable(breakout ${SOURCES})
+33
View File
@@ -0,0 +1,33 @@
#ifndef ELEMENTS_HPP
#define ELEMENTS_HPP
struct Point {
Point();
int m_row;
int m_col;
void assign(int row, int col);
void to_string();
};
struct Paddle {
Paddle();
int _size;
Point m_nodes[3];
void get_new_pos();
};
struct Ball {
Ball();
Point m_origin;
int vx;
int vy;
void get_new_pos();
void check_collision();
void change_velocity(char d);
};
// struct Block {
//
// };
//
#endif
+1 -1
View File
@@ -2,6 +2,6 @@
#define GLOBALS_HPP
constexpr int ROW = 10;
constexpr int COL = 10;
constexpr int COL = 15;
#endif
+2 -2
View File
@@ -1,10 +1,10 @@
#ifndef INPUT_HPP
#define INPUT_HPP
// #include "elements.hpp"
#include "elements.hpp"
struct Input {
char m_getch = ' ';
void get_input(bool &running);
void get_input(bool &running, Paddle &paddle);
};
#endif // INPUT_HPP
+2 -2
View File
@@ -3,10 +3,10 @@
#include <array>
#include "globals.hpp"
// #include "elements.hpp"
#include "elements.hpp"
struct Window {
std::array<std::array<char, COL>, ROW> m_board = {};
void update_display();
void update_display(Paddle &paddle, Ball &ball);
};
#endif
+68
View File
@@ -0,0 +1,68 @@
#include "elements.hpp"
#include "globals.hpp"
#include <iostream>
Point::Point() {
m_row = 0;
m_col = 0;
}
void Point::assign(int row, int col) {
m_row = row;
m_col = col;
}
void Point::to_string() {
std::cout << "(" << m_row << ", " << m_col << ")";
}
Paddle::Paddle() {
_size = 3;
m_nodes[0].m_col = 6;
for(int i = 0; i < _size; i++) {
m_nodes[i].m_row = 8;
}
}
void Paddle::get_new_pos() {
for(int i = 1; i < _size; i++) {
m_nodes[i].m_col = m_nodes[0].m_col + i;
}
}
Ball::Ball() {
m_origin.assign(ROW - 3, (COL / 2));
vx = 1;
vy = -1;
}
void Ball::change_velocity(char d) {
if(d == 'x') {
if(vx == -1) vx = 1;
else if (vx == 1) vx = -1;
} else if(d == 'y') {
if(vy == -1) vy = 1;
else if (vy == 1) vy = -1;
}
}
void Ball::check_collision() {
if(m_origin.m_row <= 0 || m_origin.m_row >= ROW -1) {
change_velocity('y');
} if(m_origin.m_col <= 0 || m_origin.m_col >= COL -1) {
change_velocity('x');
}
}
void Ball::get_new_pos() {
if(vx == -1) {
m_origin.m_col--;
} else if(vx == 1) {
m_origin.m_col++;
} if(vy == -1) {
m_origin.m_row--;
} else if(vy == 1) {
m_origin.m_row++;
}
}
+11 -7
View File
@@ -1,17 +1,21 @@
#include "input.hpp"
#include <ncurses.h>
#include "input.hpp"
#include "globals.hpp"
void Input::get_input(bool &running) {
void Input::get_input(bool &running, Paddle &paddle) {
while(running) {
initscr();
cbreak();
noecho();
m_getch = getch();
endwin();
if(m_getch == 'q') {
running = false;
}
if(m_getch == 'a' && paddle.m_nodes[0].m_col > 0) {
paddle.m_nodes[0].m_col--;
m_getch = ' ';
} else if(m_getch == 'd' && paddle.m_nodes[paddle._size - 1].m_col < COL - 1) {
paddle.m_nodes[0].m_col++;
m_getch = ' ';
} if(m_getch == 'q') running = false;
}
}
+8 -5
View File
@@ -1,19 +1,22 @@
#include <iostream>
#include <thread>
#include "input.hpp"
#include "window.hpp"
#include "elements.hpp"
int main() {
Input input;
Window window;
Paddle paddle;
Ball ball;
bool running = true;
std::thread input_thread(&Input::get_input, &input, std::ref(running));
std::thread input_thread(&Input::get_input, &input, std::ref(running), std::ref(paddle));
while (running) {
std::cout << "Hello World" << std::endl;
window.update_display();
window.update_display(paddle, ball);
paddle.get_new_pos();
ball.check_collision();
ball.get_new_pos();
}
input_thread.join();
return 0;
+6 -5
View File
@@ -3,11 +3,12 @@
#include <chrono>
#include <thread>
void Window::update_display() {
void Window::update_display(Paddle &paddle, Ball &ball) {
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
// Modify board
// Refresh and draw screen
for(int i = 0; i < paddle._size; i++) {
m_board[paddle.m_nodes[i].m_row][paddle.m_nodes[i].m_col] = '-';
}
m_board[ball.m_origin.m_row][ball.m_origin.m_col] = '*';
std::cout << "\033[H" << std::flush;
for(std::array<char, COL> &row : m_board) {
for(char &col : row) {
@@ -15,5 +16,5 @@ void Window::update_display() {
}
std::cout << "\r\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds(130)); // 1000 / fps; 200ms = 5fps
std::this_thread::sleep_for(std::chrono::milliseconds(300)); // 1000 / fps; 200ms = 5fps
}