diff --git a/CMakeLists.txt b/CMakeLists.txt index 946a378..6b3f461 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/include/elements.hpp b/include/elements.hpp new file mode 100644 index 0000000..4d4a4c2 --- /dev/null +++ b/include/elements.hpp @@ -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 diff --git a/include/globals.hpp b/include/globals.hpp index c203563..3e66cd9 100644 --- a/include/globals.hpp +++ b/include/globals.hpp @@ -2,6 +2,6 @@ #define GLOBALS_HPP constexpr int ROW = 10; -constexpr int COL = 10; +constexpr int COL = 15; #endif diff --git a/include/input.hpp b/include/input.hpp index 70892f4..17ad117 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -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 diff --git a/include/window.hpp b/include/window.hpp index c5a22c8..0ced018 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -3,10 +3,10 @@ #include #include "globals.hpp" -// #include "elements.hpp" +#include "elements.hpp" struct Window { std::array, ROW> m_board = {}; - void update_display(); + void update_display(Paddle &paddle, Ball &ball); }; #endif diff --git a/src/elements.cpp b/src/elements.cpp new file mode 100644 index 0000000..8bc983d --- /dev/null +++ b/src/elements.cpp @@ -0,0 +1,68 @@ +#include "elements.hpp" +#include "globals.hpp" +#include + +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++; + } +} diff --git a/src/input.cpp b/src/input.cpp index 63361b1..e2de8f0 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,17 +1,21 @@ -#include "input.hpp" #include +#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; } } diff --git a/src/main.cpp b/src/main.cpp index f0027d6..4404bb5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,22 @@ -#include #include #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; diff --git a/src/window.cpp b/src/window.cpp index b0325d9..622d8d7 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -3,11 +3,12 @@ #include #include -void Window::update_display() { +void Window::update_display(Paddle &paddle, Ball &ball) { for(std::array &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 &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 }