49 lines
949 B
C++
49 lines
949 B
C++
#ifndef ELEMENT_HPP
|
|
#define ELEMENT_HPP
|
|
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <vector>
|
|
#include "globals.hpp"
|
|
|
|
class Window;
|
|
|
|
enum class Direction {
|
|
UP, DOWN, LEFT, RIGHT
|
|
};
|
|
|
|
struct vec {
|
|
int x, y;
|
|
vec() : x(0), y(0) {}
|
|
vec(int x, int y) : x(x), y(y) {}
|
|
void set(int _x, int _y) { x = _x; y = _y; }
|
|
auto operator <=> (const vec&) const = default;
|
|
};
|
|
|
|
class SnakeGame {
|
|
public:
|
|
SnakeGame();
|
|
~SnakeGame();
|
|
void run();
|
|
|
|
private:
|
|
std::unique_ptr<Window> win;
|
|
std::vector<vec> buf;
|
|
vec head { COL / 2, ROW / 2 };
|
|
vec fruit { rand() % COL, rand() % ROW };
|
|
vec prev_head { head.x, head.y };
|
|
struct {
|
|
bool running = true;
|
|
Direction m_dir = Direction::RIGHT;
|
|
uint8_t m_score = 0;
|
|
} state;
|
|
void process_input();
|
|
void determine_index_zero_pos();
|
|
void move_tail_indicies();
|
|
void add_tail_and_respawn_fruit(std::array<std::array<char, COL>, ROW> board);
|
|
bool segment_overlap();
|
|
bool exit_summary();
|
|
};
|
|
|
|
#endif // ELEMENT_HPP
|