bari code

PHOTO EMBED

Fri Dec 01 2023 15:51:05 GMT+0000 (Coordinated Universal Time)

Saved by @yolobotoffender

#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cstdlib>
#include <ctime>
#include<string>
using namespace std;
using namespace sf;
bool changing=true;
bool gameover=false;
const int resolutionX = 960;
const int resolutionY = 960;
const int boxPixelsX = 32;
const int boxPixelsY = 32;
const int gameRows = resolutionX / boxPixelsX;
const int gameColumns = resolutionY / boxPixelsY;

const int x = 0;
const int y = 1;
const int exists = 2;
int t;
bool headshot=false;




int maxcentipede=1;

void drawline(RenderWindow &window , float player[]){

VertexArray line(Lines,2);
line[0].position=Vector2f(830,830);
line[1].position=Vector2f(10,830);
line[0].color=Color::Yellow;
line[1].color=Color::Red;

window.draw(line);

}




/*void newcentipede(int j   , float givingposition2[][3],Sprite centipedetail2[][2], Sprite centipedehead2[][2]  )
{


 Sprite centipedetail2[12][2];
    Texture centipedetailtexture2;
    Texture centipedeTexture2;
  //  float positiontail[12][3];
    Sprite centipedehead2[12][2];
    float givingposition2[12][3];
    centipedeTexture2.loadFromFile("Textures/head.png");
    centipedetailtexture2.loadFromFile("Textures/body.png");
//if(givingposition[0][exists]==true)
//{

    givingposition2[0][x] = (rand() % 1000) + boxPixelsX;
    givingposition2[0][y] =(rand() % 1000) + boxPixelsY;
    givingposition2[0][exists]=true;
    centipedehead2[0].setTextureRect(IntRect(0, 0, boxPixelsX, boxPixelsY));
    centipedehead2[0].setPosition(givingposition2[0][x], givingposition2[0][y]);
    centipedehead2[0].setTexture(centipedeTexture2);
   // centipedehead[1][exists]=true;


    for (int i = 0; i <12; i++)
    {
        givingposition2[i][x] = givingposition2[i - 1][x] + boxPixelsX;
        givingposition2[i][y] = givingposition2[i - 1][y];
        givingposition2[i][exists]=true;
        
    }


    for (int i = 0; i < 12; i++)
    { 
	
        centipedetail2[i][1].setTextureRect(IntRect(0, 0, boxPixelsX, boxPixelsY));
        centipedetail2[i][1].setTexture(centipedetailtexture);}
        centipedetail2[i][1].setPosition(givingposition2[i][x], givingposition2[i][y]);
        //centipedetail[i][exists]=true;
    }




}
*/
void movecentipede(RenderWindow &window, float givingposition[12][12][3], Clock &centipedeclock, float mushroom[][3])
{
    if (centipedeclock.getElapsedTime().asMilliseconds() < 200)
        return;

    centipedeclock.restart();

    static bool left = true;

    for (int c = 0; c < maxcentipede; c++)
    {
        if (!headshot)
        {
            // Move the body segments first
            for (int i = 11; i > 0; i--)
            {
                givingposition[c][i][x] = givingposition[c][i - 1][x];
                givingposition[c][i][y] = givingposition[c][i - 1][y];
            }

            // Move the head based on the direction
            if (left)
            {
                givingposition[c][0][x] -= 32;
            }
            if(!left)
            {
                givingposition[c][0][x] += 32;
            }

            // Check if the centipede collides with the screen borders
            if (givingposition[c][0][x] < 0 || givingposition[c][0][x] >= resolutionX - boxPixelsX)
            {
                left = !left;

                // Move the entire centipede down
                for (int j = 0; j < 12; j++)
                {
                    if (givingposition[c][j][y] < 880)
                    {
                        givingposition[c][j][y] += boxPixelsY;
                    }
                }
            }
           /* else if ((givingposition[c][0][x] >= mushroom[0][x] && givingposition[c][0][x] <= mushroom[0][x] + boxPixelsX &&
                      givingposition[c][0][y] >= mushroom[0][y] && givingposition[c][0][y] <= mushroom[0][y] + boxPixelsY))
            {
                left = !left;

                // Move the entire centipede down
                for (int j = 0; j < 12; j++)
                {
                    givingposition[c][j][y] += boxPixelsY;
                    mushroom[j][exists] = false;
                }
            }*/
            else
            {
                // Check if the head collides with mushrooms
                for (int j = 1; j < 24; j++)
                {
                    if (givingposition[c][0][x] >= mushroom[j][x] && givingposition[c][0][x] <= mushroom[j][x] + boxPixelsX &&
                        givingposition[c][0][y] >= mushroom[j][y] && givingposition[c][0][y] <= mushroom[j][y] + boxPixelsY)
                    {
                        left = !left;

                        // Move the entire centipede down
                        for (int k = 0; k < 12; k++)
                        {
                            givingposition[c][k][y] += boxPixelsY;
                            mushroom[j][exists] = false;
                        }
                        break;
                    }
                }
            }
        }
    }
}






