This commit is contained in:
DarthMooMancer
2025-11-24 14:23:37 -05:00
parent 4a2c46af9e
commit 820c4b6dc4
2 changed files with 7 additions and 10 deletions
+3 -2
View File
@@ -3,6 +3,7 @@
#include <string>
#include <deque>
#include <array>
#include <ncurses.h>
constexpr int ROW = 15;
constexpr int COL = 21;
@@ -21,13 +22,13 @@ struct vec2 {
struct Window {
std::array<std::array<char, COL>, ROW> view;
void draw_display();
void update_display(vec2 &head, vec2 &fruit, std::deque<vec2> &buf);
void update_display(const vec2 fruit, std::deque<vec2> &buf);
};
class Engine {
public:
Engine();
~Engine();
~Engine() { endwin(); }
void run();
private:
+4 -8
View File
@@ -3,7 +3,6 @@
#include <cstdlib>
#include <ctime>
#include <format>
#include <ncurses.h>
#include <ranges>
#include <thread>
#include <chrono>
@@ -14,13 +13,10 @@ void Window::draw_display() {
mvaddch(i, j * 2, view[i][j]);
}
}
refresh();
}
void Window::update_display(vec2 &head, vec2 &fruit, std::deque<vec2> &buf) {
view[head.y][head.x] = '*';
void Window::update_display(const vec2 fruit, std::deque<vec2> &buf) {
view[fruit.y][fruit.x] = 'o';
for(const auto& segment : buf) {
if(segment.x < COL && segment.y < ROW) {
view[segment.y][segment.x] = '~';
@@ -38,16 +34,15 @@ Engine::Engine() {
for(auto& row : win.view) row.fill('.');
}
Engine::~Engine() { endwin(); }
void Engine::run() {
while (exit_summary() && process_input()) {
add_tail_and_respawn_fruit();
determine_index_zero_pos();
move_tail_indicies();
win.update_display(head, fruit, tail);
win.update_display(fruit, tail);
win.draw_display();
refresh();
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200)));
}
nodelay(stdscr, false);
@@ -86,6 +81,7 @@ void Engine::determine_index_zero_pos() {
else if(dir == Direction::UP) { head.y = (head.y + ROW - 1) % ROW; }
else if(dir == Direction::DOWN) { head.y = (head.y + 1) % ROW; }
win.view[prev_head.y][prev_head.x] = '.';
win.view[head.y][head.x] = '*';
}
void Engine::move_tail_indicies() {