wip
This commit is contained in:
+12
-14
@@ -4,7 +4,7 @@
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "globals.hpp"
|
||||
#include <globals.hpp>
|
||||
|
||||
class Window;
|
||||
|
||||
@@ -12,12 +12,12 @@ enum class Direction {
|
||||
UP, DOWN, LEFT, RIGHT
|
||||
};
|
||||
|
||||
struct vec {
|
||||
struct vec2 {
|
||||
int x, y;
|
||||
vec() : x(0), y(0) {}
|
||||
vec(int x, int y) : x(x), y(y) {}
|
||||
vec2() : x(0), y(0) {}
|
||||
vec2(int x, int y) : x(x), y(y) {}
|
||||
void set(int _x, int _y) { x = _x; y = _y; }
|
||||
auto operator <=> (const vec&) const = default;
|
||||
auto operator <=> (const vec2&) const = default;
|
||||
};
|
||||
|
||||
class SnakeGame {
|
||||
@@ -28,20 +28,18 @@ public:
|
||||
|
||||
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 };
|
||||
std::vector<vec2> buf;
|
||||
vec2 head { COL / 2, ROW / 2 };
|
||||
vec2 fruit { rand() % COL, rand() % ROW };
|
||||
vec2 prev_head { head.x, head.y };
|
||||
struct {
|
||||
bool running = true;
|
||||
Direction m_dir = Direction::RIGHT;
|
||||
Direction dir = Direction::RIGHT;
|
||||
uint8_t m_score = 0;
|
||||
} state;
|
||||
void process_input();
|
||||
bool 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();
|
||||
void add_tail_and_respawn_fruit(std::array<std::array<char, COL>, ROW> view);
|
||||
bool exit_summary();
|
||||
};
|
||||
|
||||
|
||||
+4
-4
@@ -1,14 +1,14 @@
|
||||
#ifndef WINDOW_HPP
|
||||
#define WINDOW_HPP
|
||||
|
||||
#include "globals.hpp"
|
||||
#include "element.hpp"
|
||||
#include <globals.hpp>
|
||||
#include <element.hpp>
|
||||
#include <array>
|
||||
|
||||
struct Window {
|
||||
std::array<std::array<char, COL>, ROW> board;
|
||||
std::array<std::array<char, COL>, ROW> view;
|
||||
void draw_display();
|
||||
void update_display(vec &head, vec &fruit, std::vector<vec> &buf);
|
||||
void update_display(vec2 &head, vec2 &fruit, std::vector<vec2> &buf);
|
||||
};
|
||||
|
||||
#endif // WINDOW_HPP
|
||||
|
||||
+18
-26
@@ -1,8 +1,6 @@
|
||||
#include "element.hpp"
|
||||
#include "globals.hpp"
|
||||
#include <element.hpp>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <memory>
|
||||
#include <ncurses.h>
|
||||
#include <ranges>
|
||||
#include <iostream>
|
||||
@@ -25,9 +23,8 @@ SnakeGame::~SnakeGame() {
|
||||
}
|
||||
|
||||
void SnakeGame::run() {
|
||||
while (exit_summary()) {
|
||||
process_input();
|
||||
add_tail_and_respawn_fruit(win->board);
|
||||
while (exit_summary() && process_input()) {
|
||||
add_tail_and_respawn_fruit(win->view);
|
||||
determine_index_zero_pos();
|
||||
move_tail_indicies();
|
||||
|
||||
@@ -37,35 +34,38 @@ void SnakeGame::run() {
|
||||
}
|
||||
}
|
||||
|
||||
void SnakeGame::process_input() {
|
||||
bool SnakeGame::process_input() {
|
||||
int ch;
|
||||
while((ch = getch()) != ERR) {
|
||||
switch(ch) {
|
||||
case 'w': if(state.m_dir != Direction::DOWN) state.m_dir = Direction::UP; break;
|
||||
case 's': if(state.m_dir != Direction::UP) state.m_dir = Direction::DOWN; break;
|
||||
case 'd': if(state.m_dir != Direction::LEFT) state.m_dir = Direction::RIGHT; break;
|
||||
case 'a': if(state.m_dir != Direction::RIGHT) state.m_dir = Direction::LEFT; break;
|
||||
case 'q': state.running = false; break;
|
||||
case 'w': if(state.dir != Direction::DOWN) state.dir = Direction::UP; break;
|
||||
case 's': if(state.dir != Direction::UP) state.dir = Direction::DOWN; break;
|
||||
case 'd': if(state.dir != Direction::LEFT) state.dir = Direction::RIGHT; break;
|
||||
case 'a': if(state.dir != Direction::RIGHT) state.dir = Direction::LEFT; break;
|
||||
case 'q': return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SnakeGame::exit_summary() {
|
||||
if(!state.running) return false;
|
||||
if(buf.size() >= MAX_LEN - 1) {
|
||||
std::cout << "You filled the whole board and won! Score: " << state.m_score << "\n";
|
||||
return false;
|
||||
} if(segment_overlap()) {
|
||||
}
|
||||
for(int i : std::ranges::views::iota(0, (int)buf.size())) {
|
||||
if(buf[i] == head) {
|
||||
std::cout << "You collided with yourself and died! Score: " << state.m_score << "\n";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void SnakeGame::determine_index_zero_pos() {
|
||||
prev_head.set(head.x, head.y);
|
||||
|
||||
switch(state.m_dir) {
|
||||
switch(state.dir) {
|
||||
case Direction::LEFT: head.x = (head.x + COL - 1) % COL; break;
|
||||
case Direction::RIGHT: head.x = (head.x + 1) % COL; break;
|
||||
case Direction::UP: head.y = (head.y + ROW - 1) % ROW; break;
|
||||
@@ -79,22 +79,14 @@ void SnakeGame::move_tail_indicies() {
|
||||
buf.insert(buf.begin(), prev_head);
|
||||
}
|
||||
|
||||
void SnakeGame::add_tail_and_respawn_fruit(std::array<std::array<char, COL>, ROW> board) {
|
||||
void SnakeGame::add_tail_and_respawn_fruit(std::array<std::array<char, COL>, ROW> view) {
|
||||
if(fruit != head) return;
|
||||
int row, col;
|
||||
do {
|
||||
row = (rand() % ROW);
|
||||
col = (rand() % COL);
|
||||
} while (board[row][col] != '.');
|
||||
} while (view[row][col] != '.');
|
||||
fruit.set(row, col);
|
||||
buf.insert(buf.begin(), vec { ROW, COL });
|
||||
buf.insert(buf.begin(), vec2 { ROW, COL });
|
||||
state.m_score++;
|
||||
}
|
||||
|
||||
bool SnakeGame::segment_overlap() {
|
||||
if(buf.size() < 1) return false;
|
||||
for(int i : std::ranges::views::iota(0, (int)buf.size())) {
|
||||
if(buf[i] == head) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
#include "element.hpp"
|
||||
#include <element.hpp>
|
||||
|
||||
int main () {
|
||||
SnakeGame engine;
|
||||
|
||||
+8
-8
@@ -1,5 +1,5 @@
|
||||
#include "window.hpp"
|
||||
#include "element.hpp"
|
||||
#include <window.hpp>
|
||||
#include <element.hpp>
|
||||
#include <ncurses.h>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
@@ -7,21 +7,21 @@
|
||||
void Window::draw_display() {
|
||||
for(int i : std::ranges::views::iota(0, ROW)) {
|
||||
for(int j : std::ranges::views::iota(0, COL)) {
|
||||
mvaddch(i, j * 2, board[i][j]);
|
||||
mvaddch(i, j * 2, view[i][j]);
|
||||
}
|
||||
}
|
||||
refresh();
|
||||
}
|
||||
|
||||
void Window::update_display(vec &head, vec &fruit, std::vector<vec> &buf) {
|
||||
for(auto& row : board) row.fill('.');
|
||||
board[head.y][head.x] = '*';
|
||||
board[fruit.y][fruit.x] = 'o';
|
||||
void Window::update_display(vec2 &head, vec2 &fruit, std::vector<vec2> &buf) {
|
||||
for(auto& row : view) row.fill('.');
|
||||
view[head.y][head.x] = '*';
|
||||
view[fruit.y][fruit.x] = 'o';
|
||||
|
||||
for(int i : std::ranges::views::iota(0, (int)buf.size())) {
|
||||
const auto &segments = buf.at(i);
|
||||
if(segments.x < COL && segments.y < ROW) {
|
||||
board[segments.y][segments.x] = '~';
|
||||
view[segments.y][segments.x] = '~';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user