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" #include "globals.hpp"
struct Point { struct Point {
Point() : m_row(0), m_col(0) {}; Point() : m_row(0), m_col(0), m_symbol(' ') {};
Point(int row, int col) : m_row(row), m_col(col) {}; Point(int row, int col, char symbol) : m_row(row), m_col(col), m_symbol(symbol) {};
int m_row; int m_row;
int m_col; int m_col;
char m_symbol;
void assign(int row, int col); void assign(int row, int col);
void to_string(); void to_string();
}; };
struct Bird { struct Bird {
Bird() : m_fly(false) {}; Bird() : m_fly(false) {};
Point m_body {(ROW/2), 4}; Point m_body { (ROW/2), 4, '*' };
bool m_fly; bool m_fly;
void move(); void move();
void check_collision(); void check_collision();
+2 -1
View File
@@ -30,9 +30,10 @@ Pipe::Pipe() {
int _start = (rand() % (ROW - 3)); int _start = (rand() % (ROW - 3));
for(int i = 0; i < m_size; i++) { for(int i = 0; i < m_size; i++) {
m_nodes[i].assign(i, COL - 1); m_nodes[i].assign(i, COL - 1);
m_nodes[i].m_symbol = '|';
} }
for(int i = _start; i < _start + 3; i++) { for(int i = _start; i < _start + 3; i++) {
m_nodes[i].assign(ROW, COL); m_nodes[i].m_symbol = ' ';
} }
} }
+5 -6
View File
@@ -42,12 +42,11 @@ int main() {
} }
for(int j = 0; j < pipes.size(); j++) { for(int j = 0; j < pipes.size(); j++) {
for(int i = 0; i < pipes.front().m_size; i++) { 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_col != bird.m_body.m_col) continue;
if(pipes.at(j).m_nodes[i].m_row == bird.m_body.m_row) { if(pipes.at(j).m_nodes[i].m_row != bird.m_body.m_row) continue;
running = false; if(pipes.at(j).m_nodes[i].m_symbol == ' ') continue;
std::cout << "You died!" << std::endl; running = false;
} std::cout << "You died!" << std::endl;
}
} }
} }
window.update_display(bird, pipes); window.update_display(bird, pipes);
+4 -2
View File
@@ -10,10 +10,12 @@ void Window::tick(int fps) {
void Window::update_display(Bird &bird, std::vector<Pipe> pipes) { void Window::update_display(Bird &bird, std::vector<Pipe> pipes) {
for(std::array<char, COL> &row : m_board) { row.fill('.'); } 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 i = 0; i < pipes.size(); i++) {
for(int j = 0; j < pipes.front().m_size; j++) { 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;
}
} }
} }