You are on page 1of 6

#include #include #include #include #include

<SFML/Graphics.hpp> <SFML/System.hpp> <iostream> <vector> <SFML/Audio.hpp>

int Mazes_beat = 0; const int mazesize_x = 10; const int mazesize_y = 10; float speed = 50.f; const int blocksizex = 32; const int blocksizey = 32; const int playersizex = 28; const int playersizey = 28; int randommaze_fillingpercentage = 0; const int max_fillingrate=80; const int step_fillingrate=5; int personx=0; int persony=0; std::vector<sf::Image> images(10); std::vector<sf::Sprite> tiles(10);

// Initialize variables

int mapm[mazesize_y][mazesize_x] = { {0,0,0,0,0,0,0,0,0,6}, {1,1,0,1,1,1,1,1,1,1}, {1,1,0,0,0,0,0,0,0,0}, {1,1,1,1,1,1,1,1,1,0}, {1,1,1,1,1,1,1,1,1,0}, pecified {1,1,1,1,1,0,0,0,0,0}, {1,0,0,0,1,0,1,1,0,0}, {1,0,1,0,1,0,0,1,0,0}, {1,0,1,0,1,1,0,1,1,0}, {1,0,1,0,0,0,0,1,1,0} };

// Create level 1 maze as s

// ########################################################### // ########################################################### // ########################################################### int call_splashscreen() { if (!images[7].LoadFromFile("Splash.png")) { te Splash screen Image and sprite return EXIT_FAILURE; } tiles[7] = sf::Sprite(images[7]); //crea

sf::RenderWindow App(sf::VideoMode(mazesize_x*blocksizex, mazesize_y*blo cksizey, 32), "Splash Screen"); //create splash screen window tiles[7].SetPosition(personx, persony); App.Draw(tiles[7]); //draw created sprite App.Display(); while (App.IsOpened())

// Process events sf::Event Event; while (App.GetEvent(Event)) { // Close window : exit if (Event.Type == sf::Event::Closed) en window is closed App.Close(); } } } int call_endscreen() { if (!images[8].LoadFromFile("End.png")) { Splash screen Image and sprite return EXIT_FAILURE; } tiles[8] = sf::Sprite(images[8]);

//close window wh

//create

sf::RenderWindow App(sf::VideoMode(mazesize_x*blocksizex, mazesize_y*blo cksizey, 32), "End Screen"); //create splash screen window tiles[8].SetPosition(personx, persony); App.Draw(tiles[8]); //draw created sprite App.Display(); std::cout << "Congratulations you beat\n" << Mazes_beat <<"Mazes \n Than ks for playing"; while (App.IsOpened()) { // Process events sf::Event Event; while (App.GetEvent(Event)) { // Close window : exit if (Event.Type == sf::Event::Closed) //close window wh en window is closed App.Close(); } } } bool collideswall_or_outofbounds(int posx, int posy) { int xmaze = (int)(floor((float)(posx - 1)/(float)blocksizex)); //0-based int ymaze = (int)(floor((float)(posy - 1)/(float)blocksizey)); //0-based 2 coordinate sytems, got the amount of pixels and the amount of blo cks if (mapm[ymaze][xmaze]==1) { // If the player would collide with a wa ll returns true return true; } else if (!(xmaze>=0 && xmaze<=(mazesize_x-1) && ymaze>=0 && ymaze<=(m azesize_y-1) )) { // if the player would go outside the boundary of the maze re turns true return true; } else { return false; // Returns false and the player is allowed to mov e } } bool collides_portal(int posx, int posy) { int xmaze = (int)(floor((float)(posx - 1)/(float)blocksizex)); //0-based int ymaze = (int)(floor((float)(posy - 1)/(float)blocksizey));

//0-based if (mapm[ymaze][xmaze]==6) { return true; //if colided tile is portal return true } else { return false; } } bool pixelposition_ok(int posx, int posy) { //posx, posy parameters: left upper pixel position of the player sprite; checks bounding rectangle around t he player sprite //checks whether player sprite (or rather its bounding rectangle) does not br eak "rules" (out of bound or collides with wall) return (!( collideswall_or_outofbounds(posx, posy) || //left u pper pixel of player sprite collideswall_or_outofbounds(posx, posy+playersizey-1) || //leftlower pixel of player sprite collideswall_or_outofbounds(posx+playersizex-1, posy) || //right upper pixel of player sprite collideswall_or_outofbounds(posx+playersizex-1, posy+pla yersizey-1) //right lower pixel of player sprite )) ; } bool pixelposition_metportal(int posx, int posy) { //posx, posy paramete rs: left upper pixel position of the player sprite; checks bounding rectangle around the player sprite //checks whether player sprite (or rather its bounding rectangle) collided with portal return (( collides_portal(posx, posy) || //left upper collides_portal(posx, posy+playersizey-1) || //leftlo wer collides_portal(posx+playersizex-1, posy) || //right upper //Checks if player has colided with portal collides_portal(posx+playersizex-1, posy+playersizey-1) //right lower )) ; } void randomize_array() { srand((unsigned)time(0)); // seeds form the time in ms at the current mo ment randommaze_fillingpercentage+=step_fillingrate; randommaze_fillingpercentage = randommaze_fillingpercentage>=max_filling rate?max_fillingrate:randommaze_fillingpercentage; for (int y=0; y<=mazesize_y-1; y++) { for (int x=0; x<=mazesize_x-1; x++) { //Random maze gen eration based on filling percentage int r100 = (rand()%100)+1; //gets a random float between 1 and zero and scales it to 1 -100 mapm[y][x] = (r100<=randommaze_fillingpercentage?1:0);// if the random number is above the filling percentage that block is a passabel b lock } } int portalx=0; int portaly=0; do { portalx = (rand()%(mazesize_x))+1; //Random portal placement portaly = (rand()%(mazesize_y))+1;

} while (!(portalx>=0 && portalx<=(mazesize_x-1) && portaly>=0 && portal y<=(mazesize_y-1) )); mapm[portaly][portalx]=6; // keeps looping until you get a position insi de the maze } void randomize_player() { srand((unsigned)time(0)); do { personx = (rand()%(mazesize_x*blocksizex))+1; //Random player placement persony = (rand()%(mazesize_y*blocksizey))+1; } while ((!(pixelposition_ok(personx,persony))) || pixelposition_metport al(personx,persony)); //While it is in a wall/portal or out of bounds does not p lace and keeps loopingChecks that the player is not placed on a block or portal } // same function as portal placement but finds a starting position for the pla yer

