centipede 3
Sun Nov 26 2023 09:34:19 GMT+0000 (Coordinated Universal Time)
Saved by @yolobotoffender
#include <iostream> #include <SFML/Graphics.hpp> #include <SFML/Audio.hpp> using namespace std; // Initializing Dimensions. // resolutionX and resolutionY determine the rendering resolution. // Don't edit unless required. Use functions on lines 43, 44, 45 for resizing the game window. const int resolutionX = 960; const int resolutionY = 960; const int boxPixelsX = 32; const int boxPixelsY = 32; const int gameRows = resolutionX / boxPixelsX; // Total rows on grid const int gameColumns = resolutionY / boxPixelsY; // Total columns on grid // Initializing GameGrid. int gameGrid[gameRows][gameColumns] = {}; // The following exist purely for readability. const int x = 0; const int y = 1; const int exists = 2; //bool exists;// const int direction = 3; ///////////////////////////////////////////////////////////////////////////// // // // Write your functions declarations here. Some have been written for you. // // // ///////////////////////////////////////////////////////////////////////////// void drawPlayer(sf::RenderWindow& window, float player[], sf::Sprite& playerSprite); void movePlayer(float player[],float bullet[]); void moveBullet(float bullet[], sf::Clock& bulletClock); void drawBullet(sf::RenderWindow& window, float bullet[], sf::Sprite& bulletSprite); void drawShrooms(sf::RenderWindow& window, float shroom[][2], sf::Sprite& shroomSprite,int maxShrooms); void initializeShrooms(float shroom[][2],int maxShrooms); void initialize_centipede(float centipede[][4],int totalSegments); void drawCentipede(sf::RenderWindow& window, float centipede[12][4], sf::Sprite& centipedeSprite,const int totalSegments); void move_centipede(float centipede[][4], sf::Clock& bulletClock, int maxShrooms,float shroom[][2]); //remove from sf::render.. ////,int maxShrooms,float shroom[][2] // void bullet_shroom(float bullet[],float shroom[][2]); void shroom_centipede_collision(float centipede[][4],float shroom[][2],int totalSegments,int maxShrooms); //void player_shroom_collision( int main() { srand(time(0)); /* //centipede stuff: const int totalSegments = 12; float centipede[totalSegments][2]; // 2D array to store x and y positions of each segment // Initialize centipede positions (for example, starting from the top left) const int startX = 100; // Adjust as needed const int startY = 100; // Adjust as needed const int segmentGap = 20; // Gap between segments for (int i = 0; i < totalSegments; ++i) { centipede[i][0] = startX + i * segmentGap; // x position centipede[i][1] = startY; // y position (same for all segments in this example) } */ // Declaring RenderWindow. sf::RenderWindow window(sf::VideoMode(resolutionX, resolutionY), "Centipede", sf::Style::Close | sf::Style::Titlebar); // Used to resize your window if it's too big or too small. Use according to your needs. window.setSize(sf::Vector2u(640, 640)); // Recommended for 1366x768 (768p) displays. //window.setSize(sf::Vector2u(1280, 1280)); // Recommended for 2560x1440 (1440p) displays. // window.setSize(sf::Vector2u(1920, 1920)); // Recommended for 3840x2160 (4k) displays. // Used to position your window on every launch. Use according to your needs. window.setPosition(sf::Vector2i(100, 0)); // Initializing Background Music. sf::Music bgMusic; bgMusic.openFromFile("Centipede_Skeleton/Music/field_of_hopes.ogg"); bgMusic.play(); bgMusic.setVolume(50); // Initializing Background. sf::Texture backgroundTexture; sf::Sprite backgroundSprite; backgroundTexture.loadFromFile("Centipede_Skeleton/Textures/background.png"); backgroundSprite.setTexture(backgroundTexture); backgroundSprite.setColor(sf::Color(255, 255, 255, 200)); // Reduces Opacity to 25% // Initializing Player and Player Sprites. float player[2] = {}; player[x] = (gameColumns / 2) * boxPixelsX; player[y] = (gameColumns * 3 / 4) * boxPixelsY; sf::Texture playerTexture; sf::Sprite playerSprite; playerTexture.loadFromFile("Centipede_Skeleton/Textures/player.png"); playerSprite.setTexture(playerTexture); playerSprite.setTextureRect(sf::IntRect(0, 0, boxPixelsX, boxPixelsY)); sf::Clock playerClock; // Initializing Bullet and Bullet Sprites. float bullet[3] = {}; //bool bullet1[3]; bool request = false; bullet[x] = player[x]; bullet[y] = player[y] - boxPixelsY; bullet[exists] = false; sf::Clock bulletClock; sf::Texture bulletTexture; sf::Sprite bulletSprite; bulletTexture.loadFromFile("Centipede_Skeleton/Textures/bullet.png"); bulletSprite.setTexture(bulletTexture); bulletSprite.setTextureRect(sf::IntRect(0, 0, boxPixelsX, boxPixelsY)); //initializing centipede const int totalSegments = 12; float centipede[100][4]; //centipede[x] = (gameColumns / 2) * boxPixelsX; //the position from where centipede will start its journey x-co-ordinate// //centipede[y] = (gameColumns * 3 / 4) * boxPixelsY; //the position from where centipede will start its journey y-co-ordinate// //centipede[1][exists] = false; for(int i=0;i<totalSegments;i++){ centipede[i][exists] = true; } sf::Texture centipedeTexture; sf::Sprite centipedeSprite; centipedeTexture.loadFromFile("Centipede_Skeleton/Textures/c_body_left_walk.png"); centipedeSprite.setTexture(centipedeTexture); centipedeSprite.setTextureRect(sf::IntRect(0, 0, boxPixelsX, boxPixelsY)); sf::Clock centipedeClock; initialize_centipede(centipede,totalSegments); //initializing shrooms: const int maxShrooms = 18; float shroom[25][2] = {}; sf::Texture shroomTexture; sf::Sprite shroomSprite; shroomTexture.loadFromFile("Centipede_Skeleton/Textures/mushroom.png"); shroomSprite.setTexture(shroomTexture); shroomSprite.setTextureRect(sf::IntRect(0, 0, boxPixelsX, boxPixelsY)); for(int i=0;i<maxShrooms;i++){ shroom[i][exists] = true; } initializeShrooms(shroom,maxShrooms); //calling shroom's function to initialize position; // shroom_centipede_collision(centipede,shroom,totalSegments,maxShrooms); while(window.isOpen()) { /////////////////////////////////////////////////////////////// // // // Call Your Functions Here. Some have been written for you. // // Be vary of the order you call them, SFML draws in order. // // // /////////////////////////////////////////////////////////////// window.draw(backgroundSprite); drawPlayer(window, player, playerSprite); movePlayer(player,bullet); /*shootBullet(bullet,request); if(request){ bullet[exists] = true; request = false; } */ if (bullet[exists] == true) { moveBullet(bullet, bulletClock); drawBullet(window, bullet, bulletSprite); } drawShrooms(window,shroom,shroomSprite,maxShrooms); bullet_shroom(bullet,shroom); drawCentipede(window, centipede, centipedeSprite,totalSegments); move_centipede(centipede,centipedeClock,maxShrooms,shroom); //,maxShrooms,shroom// shroom_centipede_collision(centipede,shroom,totalSegments,maxShrooms); sf::Event e; while (window.pollEvent(e)) { if (e.type == sf::Event::Closed) { return 0; } } window.display(); window.clear(); } } //////////////////////////////////////////////////////////////////////////// // // // Write your functions definitions here. Some have been written for you. // // // //////////////////////////////////////////////////////////////////////////// void drawPlayer(sf::RenderWindow& window, float player[], sf::Sprite& playerSprite) { playerSprite.setPosition(player[x], player[y]); window.draw(playerSprite); } void drawBullet(sf::RenderWindow& window, float bullet[], sf::Sprite& bulletSprite) { if(bullet[exists] == true){ bulletSprite.setPosition(bullet[x], bullet[y]); window.draw(bulletSprite); } } void moveBullet(float bullet[], sf::Clock& bulletClock) { float bullet_speed = 10.0f; if (bulletClock.getElapsedTime().asMilliseconds() < 10) return; bulletClock.restart(); bullet[y] += -32; if (bullet[y] < -32) { bullet[exists] = false; } } void drawShrooms(sf::RenderWindow& window, float shroom[][2], sf::Sprite& shroomSprite,int maxShrooms){ for(int i=0;i<maxShrooms;i++){ if(shroom[i][exists]){ shroomSprite.setPosition(shroom[i][x],shroom[i][y]); window.draw(shroomSprite); } } } void initializeShrooms(float shroom[][2],int maxShrooms){ for(int i=0;i<maxShrooms;i++){ shroom[i][x] = rand()%gameRows * boxPixelsX; shroom[i][y] = rand()%gameColumns * boxPixelsY; // shroom[i][exists] = true; } } } void movePlayer(float player[],float bullet[]) { float movementSpeed = 5.0f; int bottomLimit = resolutionY - (6 * boxPixelsY); // Calculate the bottom limit if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) && player[y] > bottomLimit) { player[y] -= movementSpeed + 3; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::S) && player[y] < resolutionY - boxPixelsY) { player[y] += movementSpeed + 3; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) && player[x] < resolutionX - boxPixelsX) { player[x] += movementSpeed + 3; } if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) && player[x] > 0) { player[x] -= movementSpeed + 3; } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && bullet[exists]==false){ bullet[exists] = true; bullet[x] = player[x]; bullet[y] = player[y] - boxPixelsY; } } void initialize_centipede(float centipede[][4],int totalSegments){ for(int j=0;j<totalSegments;j++){ centipede[j][x] = boxPixelsX*j; centipede[j][y] = boxPixelsY; centipede[j][exists] = true; centipede[j][direction] = 1; //1 for right and 0 for left; } } void drawCentipede(sf::RenderWindow& window, float centipede[12][4], sf::Sprite& centipedeSprite,const int totalSegments) { const int segmentWidth = boxPixelsX; // Width of each centipede segment const int segmentHeight = boxPixelsY; // Height of each centipede segment for (int i = 0; i < totalSegments; ++i) { if(centipede[i][exists]){ centipedeSprite.setPosition(centipede[i][x], centipede[i][y]); window.draw(centipedeSprite); } } } void move_centipede(float centipede[][4], sf::Clock& centipedeClock, int maxShrooms,float shroom[][2] ) { //, int maxShrooms,float shroom[][2] // int totalSegments = 12; if (centipedeClock.getElapsedTime().asMilliseconds() < 100) return; centipedeClock.restart(); bool reachedBottomRight = true; bool reachedBottomLeft = true; shroom_centipede_collision(centipede,shroom,totalSegments,maxShrooms); for (int j = 0;j < totalSegments ; j++) { if (centipede[j][direction] == 1 ) { // Moving right //// if (centipede[j][x] < 928) { centipede[j][x] += 32; if (centipede[j][y] != 928) { reachedBottomRight = false; reachedBottomLeft = false; } } else { centipede[j][direction] = 0; // Change direction to down centipede[j][y] += 32; // Move down a row } } else { // Moving left if (centipede[j][x] > 0) { centipede[j][x] -= 32; if (centipede[j][y] != 928) { reachedBottomRight = false; reachedBottomLeft = false; } } else { centipede[j][direction] = 1; // Change direction to down centipede[j][y] += 32; // Move down a row } } } for (int j = 0; j < totalSegments; j++){ if (centipede[j][y] == 928 && centipede[j][x] == 928){ reachedBottomRight = true;} else{reachedBottomRight = false;} if(centipede[j][y] == 928 && centipede[j][x] < 0){ reachedBottomLeft = true; } else{reachedBottomLeft = false;} if (reachedBottomRight || reachedBottomLeft) { // Move to the 6th row above the bottom { centipede[j][y] = 928 - (7 * boxPixelsY); } } } /*mushroom_centipede collsion: initializeShrooms(shroom,maxShrooms); for(int i=0,j=0;i<maxShrooms && j<totalSegments;i++,j++){ if(shroom[i][x] == centipede[j][x] && shroom[i][y] == centipede[j][y]){ centipede[j][y] += 32;} */ } void bullet_shroom(float bullet[], float shroom[][2]) { float bulletX = bullet[x]; float bulletY = bullet[y]; for (int i = 0; i < 18; i++) { float shroomX = shroom[i][x]; float shroomY = shroom[i][y]; // Define a range around the mushroom position for collision detection float collisionRange = 16.0f; // Adjust this value as needed // Check if bullet position is within the range of a mushroom if (bulletX >= shroomX - collisionRange && bulletX <= shroomX + collisionRange && bulletY >= shroomY - collisionRange && bulletY <= shroomY + collisionRange) { shroom[i][exists] = false; bullet[exists] = false; // break; // Exit loop after handling collision with one mushroom// } /* if(shroom[i][exists] == false){ bullet[exists] == true; } */ } } void shroom_centipede_collision(float centipede[][4],float shroom[][2],int totalSegments,int maxShrooms){ //initialize_centipede(centipede,totalSegments); //initializeShrooms(shroom,maxShrooms); for(int i=0;i<totalSegments;i++){ for(int j=0;j<maxShrooms;j++){ if(centipede[i][x] == shroom[j][x] && centipede[i][y] == shroom[j][y] && centipede[i][y] != 928 && shroom[i][x] != 0){ centipede[i][y] += 32; } else if(centipede[i][x] == shroom[j][x] && centipede[i][y] == shroom[j][y] && centipede[i][y] == 928 ){ centipede[i][y] -= 32; } } } } /*for(int i=0,j=0;i<maxShrooms && j<totalSegments;i++,j++){ if(centipede[j][x] == shroom[i][x] && centipede[j][y] == shroom[j][y] ){ centipede[j][y] += 32; } } } */
Comments