void splitCentipedeBody(RenderWindow &window, float givingposition[][12][3], int c, int j, bool left, Sprite centipedetail[][12][2], Sprite centipedehead[][2], Clock &centipedeclock, float mushroom[][3]) {
     // Body segment is hit, split into two independent segments
    for (int k = j; k < 12; k++) {
        givingposition[c][k][exists] = false;

        // Move the hit segment in the opposite direction
        if (left) {
        
        	givingposition[c+1][k][x]=givingposition[c][k][x];
        		givingposition[c+1][k][y]=givingposition[c][k][y];
        
            givingposition[c + 1][k][x] += boxPixelsX - 0.9f;
          //  movecentipede(window, givingposition, centipedeclock, mushroom);
        } if(!left) {
        	givingposition[c+1][k][exists]=true;
        		givingposition[c+1][k][x]=givingposition[c][k][x];
        		givingposition[c+1][k][x]=givingposition[c][k][x];
        	givingposition[c+1][k][exists]=true;
            givingposition[c + 1][k][x] -= boxPixelsX + 0.9f;
           // movecentipede(window, givingposition, centipedeclock, mushroom);
        }

        // Copy the head position from the previous segment to the new one
       givingposition[c + 1][0][x] = givingposition[c][k-1][x];
        givingposition[c + 1][0][y] = givingposition[c][k-1][y];

        // Ensure that you don't exceed the maximum number of segments
        if (k + 1 < 12) {
            //givingposition[c][k + 1][x] = -0.1f;
            //givingposition[c][k + 1][y] = -0.1f;
        }

        // Set the new segment at the same location and mark it as the head
      /*  givingposition[c + 1][k][exists] = true;
        givingposition[c + 1][k][x] = givingposition[c][k + 1][x];
        givingposition[c + 1][k][y] = givingposition[c][k + 1][y];

        // Create a new head at the end of the centipede
        givingposition[c + 1][11][exists] = true;
        if (left) {
            movecentipede(window, givingposition, centipedeclock, mushroom);

            givingposition[c + 1][11][x] = givingposition[c + 1][10][x] + boxPixelsX;
            givingposition[c + 1][11][y] = givingposition[c + 1][10][y];
        } else {
            movecentipede(window, givingposition, centipedeclock, mushroom);
            givingposition[c + 1][11][x] = givingposition[c + 1][10][x] - boxPixelsX;
            givingposition[c + 1][11][y] = givingposition[c + 1][10][y];
        }

        centipedehead[c + 1][2].setPosition(givingposition[c + 1][11][x], givingposition[c + 1][11][y]);

        // Update the centipede details array for the split
        for (int i = 0; i < 12; i++) {
            centipedetail[c + 1][i][1].setPosition(givingposition[c + 1][k][x], givingposition[c + 1][k][y]);
        }*/
         for(int i=0;i<12;i++){
			   
               centipedetail[c+1][i][1].setPosition(givingposition[c+1][k][x],givingposition[c+1][k][y]);
     //  window.draw(centipedetail[c+1][i][x]);
  }
    }
}

void drawPlayer(sf::RenderWindow &window, float player[], sf::Sprite &playerSprite);
void moveBullets(float bullet[][3], sf::Clock &bulletClock, float mushroom[][3], float hit[][2], float player[], int &hitMushroom);
void drawBullets(sf::RenderWindow &window, float bullets[][3], sf::Sprite bulletSprite[7][2], float hit[24][2], int &hitMushroom);
void drawmushroom(RenderWindow &window, Sprite mushroomSprite[24][2], float mushroom[24][3], float hit[24][2]);

