feat: (snake) collision with head and body

This commit is contained in:
DarthMooMancer
2025-08-29 17:25:09 -04:00
parent 157933c122
commit 684041b3c1
7 changed files with 55 additions and 38 deletions
+20 -9
View File
@@ -1,6 +1,7 @@
#include "window.hpp"
#include <iostream>
#include <chrono>
#include <string>
#include <thread>
Window::Window() {
@@ -12,9 +13,16 @@ Window::Window() {
m_previous_reset = 0;
}
void Window::draw_score() {
std::cout << "Score: " << m_score << std::endl;
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::draw_window() {
for(std::array<char, COL> &row : m_board) {
for(char &col : row) {
@@ -25,6 +33,13 @@ void Window::draw_window() {
std::cout << std::flush;
}
void Window::clear_window() {
std::cout<<"\033[H" << std::flush;
}
void Window::tick(double frames_per_second) {
std::this_thread::sleep_for(std::chrono::milliseconds((int) (1000 / frames_per_second)));
}
void Window::update_score(Fruit &fruit, Body &body) {
if(m_score % 1 == 0 && m_score >= 1) {
@@ -34,15 +49,11 @@ void Window::update_score(Fruit &fruit, Body &body) {
}
}
void Window::clear_window() {
std::cout<<"\033[H" << std::flush;
void Window::kill(bool &running) {
running = false;
}
void Window::tick(double frames_per_second) {
std::this_thread::sleep_for(std::chrono::milliseconds((int) (1000 / frames_per_second)));
}
void Window::update_loop(int fps) {
void Window::update_display(int fps) {
clear_window();
draw_window();
tick(fps);