refactor: renamed snake to element and moved fruit to element. Removed a lot of unused functions and variables
This commit is contained in:
+15
-33
@@ -1,51 +1,33 @@
|
||||
#include "window.hpp"
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
Window::Window() {
|
||||
for(std::array<char, COL> &row : m_board) {
|
||||
row.fill('.');
|
||||
}
|
||||
m_score = 0;
|
||||
m_reset = 0;
|
||||
m_previous_reset = 0;
|
||||
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
|
||||
}
|
||||
|
||||
void Window::check_exit(bool &running, Body &body, bool &collision) {
|
||||
if(body.m_body.size() >= (ROW * COL)) {
|
||||
std::cout << "You win! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
||||
kill(running);
|
||||
} else if(collision) {
|
||||
std::cout << "You died! Score: " + std::to_string(body.m_body.size()) << std::endl;
|
||||
kill(running);
|
||||
void Window::update_display(Fruit &fruit, Head &head, Body &body) {
|
||||
// Update symbols of positions
|
||||
m_board[head.m_head.m_row][head.m_head.m_col] = '*';
|
||||
m_board[fruit.m_point.m_row][fruit.m_point.m_col] = 'X';
|
||||
for(int i = 0; i < body.m_body.size(); i++) {
|
||||
m_board[body.m_body.at(i).m_tail_pos.m_row][body.m_body.at(i).m_tail_pos.m_col] = '0';
|
||||
}
|
||||
}
|
||||
|
||||
void Window::draw_window() {
|
||||
std::cout << "\033[H" << std::flush; // Clear screen
|
||||
// Draw Screen
|
||||
for(std::array<char, COL> &row : m_board) {
|
||||
for(char &col : row) {
|
||||
std::cout << col << " ";
|
||||
}
|
||||
std::cout << "\r\n";
|
||||
}
|
||||
std::cout << std::flush;
|
||||
}
|
||||
|
||||
void Window::update_score(Fruit &fruit, Body &body) {
|
||||
if(m_score >= 1) {
|
||||
m_score = 0;
|
||||
body.m_body.push_back(Tail{2, 0, 0});
|
||||
}
|
||||
}
|
||||
|
||||
void Window::kill(bool &running) {
|
||||
running = false;
|
||||
}
|
||||
|
||||
void Window::update_display(int fps) {
|
||||
std::cout<<"\033[H" << std::flush;
|
||||
draw_window();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds((int) (200))); // 1000 / fps; 200ms = 5fps
|
||||
// Clear old spaces
|
||||
for(int i = 0; i < body.m_body.size(); i++) {
|
||||
body.m_body.at(i).m_tail_pos.clear(m_board);
|
||||
}
|
||||
head.m_head.clear(m_board);
|
||||
fruit.m_point.clear(m_board);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user