This commit is contained in:
DarthMooMancer
2025-11-14 10:20:00 -05:00
parent 59c68b7bd3
commit 8d4b3990d1
2 changed files with 13 additions and 12 deletions
+5 -7
View File
@@ -1,8 +1,8 @@
#ifndef ENGINE_HPP #pragma once
#define ENGINE_HPP
#include <string> #include <string>
#include <vector> #include <deque>
#include <array>
constexpr int ROW = 11; constexpr int ROW = 11;
constexpr int COL = 11; constexpr int COL = 11;
@@ -21,7 +21,7 @@ struct vec2 {
struct Window { struct Window {
std::array<std::array<char, COL>, ROW> view; std::array<std::array<char, COL>, ROW> view;
void draw_display(); void draw_display();
void update_display(vec2 &head, vec2 &fruit, std::vector<vec2> &buf); void update_display(vec2 &head, vec2 &fruit, std::deque<vec2> &buf);
}; };
class Engine { class Engine {
@@ -32,7 +32,7 @@ public:
private: private:
Window win; Window win;
std::vector<vec2> tail; std::deque<vec2> tail;
std::string status; std::string status;
vec2 head { COL / 2, ROW / 2 }; vec2 head { COL / 2, ROW / 2 };
vec2 fruit { rand() % COL, rand() % ROW }; vec2 fruit { rand() % COL, rand() % ROW };
@@ -45,5 +45,3 @@ private:
void add_tail_and_respawn_fruit(); void add_tail_and_respawn_fruit();
bool exit_summary(); bool exit_summary();
}; };
#endif // ELEMENT_HPP
+8 -5
View File
@@ -17,8 +17,7 @@ void Window::draw_display() {
refresh(); refresh();
} }
void Window::update_display(vec2 &head, vec2 &fruit, std::vector<vec2> &buf) { void Window::update_display(vec2 &head, vec2 &fruit, std::deque<vec2> &buf) {
for(auto& row : view) row.fill('.');
view[head.y][head.x] = '*'; view[head.y][head.x] = '*';
view[fruit.y][fruit.x] = 'o'; view[fruit.y][fruit.x] = 'o';
@@ -36,6 +35,7 @@ Engine::Engine() {
noecho(); noecho();
curs_set(0); curs_set(0);
nodelay(stdscr, true); nodelay(stdscr, true);
for(auto& row : win.view) row.fill('.');
} }
Engine::~Engine() { endwin(); } Engine::~Engine() { endwin(); }
@@ -59,7 +59,7 @@ void Engine::run() {
bool Engine::process_input() { bool Engine::process_input() {
int ch; int ch;
while((ch = getch()) != ERR) { while((ch = getch()) != ERR) {
if(ch == 'q') { return false; } if(ch == 'q') { return status = "Quitting", false; }
else if(ch == 'w') { if(dir != Direction::DOWN) dir = Direction::UP; } else if(ch == 'w') { if(dir != Direction::DOWN) dir = Direction::UP; }
else if(ch == 's') { if(dir != Direction::UP) dir = Direction::DOWN; } else if(ch == 's') { if(dir != Direction::UP) dir = Direction::DOWN; }
else if(ch == 'd') { if(dir != Direction::LEFT) dir = Direction::RIGHT; } else if(ch == 'd') { if(dir != Direction::LEFT) dir = Direction::RIGHT; }
@@ -85,12 +85,15 @@ void Engine::determine_index_zero_pos() {
else if(dir == Direction::RIGHT) { head.x = (head.x + 1) % COL; } else if(dir == Direction::RIGHT) { head.x = (head.x + 1) % COL; }
else if(dir == Direction::UP) { head.y = (head.y + ROW - 1) % ROW; } else if(dir == Direction::UP) { head.y = (head.y + ROW - 1) % ROW; }
else if(dir == Direction::DOWN) { head.y = (head.y + 1) % ROW; } else if(dir == Direction::DOWN) { head.y = (head.y + 1) % ROW; }
win.view[prev_head.y][prev_head.x] = '.';
} }
void Engine::move_tail_indicies() { void Engine::move_tail_indicies() {
if(tail.empty()) return; if(tail.empty()) return;
vec2 last_tail = tail.back();
tail.pop_back(); tail.pop_back();
tail.insert(tail.begin(), prev_head); tail.push_front(prev_head);
if(last_tail.x < COL && last_tail.y < ROW) { win.view[last_tail.y][last_tail.x] = '.'; }
} }
void Engine::add_tail_and_respawn_fruit() { void Engine::add_tail_and_respawn_fruit() {
@@ -99,6 +102,6 @@ void Engine::add_tail_and_respawn_fruit() {
do { new_pos.set((rand() % ROW), (rand() % COL)); } do { new_pos.set((rand() % ROW), (rand() % COL)); }
while (new_pos == head || std::any_of(tail.begin(), tail.end(), [&](const vec2& seg) { return seg == new_pos; })); while (new_pos == head || std::any_of(tail.begin(), tail.end(), [&](const vec2& seg) { return seg == new_pos; }));
fruit = new_pos; fruit = new_pos;
tail.insert(tail.begin(), vec2 { ROW, COL }); tail.push_front(vec2 { ROW, COL });
m_score++; m_score++;
} }