fix: fixed smashing error by checking for symbols instead of just setting points to ROW and COL when making hole in pipe

This commit is contained in:
DarthMooMancer
2025-09-04 19:35:34 -04:00
parent f58907eb93
commit 57118a9fc2
4 changed files with 15 additions and 12 deletions
+4 -3
View File
@@ -4,17 +4,18 @@
#include "globals.hpp"
struct Point {
Point() : m_row(0), m_col(0) {};
Point(int row, int col) : m_row(row), m_col(col) {};
Point() : m_row(0), m_col(0), m_symbol(' ') {};
Point(int row, int col, char symbol) : m_row(row), m_col(col), m_symbol(symbol) {};
int m_row;
int m_col;
char m_symbol;
void assign(int row, int col);
void to_string();
};
struct Bird {
Bird() : m_fly(false) {};
Point m_body {(ROW/2), 4};
Point m_body { (ROW/2), 4, '*' };
bool m_fly;
void move();
void check_collision();
+2 -1
View File
@@ -30,9 +30,10 @@ Pipe::Pipe() {
int _start = (rand() % (ROW - 3));
for(int i = 0; i < m_size; i++) {
m_nodes[i].assign(i, COL - 1);
m_nodes[i].m_symbol = '|';
}
for(int i = _start; i < _start + 3; i++) {
m_nodes[i].assign(ROW, COL);
m_nodes[i].m_symbol = ' ';
}
}
+3 -4
View File
@@ -42,14 +42,13 @@ int main() {
}
for(int j = 0; j < pipes.size(); j++) {
for(int i = 0; i < pipes.front().m_size; i++) {
if(pipes.at(j).m_nodes[i].m_col == bird.m_body.m_col) {
if(pipes.at(j).m_nodes[i].m_row == bird.m_body.m_row) {
if(pipes.at(j).m_nodes[i].m_col != bird.m_body.m_col) continue;
if(pipes.at(j).m_nodes[i].m_row != bird.m_body.m_row) continue;
if(pipes.at(j).m_nodes[i].m_symbol == ' ') continue;
running = false;
std::cout << "You died!" << std::endl;
}
}
}
}
window.update_display(bird, pipes);
frames++;
if(frames > 4) {
+4 -2
View File
@@ -10,10 +10,12 @@ void Window::tick(int fps) {
void Window::update_display(Bird &bird, std::vector<Pipe> pipes) {
for(std::array<char, COL> &row : m_board) { row.fill('.'); }
m_board[bird.m_body.m_row][bird.m_body.m_col] = '*';
m_board[bird.m_body.m_row][bird.m_body.m_col] = bird.m_body.m_symbol;
for(int i = 0; i < pipes.size(); i++) {
for(int j = 0; j < pipes.front().m_size; j++) {
m_board[pipes.at(i).m_nodes[j].m_row][pipes.at(i).m_nodes[j].m_col] = '|';
if(pipes.at(i).m_nodes[j].m_symbol == '|') {
m_board[pipes.at(i).m_nodes[j].m_row][pipes.at(i).m_nodes[j].m_col] = pipes.at(i).m_nodes[j].m_symbol;
}
}
}