void hitcentipede(RenderWindow &window, bool left, float bullets[][3], float givingposition[][12][3], int maxcentipede,Sprite centipedetail[][12][2] , Sprite centipedehead[][2] ,
float mushroom[12][3] , Clock &centipedeclock) {
    for (int c = 0; c < maxcentipede; c++) {
        for (int i = 0; i < 7; i++) {
            for (int j = 0; j < 12; j++) {
                if (givingposition[c][j][exists] && bullets[i][exists] &&
                    bullets[i][x] >= givingposition[c][j][x] && bullets[i][x] <= givingposition[c][j][x] + boxPixelsX &&
                    bullets[i][y] >= givingposition[c][j][y] && bullets[i][y] <= givingposition[c][j][y] + boxPixelsY) {
                    if (j == 0) {
                        // Head is hit, remove the entire centipede
                        for (int k = 0; k < 12; k++) {
                           // givingposition[c][k][exists] = false;
                            //gameover=true;
                        }
                    } 
					
				
					else {
                        // Call the function to split the body
                        splitCentipedeBody(window , givingposition, c, j, left, centipedetail , centipedehead ,
					centipedeclock, mushroom);
                        
  

                        //	maxcentipede++;
			/*	for (int k = 0; k < 12; k++) {
                            givingposition[maxcentipede ][k][exists] = true;
                            givingposition[maxcentipede ][k][x] = givingposition[maxcentipede-1][k][x] + boxPixelsX;
                            givingposition[maxcentipede ][k][y] =  givingposition[maxcentipede-1][k][y];
                        }*/
                    }
                    bullets[i][exists] = false; // Mark the bullet as hit
                    break; // Exit the loop after removing or splitting the centipede
                }
            }
        }
    }
}


void hitplayer(RenderWindow &window,float givingposition[][12][3],float player[x]){
	
	for(int i=0;i<12;i++)
	{
	

    if (givingposition[i][i][x] >= player[x] && givingposition[i][i][x] <= player[x] + boxPixelsX &&
        givingposition[i][i][y] >= player[y] && givingposition[i][i][y] <= player[y] + boxPixelsY) {
        	
        	
      gameover=true;
        // window.close(); 
		 // Close the window when the centipede hits the player
    }
}

//	player[x]=player[x]+3;
	
//	window.close();

	}
	
	




