feat: added paddles and input for the paddles. Clears everything on screen instead of individual (faster and easier)
This commit is contained in:
@@ -8,6 +8,7 @@ set(SOURCES
|
||||
src/main.cpp
|
||||
src/input.cpp
|
||||
src/window.cpp
|
||||
src/elements.cpp
|
||||
)
|
||||
|
||||
add_executable(pong ${SOURCES})
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
+4
-2
@@ -3,10 +3,12 @@
|
||||
|
||||
#include <array>
|
||||
#include "globals.hpp"
|
||||
#include "elements.hpp"
|
||||
|
||||
struct Window {
|
||||
Window();
|
||||
// Window();
|
||||
std::array<std::array<char, COL>, ROW> m_board;
|
||||
void update_display();
|
||||
// void update_display();
|
||||
void update_display(Paddle &a, Paddle &b);
|
||||
};
|
||||
#endif
|
||||
|
||||
@@ -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
@@ -1,25 +1,39 @@
|
||||
#include "globals.hpp"
|
||||
#include "input.hpp"
|
||||
#include <ncurses.h>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+8
-4
@@ -2,15 +2,19 @@
|
||||
#include <thread>
|
||||
#include "input.hpp"
|
||||
#include "window.hpp"
|
||||
#include "elements.hpp"
|
||||
|
||||
int main() {
|
||||
bool running = true;
|
||||
Input input;
|
||||
Window window;
|
||||
std::thread input_thread(&Input::get_input, &input, std::ref(running));
|
||||
|
||||
int main() {
|
||||
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();
|
||||
|
||||
+9
-7
@@ -3,13 +3,16 @@
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
Window::Window() {
|
||||
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
|
||||
}
|
||||
// Window::Window() {
|
||||
// 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
|
||||
// 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';
|
||||
@@ -28,6 +31,5 @@ void Window::update_display() {
|
||||
// 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(std::array<char, COL> &row : m_board) { row.fill('.'); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user