I added some (smaller) blocks, and the greatest thing happens every once in a while. I just jump into the bottom of it, and about one of every five times, it stops and crashes the game. Another one is that if I jump into one of the big walls (on the sides) and keep going I fall through the bottom.
//Includes #include <SFML/Graphics.hpp> #include <string> #include <sstream> #include <vector> //Namespaces using namespace std; using namespace sf; //Classes class player { public: sf::Sprite sprite; float x; float y; float xprevious; float yprevious; float gravity; float vspeed; bool gravity_on; }; class solid { public: sf::Sprite sprite; float x; float y; float wall_animation; }; //Variables bool cjump = false, isRunning = true; unsigned int i; long num_of_scr = 0; string scr_name = "screenshot0.jpg"; //Misc functions bool check_position_solid(float a, float b, std::vector<solid> c) { for (i = 0; i < c.size(); i++) { if (a > c[i].x and a < c[i].x + (c[i].sprite.GetSubRect()).GetWidth() and b > c[i].y and b < c[i].y + (c[i].sprite.GetSubRect()).GetHeight()) { return true; } } return false; } //Game int main() { vector<solid> solids; sf::RenderWindow App(sf::VideoMode(650, 500, 32), "First Game"); App.SetFramerateLimit(60); sf::Image char_image; if (!char_image.LoadFromFile("char_sprite.png")) { return 1; } sf::Image wall_image; if (!wall_image.LoadFromFile("wall_spritesheet.png")) { return 1; } sf::Image block_image; if (!block_image.LoadFromFile("block_sprite.png")) { return 1; } player player; player.x = 325; player.y = 250; player.gravity = 0.1; player.vspeed = 0; player.gravity_on = true; player.sprite.SetX(player.x); player.sprite.SetY(player.y); player.sprite.SetImage(char_image); for (i = 0; i < 650/50; ++i) { // Add top and bottom rows solids.push_back(solid()); solids.back().sprite.SetImage(wall_image); solids.back().sprite.SetSubRect(sf::Rect<int> (0,0,50,50)); solids.back().wall_animation = round(rand() % 27 + 0); solids.back().x = i * 50; solids.back().y = 100; solids.push_back(solid()); solids.back().sprite.SetImage(wall_image); solids.back().sprite.SetSubRect(sf::Rect<int> (0,0,50,50)); solids.back().wall_animation = round(rand() % 27 + 0); solids.back().x = i * 50; solids.back().y = 450; } for (i = 100/50; i < 500/50; ++i) { // Add both columns solids.push_back(solid()); solids.back().sprite.SetImage(wall_image); solids.back().sprite.SetSubRect(sf::Rect<int> (0,0,50,50)); solids.back().wall_animation = round(rand() % 27 + 0); solids.back().x = 0; solids.back().y = i * 50; solids.push_back(solid()); solids.back().sprite.SetImage(wall_image); solids.back().sprite.SetSubRect(sf::Rect<int> (0,0,50,50)); solids.back().wall_animation = round(rand() % 27 + 0); solids.back().x = 600; solids.back().y = i * 50; } for (i = 50/25; i < 250/25; ++i) { solids.push_back(solid()); solids.back().sprite.SetImage(block_image); solids.back().x = i * 25; solids.back().y = 375; } for (i = 50/25; i < 250/25; ++i) { solids.push_back(solid()); solids.back().sprite.SetImage(block_image); solids.back().x = i * 25 + 350; solids.back().y = 300; } for (i = 50/25; i < 250/25; ++i) { solids.push_back(solid()); solids.back().sprite.SetImage(block_image); solids.back().x = i * 25; solids.back().y = 225; } sf::String how_to_scr("Press F1 to take a screenshot of the game. \nThey will appear in the game folder.", sf::Font::GetDefaultFont(), 30); how_to_scr.SetColor(sf::Color(255,255,255)); how_to_scr.SetX(8); how_to_scr.SetY(8); while (isRunning == true) { sf::Event Event; while (App.GetEvent(Event)) { if (Event.Type == sf::Event::Closed) { App.Close(); } else if (Event.Type == sf::Event::KeyPressed) { if (Event.Key.Code == sf::Key::Escape) { isRunning = false; } else if (Event.Key.Code == sf::Key::F1) { sf::Image Screen = App.Capture(); Screen.SaveToFile(scr_name); num_of_scr += 1; std::stringstream textStream; textStream << "screenshot" << num_of_scr << ".jpg"; scr_name = textStream.str(); } } } player.xprevious = player.x; player.yprevious = player.y; if (App.GetInput().IsKeyDown(sf::Key::Left)) { player.x = (player.x - 3); player.sprite.SetX(player.x); } if (App.GetInput().IsKeyDown(sf::Key::Right)) { player.x = (player.x + 3); player.sprite.SetX(player.x); } for (i = 0; i < solids.size(); i++) { if (((player.x + (player.sprite.GetSubRect()).GetWidth() ) > solids[i].x and player.x < solids[i].x + (solids[i].sprite.GetSubRect()).GetWidth()) and ((player.y + (player.sprite.GetSubRect()).GetHeight()) > solids[i].y and player.y < solids[i].y + (solids[i].sprite.GetSubRect()).GetHeight())) { if (check_position_solid(player.x + (player.sprite.GetSubRect()).GetWidth() + 1, player.y + 9, solids)) { player.x = player.xprevious; } else if (check_position_solid(player.x - 1, player.y + ( ( player.sprite.GetSubRect() ).GetHeight() )/2, solids)) { player.x = player.xprevious; } else if (check_position_solid(player.x + ((player.sprite.GetSubRect()).GetWidth())/2, player.y + (player.sprite.GetSubRect()).GetHeight() + 1, solids)) { player.y = player.yprevious - 0.75; player.vspeed = 0; player.gravity_on = false; cjump = true; } else if (check_position_solid(player.x + ((player.sprite.GetSubRect()).GetWidth())/2, player.y - 1, solids)) { player.y = player.yprevious + 1.5; player.gravity_on = true; player.vspeed = 0; } if (check_position_solid(player.x + ((player.sprite.GetSubRect()).GetWidth())/2, player.y - 1, solids)) { player.y = player.yprevious + 1.5; player.gravity_on = true; player.vspeed = 0; } } } if (player.gravity_on) { player.vspeed += player.gravity; } if (App.GetInput().IsKeyDown(sf::Key::Up) and cjump == true) { cjump = false; player.vspeed = -4; player.gravity_on = true; } if ( !( check_position_solid(player.x + ((player.sprite.GetSubRect()).GetWidth())/2, player.y + (player.sprite.GetSubRect()).GetHeight() + 1, solids) ) and player.gravity_on == false and player.vspeed == 0) { player.gravity_on = true; } player.y += player.vspeed; App.Clear(); App.Draw(how_to_scr); player.sprite.SetX(player.x); player.sprite.SetY(player.y); App.Draw(player.sprite); for(i = 0; i < solids.size(); i++) { solid& w = solids[i]; // This gets a reference to solid i. The & makes sure of that. I use this as it is easier to type :) w.sprite.SetPosition(w.x, w.y); if (w.sprite.GetImage() == solids.front().sprite.GetImage()) { if (w.wall_animation >= 0 and w.wall_animation < 1) { w.sprite.SetSubRect(sf::Rect<int>(50,0,100,50)); w.wall_animation += 0.2; } else if (w.wall_animation == 1) { w.sprite.SetSubRect(sf::Rect<int>(100,0,150,50)); w.wall_animation += 1; } else if (w.wall_animation == 2) { w.sprite.SetSubRect(sf::Rect<int>(150,0,200,50)); w.wall_animation += 1; } else if (w.wall_animation == 3) { w.sprite.SetSubRect(sf::Rect<int>(200,0,250,50)); w.wall_animation += 1; } else if (w.wall_animation == 4) { w.sprite.SetSubRect(sf::Rect<int>(250,0,300,50)); w.wall_animation += 1; } else if (w.wall_animation == 5) { w.sprite.SetSubRect(sf::Rect<int>(300,0,350,50)); w.wall_animation += 1; } else if (w.wall_animation == 6) { w.sprite.SetSubRect(sf::Rect<int>(350,0,400,50)); w.wall_animation += 1; } else if (w.wall_animation == 7) { w.sprite.SetSubRect(sf::Rect<int>(400,0,450,50)); w.wall_animation += 1; } else if (w.wall_animation == 8) { w.sprite.SetSubRect(sf::Rect<int>(450,0,500,50)); w.wall_animation += 1; } else if (w.wall_animation == 9) { w.sprite.SetSubRect(sf::Rect<int>(500,0,550,50)); w.wall_animation += 1; } else if (w.wall_animation == 10) { w.sprite.SetSubRect(sf::Rect<int>(550,0,600,50)); w.wall_animation += 1; } else if (w.wall_animation == 11) { w.sprite.SetSubRect(sf::Rect<int>(600,0,650,50)); w.wall_animation += 1; } else if (w.wall_animation == 12) { w.sprite.SetSubRect(sf::Rect<int>(650,0,700,50)); w.wall_animation += 1; } else if (w.wall_animation == 13) { w.sprite.SetSubRect(sf::Rect<int>(700,0,750,50)); w.wall_animation += 1; } else if (w.wall_animation == 14) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 1; } else if (w.wall_animation == 15) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 1; } else if (w.wall_animation == 16) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 1; } else if (w.wall_animation == 17) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 1; } else if (w.wall_animation == 18) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 1; } else if (w.wall_animation == 19) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 1; } else if (w.wall_animation == 20) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 1; } else if (w.wall_animation == 21) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 1; } else if (w.wall_animation == 22) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 1; } else if (w.wall_animation >= 23 and w.wall_animation < 24) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 0.05; } else if (w.wall_animation >= 24 and w.wall_animation < 25) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 0.05; } else if (w.wall_animation >= 25 and w.wall_animation < 26) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 0.05; } else if (w.wall_animation >= 26 and w.wall_animation < 27) { w.sprite.SetSubRect(sf::Rect<int>(750,0,800,50)); w.wall_animation += 0.05; } else if (w.wall_animation >= 27) { w.sprite.SetSubRect(sf::Rect<int>(0,0,50,50)); w.wall_animation = 0; } } App.Draw(w.sprite); } App.Display(); } App.Close(); return 0; }
Anyone see anything about what is wrong?
(EDIT: Code updated semi-significantly)