int main() { call_splashscreen(); // Bring up the splash screen as a menu for the gam e personx=(2-1)*blocksizex; persony=(10-1)*blocksizey; // Set initial starting location of player

sf::RenderWindow App(sf::VideoMode(mazesize_x*blocksizex, mazesize_y*blo cksizey, 32), "Maze Game"); // Create the main rendering window sf::Clock Clock; if (!images[0].LoadFromFile("floor.png")) { return EXIT_FAILURE; } if (!images[1].LoadFromFile("block.png")) { return EXIT_FAILURE; } if (!images[5].LoadFromFile("Player.png")) { images to be used as sprites return EXIT_FAILURE; } if (!images[6].LoadFromFile("portal.png")) { return EXIT_FAILURE; } sf::SoundBuffer Buffer; if (!Buffer.LoadFromFile("Ding.wav")) // Should set up a sound buffer f or a cash machine "ding" sound - does not work { // Error... } sf::Sound Sound; Sound.SetBuffer(Buffer); // Create the sprite tiles[0] = sf::Sprite(images[0]);

//Load the

tiles[1] = sf::Sprite(images[1]); tiles[5] = sf::Sprite(images[5]); om each image tiles[6] = sf::Sprite(images[6]);

// Create a sprite fr

// Start game loop while (App.IsOpened()) { // Process events sf::Event Event; // Run whi le game is opened while (App.GetEvent(Event)) { // Close window : exit if (Event.Type == sf::Event::Closed) // Whe n window is closed, close app App.Close(); } // Get elapsed time float ElapsedTime = App.GetFrameTime(); // USed for movement spe ed Clock.Reset(); int requestedsteps_inpixels = ((int)(speed*ElapsedTime)); // G et elapsed time, used for movement int personxnew=personx; // from the movement key pressed calcula te the new position of player sprite (left upper pixel) int personynew=persony; if (App.GetInput().IsKeyDown(sf::Key::F) ) {speed*=1.1f;} //Speed controls F to increase S to decrease by 10% if (App.GetInput().IsKeyDown(sf::Key::S) ) {speed/=1.1f;} for(int cnt=1; cnt<=requestedsteps_inpixels; cnt++) { int newposx_test = personxnew; int newposy_test = personynew; if (App.GetInput().IsKeyDown(sf::Key::Left) ) {newposx _test -= 1;} if (App.GetInput().IsKeyDown(sf::Key::Right) ) {newposx _test += 1;} // Movement keys if (App.GetInput().IsKeyDown(sf::Key::Up) ) {newposy _test -= 1;} if (App.GetInput().IsKeyDown(sf::Key::Down) ) {newposy _test += 1;} if (pixelposition_ok(newposx_test, newposy_test)) { // passes the newpos test into the pixelposition ok function and if returns fa lse moves personxnew = newposx_test; personynew = newposy_test; // this makes it move } if (pixelposition_metportal(personxnew, personynew)) { Sound.Play(); // If my sound was working corr ectly this line should play it when the player hits a portal Mazes_beat ++; randomize_array(); // creates a new array randomize_player(); // When player hits portal generate a new map and a new player starting position break; // breaks out of the loop for the pixel t o move } personx = personxnew; persony = personynew; // movement

} // Clear screen & Redraw Maze App.Clear(); //clears for (int y = 0; y <= (mazesize_y-1); y++) { for (int x = 0; x <= (mazesize_x-1); x++) { // redraw ma ze created by the randomize array int tileId = mapm[y][x]; // Get the tile's image const sf::Image* image = tiles[tileId].GetImage( ); // Goes through the array and draws the map based on it // Get the width and height of the image int width = image->GetWidth(); int height = image->GetHeight(); // Adjust the offset by using the width tiles[tileId].SetPosition(x * width, y * height) ; // Draw the tile App.Draw(tiles[tileId]); } } // Draw Player tiles[5].SetPosition(personx, persony); App.Draw(tiles[5]); // Display window contents on screen App.Display(); // wait sf::Sleep(1.0f / 60.0f); } { call_endscreen(); } return EXIT_SUCCESS; }

You might also like