I haven't gone over it carefully, but a few errors stood out to me straight away.
Think of your game's map as a grid on a piece of paper, like for a game of Battleship. This grid is 100 squares wide and 50 squares tall. The biggest problem with this code is that it often tries to look for or draw a square that is off the paper.
The player starts in the wrong place
Imagine your paper grid. Since it's 50 squares tall, you can number the rows from 0 to 49.
For most levels, the code says
startY = 50. It's trying to place the player in row 50. But since the rows only go up to 49, row 50 doesn't exist and (probably) the program immediately crashes.
Code: if ( levelSelection == 1){
tilemap.map = level1_map;
startX = 46;
startY = 50; // Problem is here
}
Major logical error in map Loading
Code: for (int i = 0; i < 100; i++){
for (int j = 0; j < 50; j++){
if (tilemap.map[i+(j*50)] == 0x00){
map[i][j] = 'X';
// ...
}
}
}
This formula i + (j * 50) assumes the map is 50 tiles wide. However, the loop for i goes from 0 to 99. This means the code is trying to read from a map that is conceptually 50 tiles wide but is accessing it as if it were 100 tiles wide.
Maybe you were thinking of declaring it as map[50][100] instead of [100][50]? If so, then inside the loops, you'd have to use map[j][i] instead of map[i][j] (with the indices swapped). That would make it consistent with the loops and the formuĊa.
There are no boundary checks in the code, which implies the map data is designed with walls around the edges. Thanks to this, wall collision keeps the player from going out of bounds (am I right?). But if the map is loaded incorrectly and is missing these edge walls, any movement can lead to a crash as soon as the player crosses the boundary.