int main()
{
	

    float hit[24][2] = {};
    int hitMushroom = -1;

    srand(time(0));
    int p = 0;

    sf::RenderWindow window(sf::VideoMode(resolutionX, resolutionY), "Centipede", sf::Style::Close | sf::Style::Titlebar);
    window.setSize(sf::Vector2u(640, 640));
    window.setPosition(sf::Vector2i(100, 0));
    sf::Music bgMusic;
    bgMusic.openFromFile("Music/field_of_hopes.ogg");
    bgMusic.play();
    bgMusic.setVolume(50);

    sf::Texture backgroundTexture;
    sf::Sprite backgroundSprite;
    backgroundTexture.loadFromFile("new.jpg");
    backgroundSprite.setTexture(backgroundTexture);
    backgroundSprite.setColor(sf::Color(255, 255, 255, 455 * 0.20));

    float player[2] = {};
    player[x] = (gameColumns / 2) * boxPixelsX;
    player[y] = (gameColumns * 3 / 4) * boxPixelsY;
    sf::Texture playerTexture;
    sf::Sprite playerSprite;
    playerTexture.loadFromFile("Textures/player.png");
    playerSprite.setTexture(playerTexture);
    playerSprite.setTextureRect(IntRect(0, 0, boxPixelsX, boxPixelsY));

    Texture mushroomTexture;
    Sprite mushroomSprite[24][2];
    mushroomTexture.loadFromFile("Textures/mushroom.png");
    float mushroom[24][3] = {};
    for (int i = 1; i < 24; i++)
    {
        int randomboxX = rand() % 24;
        int randomboxY = rand() % 24;

        if (mushroom[i][x] == player[x] || mushroom[i][y] == player[y])
        {
            continue;
        }
        if (hit[i][2])
            continue;
        mushroom[i][x] = randomboxX * boxPixelsX + (rand() % boxPixelsX);

        mushroom[i][y] = randomboxY * boxPixelsY + rand() % boxPixelsY;
        mushroom[i][exists] = true;
        mushroomSprite[i][x].setTexture(mushroomTexture);
        mushroomSprite[i][x].setTextureRect(IntRect(0, 0, boxPixelsX, boxPixelsY));
        mushroomSprite[i][x].setPosition(mushroom[i][x], mushroom[i][y]);
    }

    Sprite centipedetail[6][12][2];
    Texture centipedetailtexture;
    Texture centipedeTexture;
  //  float positiontail[12][3];
    Sprite centipedehead[3][2];
    float givingposition[12][12][3];
    centipedeTexture.loadFromFile("Textures/head.png");
    centipedetailtexture.loadFromFile("Textures/body.png");
//if(givingposition[0][exists]==true)
//{

    givingposition[0][0][x] = (rand() % 1000) + boxPixelsX;
    givingposition[0][0][y] =(rand() % 1000) + boxPixelsY;
    givingposition[0][0][exists]=true;
    
    centipedehead[0][2].setTextureRect(IntRect(0, 0, boxPixelsX, boxPixelsY));
    centipedehead[0][2].setPosition(givingposition[0][0][x], givingposition[0][0][y]);
    centipedehead[0][2].setTexture(centipedeTexture);
    centipedehead[1][2].setTexture(centipedeTexture);
    centipedehead[2][2].setTexture(centipedeTexture);
    centipedehead[3][2].setTexture(centipedeTexture);
   // centipedehead[1][exists]=true;
for(int c=0;c<maxcentipede;c++){

    for (int i = 0; i <12; i++)
    {
        givingposition[c][i][x] = givingposition[c][i - 1][x] + boxPixelsX;
        givingposition[c][i][y] = givingposition[c][i - 1][y];
        givingposition[c][i][exists]=true;
        
    }}
for(int c=0;c<maxcentipede;c++){

    for (int i = 0; i < 12; i++)
    { for(t=0;t<3;t++){
	
        centipedetail[t][i][1].setTextureRect(IntRect(0, 0, boxPixelsX, boxPixelsY));
        centipedetail[t][i][1].setTexture(centipedetailtexture);}
        centipedetail[t][i][1].setPosition(givingposition[c][i][x], givingposition[c][i][y]);
        //centipedetail[i][exists]=true;
    }}
    Clock centipedeclock;
    sf::Texture bulletTexture;
    sf::Sprite bulletSprite[7][2];
    bulletTexture.loadFromFile("Textures/bullet.png");
    float bullet[7][3] = {};
    for (int i = 1; i < 6; i++)
    {
        bullet[i][x] = player[x];
        bullet[i][y] = player[y] - boxPixelsY;
        bullet[0][exists] = true;
        bulletSprite[i][x].setTexture(bulletTexture);
        bulletSprite[i][x].setTextureRect(sf::IntRect(0, 0, boxPixelsX, boxPixelsY));
    }

    sf::Clock bulletClock;
    while (window.isOpen())
    {
        sf::Event e;
        while (window.pollEvent(e))
        {
            if (e.type == sf::Event::Closed)
            {
                return 0;
            }
        }

        const float speed = 0.9f;
        for (int i = 0; i < 20; i++)
        {
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            {
                if (player[x] + 1 < resolutionX - boxPixelsX)
                    player[x] = player[x] + 1 - speed;
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            {
                if (player[x] - 1 >= 0)
                    player[x] = player[x] - 1 + speed;
            }
        }
        const float speed2 = 0.1f;
        moveBullets(bullet, bulletClock, mushroom, hit, player, hitMushroom);

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            if (player[y] > (resolutionY - boxPixelsY - 95))
            {

                player[y] = player[y] - 1 + speed;
            }
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {

            if ((player[y] - 1) < (resolutionY - boxPixelsY))
            {

                player[y] = player[y] + 1 - speed;
            }
        }
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
        {
            for (int i = 0; i < 7; ++i)
            {
                if (!bullet[i][exists])
                {
                    bullet[i][x] = player[x];
                    bullet[i][y] = player[y] - boxPixelsY;
                    bullet[i][exists] = true;
                    hitMushroom = -1; // Reset hitMushroom before firing a new bullet
                    break;
                }
            }
        }

        window.clear();
for(int c=0;c<maxcentipede;c++){


        for (int i = 0; i <12; i++)
        {
        	if(!headshot){
        	//	for(int t=0;t<12;t++){
				
			
        //	if(givingposition[i][exists]==true){
        		
			
           // for (int j = 1; j <= 2; j++)
           // {
           if(givingposition[c][i][exists]&&(!gameover)){
		   
                centipedetail[c][i][1].setPosition(givingposition[c][i][x], givingposition[c][i][y]);
                   centipedetail[c+1][i][1].setPosition(givingposition[c+1][i][x], givingposition[c+1][i][y]);
                window.draw(centipedehead[0][2]);
                window.draw(centipedetail[c][i][1]);
               //  window.draw(centipedetail[c + 1][i][1]);
              // centipedehead[1][2].setPosition(givingposition[c][11][x] , givingposition[c][11][y]);
                 // window.draw(centipedetail[c + 1][i][t]);
               // window.draw(centipedetail[c+1][i][1]);
              //  window.draw(centipedehead[1][2]);
              
            }
                centipedehead[c][2].setPosition(givingposition[c][0][x], givingposition[c][0][y]);
                
                movecentipede(window, givingposition, centipedeclock , mushroom);
            //  centipedehead[c][]
             
               hitcentipede(window,left,bullet,givingposition,maxcentipede,centipedetail , centipedehead , mushroom  , centipedeclock );
              
               
        }//}
      //  }}
	//	else
	//	centipedetail[i][1]=-1.0f;
	}
		}// window.draw(centipedehead[1][2]);

      

        window.draw(backgroundSprite);
        drawPlayer(window, player, playerSprite);
        drawBullets(window, bullet, bulletSprite, hit, hitMushroom);
        drawmushroom(window, mushroomSprite, mushroom, hit);
hitplayer(window,givingposition,player);
drawline(window,player);
if(gameover){

Font font;
font.loadFromFile("arial.ttf"); // Load the font file
Text gameOver("Game Over! Score: " ,font,30); // Fix the syntax here
gameOver.setPosition(200, resolutionY / 2);
window.draw(gameOver);
}
//splitCentipedeBody( givingposition,  c,  j,  left);
// void hitcentipede(RenderWindow &window,   left,bullet,  givingposition) ;

        window.display();
    }

    return 0;
}

void drawmushroom(RenderWindow &window, Sprite mushroomSprite[24][2], float mushroom[24][3], float hit[24][2])
{
    for (int i = 1; i < 24; i++)
    {
        for (int j = 0; j < 2; j++)
        {

            if (mushroom[i][exists] && hit[i][0] < 2){
			

                window.draw(mushroomSprite[i][j]);
            if (hit[i][0] >= 2){
			
                mushroom[i][exists] = false;
        }
    }}}
}

void drawPlayer(sf::RenderWindow &window, float player[], sf::Sprite &playerSprite)
{
    playerSprite.setPosition(player[x], player[y]);
    window.draw(playerSprite);
}

void moveBullets(float bullet[][3], sf::Clock &bulletClock, float mushroom[][3], float hit[][2], float player[], int &hitMushroom)
{
    if (bulletClock.getElapsedTime().asMilliseconds() < 20)
      // return;

    bulletClock.restart();

    for (int i = 0; i < 7; ++i)
    {
        if (bullet[i][exists])
        {
            bullet[i][y] -= 10;
            if (bullet[i][y] < -32)
            {
                bullet[i][exists] = false;
                hit[i][0] = 0;
            }
//hitMushroom=-1;
            for (int j = 1; j <= 23; ++j)
            {
                if (bullet[i][x] >= mushroom[j][x] && bullet[i][x] <= mushroom[j][x] + boxPixelsX &&
                    bullet[i][y] >= mushroom[j][y] && bullet[i][y] <= mushroom[j][y] + boxPixelsY)
                {
                    bullet[i][exists] = false;
                    hit[j][0]=hit[j][0]+1;

                    if (hit[j][0] >= 2)
                    {
                        mushroom[j][exists] = false;
                        mushroom[j][x] = -1.0f;
                        hitMushroom = 0; // Set the hit mushroom index
                      //  bullet[i][x]=-1.0f;
                      break;
                    }
                }
            }
        }
    }
 //   hitMushroom=-1;
}

void drawBullets(sf::RenderWindow &window, float bullet[7][3], sf::Sprite bulletSprite[7][2], float hit[24][2], int &hitMushroom)
{
    for (int i = 0; i < 7; ++i)
    {
        if (bullet[i][exists] && hitMushroom==-1)
        {
            bulletSprite[i][x].setPosition(bullet[i][x], bullet[i][y]);
            window.draw(bulletSprite[i][x]);

            // Check if the current hitMushroom is the same as the bullet hit
            //if (hitMushroom == i)
           //{
          //     hitMushroom = -1; // Reset hitMushroom after drawing
          //  }
        }
    }
}
content_copyCOPY