Пример #1
1
int main()
{
    enum GameState
    {
        MainMenu,
        Playing,
        GameOver
    };

    GameState state = GameState::MainMenu;
    VideoMode videoMode(SCREEN_WIDTH, SCREEN_HEIGHT);
    RenderWindow renderWindow(videoMode, "SFML Tetris");
    Event gameEvent;
    Texture backgroundTexture;
    Texture brickPreviewBackground;
    Texture gameOverBackground;
    Texture mainMenuBackground;
    Sprite mainMenuBackgroundSprite;
    Sprite brickPreviewSprite;
    Sprite backgroundSprite;
    Sprite gameOverBackgroundSprite;
    SoundBuffer dropSoundBuffer;
    Sound dropSound;
    Music music;
    Text scoreHeaderText("Score: ");
    Text scoreDisplayText;
    Text levelHeaderText("Level: ");
    Text levelDisplayText;
    Text nextBrickText("Next: ");
    Text gameOverHeaderText("\t  GAME OVER!\nHere are your final stats:");
    Text mainMenuHeaderText("SFML TETRIS!");
    Text mainMenuStartText("Start");
    Text mainMenuQuitText("Quit");
    Text brickCountText[NUMBER_OF_BRICK_TYPES];
    bool gameBoard[GB_HEIGHT][GB_WIDTH];
    unsigned int score = 0;
    unsigned int level = 1;
    unsigned int totalLines = 0;
    unsigned int brickCount[NUMBER_OF_BRICK_TYPES] = {0};
    vector<Brick> brickQueue;
    vector<Brick> brickList;
    Clock brickLockTimer;
    Clock brickFallTimer;
    bool isCheckingLockTime = false;

    backgroundTexture.LoadFromFile("Images/background.png");
    brickPreviewBackground.LoadFromFile("Images/previewBG.png");
    gameOverBackground.LoadFromFile("Images/gameOverBG.png");
    mainMenuBackground.LoadFromFile("Images/mainMenuBG.png");

    brickPreviewSprite.SetTexture(brickPreviewBackground);
    brickPreviewSprite.SetPosition(450, 150);

    backgroundSprite.SetTexture(backgroundTexture);

    gameOverBackgroundSprite.SetTexture(gameOverBackground);
    gameOverBackgroundSprite.SetPosition(110, 82);

    mainMenuBackgroundSprite.SetTexture(mainMenuBackground);

    scoreHeaderText.SetCharacterSize(TEXT_CHAR_SIZE);
    scoreHeaderText.SetPosition(450, 20);

    char* scoreDisplayBuffer = new char;
    scoreDisplayText.SetCharacterSize(TEXT_CHAR_SIZE);
    scoreDisplayText.SetPosition(520, 20);

    levelHeaderText.SetCharacterSize(TEXT_CHAR_SIZE);
    levelHeaderText.SetPosition(450, 60);

    char* levelDisplayBuffer = new char;
    levelDisplayText.SetCharacterSize(TEXT_CHAR_SIZE);
    levelDisplayText.SetPosition(520, 60);

    nextBrickText.SetCharacterSize(TEXT_CHAR_SIZE);
    nextBrickText.SetPosition(450, 120);

    gameOverHeaderText.SetCharacterSize(TEXT_CHAR_SIZE);
    gameOverHeaderText.SetPosition(210, 265);

    mainMenuHeaderText.SetCharacterSize(40);
    mainMenuHeaderText.SetPosition(180, 50);

    mainMenuStartText.SetCharacterSize(32);
    mainMenuStartText.SetPosition(285, 210);

    mainMenuQuitText.SetCharacterSize(32);
    mainMenuQuitText.SetPosition(285, 310);

    char* brickCountTextBuffer[NUMBER_OF_BRICK_TYPES];
    for (int i = 0; i < NUMBER_OF_BRICK_TYPES; i++)
    {
        brickCountTextBuffer[i] = new char;
        brickCountText[i].SetPosition(100, 50 * (i + 1));
    }

    dropSoundBuffer.LoadFromFile("Sounds/drop.wav");
    dropSound.SetBuffer(dropSoundBuffer);

    for (int i = 0; i < GB_HEIGHT; i++)
    {
        for (int j = 0; j < GB_WIDTH; j++)
        {
            gameBoard[i][j] = false;
        }
    }

    RefillBrickQueue(&brickQueue);
    Brick activeBrick = PullBrickFromQueue(&brickQueue);
    Brick previewBrick = PullBrickFromQueue(&brickQueue);
    previewBrick.SetPreviewPosition();

    bool isGameOver = false;

    while (renderWindow.IsOpen())
    {
        switch (state)
        {
        case GameState::MainMenu:
        {
            while (renderWindow.PollEvent(gameEvent))
            {
                if (gameEvent.Type == Event::Closed || gameEvent.Key.Code == Keyboard::Escape)
                {
                    renderWindow.Close();
                }
                else if (gameEvent.Type == Event::MouseButtonPressed)
                {
                    Vector2i mousePos = Mouse::GetPosition(renderWindow);

                    if (mousePos.x >= 220 && mousePos.x <= 420 &&
                            mousePos.y >= 200 && mousePos.y <= 260)
                    {
                        state = GameState::Playing;
                    }
                    else if ((mousePos.x >= 220 && mousePos.x <= 420 &&
                              mousePos.y >= 300 && mousePos.y <= 360) ||
                             gameEvent.Key.Code == Keyboard::Escape)
                    {
                        renderWindow.Close();
                    }
                }
            }

            renderWindow.Draw(mainMenuBackgroundSprite);
            renderWindow.Draw(mainMenuHeaderText);
            renderWindow.Draw(mainMenuStartText);
            renderWindow.Draw(mainMenuQuitText);
            renderWindow.Display();
            break;
        }

        case GameState::GameOver:
        {
            Text gameOverScoreHeaderText = scoreHeaderText;
            gameOverScoreHeaderText.SetPosition(130, 320);
            Text gameOverScoreDisplayText = scoreDisplayText;
            gameOverScoreDisplayText.SetPosition(210, 320);
            Text gameOverLevelHeaderText = levelHeaderText;
            gameOverLevelHeaderText.SetPosition(130, 350);
            Text gameOverLevelDisplayText = levelDisplayText;
            gameOverLevelDisplayText.SetPosition(210, 350);

            while (renderWindow.PollEvent(gameEvent))
            {
                if (gameEvent.Type == Event::Closed || gameEvent.Key.Code == Keyboard::Escape)
                {
                    renderWindow.Close();
                }
            }

            renderWindow.Draw(gameOverBackgroundSprite);
            renderWindow.Draw(gameOverHeaderText);
            renderWindow.Draw(gameOverScoreHeaderText);
            renderWindow.Draw(gameOverScoreDisplayText);
            renderWindow.Draw(gameOverLevelHeaderText);
            renderWindow.Draw(gameOverLevelDisplayText);
            renderWindow.Display();
            break;
        }

        case GameState::Playing:
        {
            if (activeBrick.IsLocked())
            {
                dropSound.Play();
                brickList.push_back(activeBrick);
                brickCount[(int)activeBrick.GetBrickType()]++;
                int linesCleared = 0;

                for (int i = 0; i < SPRITES_IN_BRICK; i++)
                {
                    if (activeBrick.GetSpriteYPosition(i) < BRICK_UPPER_BOUNDRY)
                    {
                        state = GameState::GameOver;
                        break;
                    }
                    else
                    {
                        gameBoard[activeBrick.GetSpriteYPosition(i)][activeBrick.GetSpriteXPosition(i)] = true;
                    }
                }

                if (state == GameState::GameOver)
                {
                    break;
                }

                //Check if there are any full rows that need to be cleared.
                for (int i = BRICK_UPPER_BOUNDRY; i < GB_HEIGHT; i++)
                {
                    for (int j = 0; j < GB_WIDTH; j++)
                    {
                        if (gameBoard[i][j])
                        {
                            if (j == GB_WIDTH - 1)
                            {
                                ClearRow(&brickList, i, gameBoard);
                                linesCleared++;
                                totalLines++;

                                if (totalLines % 8 == 0 && level < LEVEL_MAX)
                                {
                                    level++;
                                }
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                if (linesCleared > 0)
                {
                    if (linesCleared > 1 && linesCleared < 4)
                    {
                        score += (linesCleared * MULTI_LINE_CLEAR_SCORE) + BRICK_LOCK_SCORE;
                    }
                    else if (linesCleared == 4)
                    {
                        score += TETRIS_CLEAR_SCORE + BRICK_LOCK_SCORE;
                    }
                    else
                    {
                        score += SINGLE_LINE_CLEAR_SCORE + BRICK_LOCK_SCORE;
                    }
                }
                else
                {
                    score += BRICK_LOCK_SCORE;
                }

                activeBrick = previewBrick;
                activeBrick.SetActivePosition();
                previewBrick = PullBrickFromQueue(&brickQueue);
                previewBrick.SetPreviewPosition();
            }

            else
            {
                Time fallTimer = brickFallTimer.GetElapsedTime();
                Time lockTimer = brickLockTimer.GetElapsedTime();
                float dropSpeed = 1 - (float)(level * 0.05f);

                if (!IsBottomCollision(activeBrick, gameBoard) && fallTimer.AsSeconds() >= dropSpeed)
                {
                    for (int i = 0; i < SPRITES_IN_BRICK; i++)
                    {
                        activeBrick.SetSpriteYPosition(i, 1);
                        brickFallTimer.Restart();
                        isCheckingLockTime = false;
                    }
                }

                else if (IsBottomCollision(activeBrick, gameBoard))
                {
                    if (!isCheckingLockTime)
                    {
                        isCheckingLockTime = true;
                        brickLockTimer.Restart();
                    }

                    else if (lockTimer.AsSeconds() >= 0.5f)
                    {
                        activeBrick.LockBrick();
                        isCheckingLockTime = false;
                    }
                }

            }

            while (renderWindow.PollEvent(gameEvent))
            {
                if (gameEvent.Type == Event::Closed || gameEvent.Key.Code == Keyboard::Escape)
                {
                    renderWindow.Close();
                }

                if (gameEvent.Type == Event::KeyPressed)
                {
                    CheckInput(&gameEvent, &activeBrick, gameBoard, &score);
                }
            }

            scoreDisplayText.SetString(_itoa(score, scoreDisplayBuffer, 10));
            levelDisplayText.SetString(_itoa(level, levelDisplayBuffer, 10));

            for (int i = 0; i < NUMBER_OF_BRICK_TYPES; i++)
            {
                brickCountText[i].SetString(_itoa(brickCount[i], brickCountTextBuffer[i], 10));
            }

            renderWindow.Clear(Color(0, 255, 255));
            renderWindow.Draw(backgroundSprite);
            renderWindow.Draw(scoreHeaderText);
            renderWindow.Draw(scoreDisplayText);
            renderWindow.Draw(levelHeaderText);
            renderWindow.Draw(levelDisplayText);
            renderWindow.Draw(brickPreviewSprite);
            renderWindow.Draw(nextBrickText);

            for (int i = 0; i < NUMBER_OF_BRICK_TYPES; i++)
            {
                renderWindow.Draw(brickCountText[i]);
            }

            for (int i = 0; i < brickList.size(); i++)
            {
                for (int j = 0; j < SPRITES_IN_BRICK; j++)
                {
                    renderWindow.Draw(brickList[i].GetSpriteArray()[j]);
                }
            }

            for (int i = 0; i < SPRITES_IN_BRICK; i++)
            {
                renderWindow.Draw(activeBrick.GetSpriteArray()[i]);
                renderWindow.Draw(previewBrick.GetSpriteArray()[i]);
            }
            renderWindow.Display();
            break;

        }	//END CASE

        }	//END SWITCH

    } //END GAME LOOP

    return 0;
}
Пример #2
0
bool SoundbufManager::LoadSoundbuf(const std::string& path, const std::string& key)
{
  SoundBuffer buf;
  if (!buf.LoadFromFile(path))
    assert(false);
  return util::Insert(soundbufs_, std::make_pair(key, buf));
}
Пример #3
0
bool AudioPlayer::LoadFromFile(char *szFile)
{
	if (pBuffer)
		delete pBuffer;
	SoundBuffer *Buffer = new SoundBuffer();
	bool result = Buffer->LoadFromFile(szFile);
	this->SetBuffer(Buffer);
	return result;
}
Пример #4
0
static VALUE SoundBuffer_initialize(int argc, VALUE *argv, VALUE vSelf) {
	// Get C++ object pointer from vSelf
	SoundBuffer *pSelf;
	Data_Get_Struct(vSelf, SoundBuffer, pSelf);
	if(argc == 0) {
		// Nothing to initialize
	} else if(argc == 1 && IS(argv[0], g_cSoundBuffer)) {
		*pSelf = *(SoundBuffer *)DATA_PTR(argv[0]);
	} else if(argc == 1 && ISSTR(argv[0])) {
		if(!pSelf->LoadFromFile(STR2CSTR(argv[0])))
			rb_raise(rb_eRuntimeError, "Failed to load specified sound file");
	} else
		rb_raise(rb_eTypeError, "wrong argument type(s)");
	return vSelf;
}
Пример #5
0
static VALUE SoundBuffer_loadFromFile(VALUE vSelf, VALUE vPath) {
	// Get C++ object pointer from vSelf
	SoundBuffer *pSelf;
	Data_Get_Struct(vSelf, SoundBuffer, pSelf);
	return pSelf->LoadFromFile(STR2CSTR(vPath)) ? Qtrue : Qfalse;
}
Пример #6
0
int Game::Run(RenderWindow &win, VideoMode &vMode)
{
	Music m;
	SoundBuffer playerShootBuffer;
	SoundBuffer meteorBuffer;
	meteorBuffer.LoadFromFile("..\\Resources\\meteorexplosion.ogg");
	playerShootBuffer.LoadFromFile("..\\Resources\\playerShoot.ogg");
	Sound playerSound;
	Sound meteorSound;
	meteorSound.SetBuffer(meteorBuffer);
	playerSound.SetBuffer(playerShootBuffer);
	playerSound.SetLoop(false);
	meteorSound.SetLoop(false);
	m.OpenFromFile("..\\Resources\\asteroid.ogg");
	
	m.SetLoop(true);
	m.SetVolume(100);
	PlayerShip ship(player_ship, 2, vMode);
	EnemyShip enemy(enemy_ship, vMode);
	Collision col;
	Event events;
	Image bg1;
	Image bg2;
	Image bg3;
	bg1.LoadFromFile("..\\Resources\\bg.jpg");
	Image stars;
	string abc = "..\\Resources\\star.png";
	stars.LoadFromFile(abc);
	Sprite sp(bg1);
	Sprite sp2(bg1);
	Sprite sp3(bg1);
	Sprite spStar[10];
	Clock clock;
	srand((unsigned)time(0)); 
	for(int i = 0; i<10; i++)
	{
		spStar[i].SetImage(stars);
		//spStar[i].SetPosition
	}
	spStar[0].SetPosition(0,100);
	spStar[1].SetPosition(70,200);
	spStar[2].SetPosition(200,300);
	spStar[3].SetPosition(320,400);
	spStar[4].SetPosition(460,500);
	spStar[5].SetPosition(260,300);
	spStar[6].SetPosition(160,400);
	spStar[7].SetPosition(400,200);
	spStar[8].SetPosition(760,100);
	spStar[9].SetPosition(800,600);
	

	int lowestx, highestx, lowesty, highesty, rangex, rangey, random_integerx, random_integery;
	float time1 = 0;
	float time2 = 0;
	bool start = false;

	



	
	//Sprite spStar1(stars);
	//spStar1.SetPosition(spStar.GetPosition().x, spStar.GetPosition().y - spStar.GetSize().y);
	sp.Resize((float)vMode.Width, (float)vMode.Height);
	sp2.Resize((float)vMode.Width, (float)vMode.Height);
	sp2.SetPosition(sp.GetPosition().x, sp.GetPosition().y - sp.GetSize().y);
	sp3.SetPosition(sp2.GetPosition().x, sp2.GetPosition().y - sp2.GetSize().y);
	//spStar.Resize((float)vMode.Width, (float)vMode.Height);
	bool key = false;
	bool down = false;
	Bullet *bul;
	Meteor* met[METEORCOUNT];
	for(int i = 0; i<METEORCOUNT; i++)
	{
		met[i] = new Meteor(meteor, vMode);
	}
	met[0]->SetPosition(200,-400);
	met[1]->SetPosition(420,-200);
	met[2]->SetPosition(720,-400);
	met[3]->SetPosition(520,-600);
	met[4]->SetPosition(320,-400);
	/*met[5]->SetPosition(620,-400);
	met[6]->SetPosition(100,-400);
*/
	int bulletCount = 0;
	vector<Bullet*> bulletVector;
	vector<Meteor*> meteorVector;
	for(int i = 0; i<METEORCOUNT; i++)
	{
		meteorVector.push_back(met[i]);
	}
	bool fired = false;
	bool pressed = false;
	int count;
	bool change = false;
	//win.UseVerticalSync(true);
	//win.SetFramerateLimit(60);
	int type = pistol;
	Image sunImg;
	sunImg.LoadFromFile("..\\Resources\\Sun.png");
	Sprite sunSp(sunImg);
	sunSp.SetPosition(500,2000);
	//void* user;
	//int score = 0;
//	enemys e(vMode);
	
	//Thread thread(Thread::FuncType Move,void* enemy, void* &win, void* ship);
	m.Play();
	while(win.IsOpened())
	{
		
		while(win.GetEvent(events))
		{
			if(events.Type == Event::Closed)
			{
				win.Close();
				return -1;
			}
				if((events.Type == Event::KeyPressed) && (events.Key.Code == Key::Escape))
				{
					
					return 0;
				}
				if(ship.IsDestroyed() && ((events.Type == Event::KeyPressed) && (events.Key.Code == Key::Return)))
		{
			return 0;
		}
		}

		
		if(win.GetInput().IsKeyDown(Key::Up) && !ship.IsDestroyed())
		{	
			ship.Throttled();
		}
		if(!win.GetInput().IsKeyDown(Key::Up)&& !ship.IsDestroyed())
		{	
			ship.NoThrottle();
			key = false;
		}

		if(!win.GetInput().IsKeyDown(Key::Right) && !win.GetInput().IsKeyDown(Key::Up) && !ship.IsDestroyed())
		{	
			ship.NoThrottle();
		}
		if(!win.GetInput().IsKeyDown(Key::Left)&& !win.GetInput().IsKeyDown(Key::Up) && !ship.IsDestroyed())
		{	
			ship.NoThrottle();
		}
		if(win.GetInput().IsKeyDown(Key::Right) && !ship.IsDestroyed())
		{
			ship.Right();
			if(ship.GetPositionX()<=vMode.Width)
			{
				//ship.SetSpeed(100);
			ship.AddVelocity(300,0);
			ship.MoveSide(win.GetFrameTime());
			}
		}
		if(win.GetInput().IsKeyDown(Key::Left) && !ship.IsDestroyed())
		{
			ship.Left();
			if(ship.GetPositionX()>=0)
			{
			ship.AddVelocity(-300,0);
			//ship.SetSpeed(-100);
			ship.MoveSide(win.GetFrameTime());
			}
		}

		if(win.GetInput().IsKeyDown(Key::LShift))
		{
			ship.TellDestroyed();
			//ship.Destroyed();
			//win.Close();
		}
		if(win.GetInput().IsKeyDown(Key::B))
		{
			
		}
		if(win.GetInput().IsKeyDown(Key::Space) && !ship.IsDestroyed()&&!pressed)
		{
			playerSound.Play();
			bul = new Bullet(bullets, vMode, &ship, type);	
			//bul->ChangeType(type);
			//bul->ChangeType(pistol);
			bulletVector.push_back(bul);
			count = bulletVector.size();
			//cout<<count;

			pressed = true;
		}
		if(!win.GetInput().IsKeyDown(Key::Space))
		{
			pressed = false;
		}
		
		if(win.GetInput().IsKeyDown(Key::N))
		{
			
			type = laser;
			for(int i = 0; i< METEORCOUNT; i++)
			{
				met[i]->SetDestroyed();
			}
		
		}
		win.Clear();
		
		
		

		ship.MoveUp(win.GetFrameTime());
		
		if(enemy.GetPositionY()>ship.GetPositionY()-500)
		{
			enemy.MoveUp(&win, ship);
			//enemy.NavigatorUp(&win, ship);
		}	//ship.Simulate(win.GetFrameTime(), key, down);
		for(int i = 0; i<10; i++)
		{
			if(spStar[i].GetPosition().y>ship.GetPositionY() + 50)
			{
				 
					lowesty=(int)ship.GetPositionY(), highesty=(int)ship.GetPositionY()-600; 
					rangey=(highesty-lowesty)+1; 
					random_integery;
	
					random_integery = lowesty+int(rangey*rand()/(RAND_MAX + 1.0)); 
					//cout << random_integery << endl;

					lowestx=0, highestx=800; 
					rangex=(highestx-lowestx)+1; 
					random_integerx;
	
					random_integerx = lowestx+int(rangex*rand()/(RAND_MAX + 1.0)); 
					//cout << random_integerx << endl;
				 
	
					spStar[i].SetPosition((float)random_integerx, (float)random_integery);
			}
		}

		for(int i = 0; i<METEORCOUNT; i++)
		{
			if(ship.GetPositionY()<=-2000)//Score compare
			{
				met[i]->ChangeSpeed();	
			}
			met[i]->Update(win.GetFrameTime());
			if(met[i]->GetPositionY()>ship.GetPositionY() + 50)
			{
					lowesty=(int)ship.GetPositionY() - 600, highesty=(int)ship.GetPositionY()-1200; 
					rangey=(highesty-lowesty)+1; 
					random_integery;
	
					random_integery = lowesty+int(rangey*rand()/(RAND_MAX + 1.0)); 
					//cout << random_integery << endl;

					lowestx=0, highestx=720; 
					rangex=(highestx-lowestx)+1; 
					random_integerx;
	
					random_integerx = lowestx+int(rangex*rand()/(RAND_MAX + 1.0)); 
				//	cout << random_integerx << endl;
					if(met[i]->IsDestroyed())
					{
						met[i]->SetAlive();
					}
					met[i]->SetPosition(random_integerx, random_integery);
					if(!ship.IsDestroyed())
					{
					ship.IncreaseScore();
					ship.UpdateScore();
					}
			}
			met[i]->UpdateAI();
		}

		

		
		if(enemy.IsFired())
		{
			
		if(col.PixelPerfectTest(enemy.GetBullet(), ship.GetSprite()))
				{
					
					//ship.TellDestroyed();
					ship.DecreaseHealth();
					if(ship.GetHealth()<=0)
					{
						ship.TellDestroyed();
					}
					enemy.BulletReset();
					enemy.NotAttacking();
				}
		}
		if(!meteorVector.empty())
		{
		for(int i = 0; i<METEORCOUNT; i++)
		{
			if(col.CircleTest(enemy.GetBullet(), meteorVector[i]->GetSprite()))
				{
					//win.Close();
					
					//ship.TellDestroyed();
					met[i]->SetDestroyed();
					meteorSound.Play();
					enemy.BulletReset();
				}
			if(col.CircleTest(ship.GetSprite(), meteorVector[i]->GetSprite()))
				{
					//win.Close();
					
					ship.TellDestroyed();
					met[i]->SetDestroyed();
					
				}
			if(col.CircleTest(ship.GetSprite(), sunSp))
				{
					//win.Close();
					
					ship.TellDestroyed();
					//met[i]->SetDestroyed();
				}

			
			if(col.PixelPerfectTest(enemy.GetSprite(), meteorVector[i]->GetSprite()))
				{
					//win.Close();
					
					enemy.DecreaseHealth(10);
					met[i]->SetDestroyed();
					meteorSound.Play();
				}
			if(col.PixelPerfectTest(enemy.GetSprite(), sunSp))
				{
					//win.Close();
					
					enemy.DecreaseHealth(20);
					//met[i]->SetDestroyed();
				}
			//
			if(col.PixelPerfectTest(meteorVector[i]->GetSprite(), enemy.GetNavigator2())||(col.PixelPerfectTest(meteorVector[i]->GetSprite(), enemy.GetNavigator3())||(col.PixelPerfectTest(sunSp, enemy.GetNavigator3()) )))
				{
					//win.Close();
					enemy.MoveLeft(1, &win, ship);
					
				}
			else if(col.PixelPerfectTest(meteorVector[i]->GetSprite(), enemy.GetNavigator1())||(col.PixelPerfectTest(meteorVector[i]->GetSprite(), enemy.GetNavigator4())||(col.PixelPerfectTest(sunSp, enemy.GetNavigator4()) )))
				{
					//win.Close();
					enemy.MoveRight(&win, ship);
					
				}
			
			else
			{
				enemy.Attacking();
				enemy.UpdatePlayerPosition(ship.GetSprite().GetPosition(), &win);
			}

			
		
			
			 
			if(!bulletVector.empty())
			{
			for(int j = 0; j< (int)bulletVector.size(); j++)
			{
				//if((bulletVector[j]->GetSprite().GetPosition().x >= meteorVector[i]->GetSprite().GetPosition().x && bulletVector[j]->GetSprite().GetPosition().x < meteorVector[i]->GetSprite().GetPosition().x +meteorVector[i]->GetSprite().GetSize().x ) && (bulletVector[j]->GetSprite().GetPosition().y >= meteorVector[i]->GetSprite().GetPosition().y && bulletVector[j]->GetSprite().GetPosition().y <  meteorVector[i]->GetSprite().GetPosition().y + meteorVector[i]->GetSprite().GetSize().x))
				//{
				//	//win.Close();
				//	bulletVector[j]->Hit();
				//	meteorVector[i]->SetDestroyed();
				//	delete bulletVector[j];
				//	bulletVector.erase(bulletVector.begin() + j);
				//}
				if(col.PixelPerfectTest(enemy.GetBullet(), ship.GetSprite()))
				{
					//ship.TellDestroyed();
					/*ship.DecreaseHealth();
					if(ship.GetHealth()<=0)
					{
						ship.TellDestroyed();
					}*/
				}
				if(col.PixelPerfectTest(bulletVector[j]->GetSprite(), enemy.GetSprite()))
				{
					enemy.DecreaseHealth();
					meteorSound.Play();
					bulletVector[j]->Hit();
					delete bulletVector[j];
					bulletVector.erase(bulletVector.begin() + j);
				}
				else if(col.PixelPerfectTest(bulletVector[j]->GetSprite(), meteorVector[i]->GetSprite()))
				{
					bulletVector[j]->Hit();
					meteorVector[i]->SetDestroyed();
					meteorSound.Play();
					delete bulletVector[j];
					bulletVector.erase(bulletVector.begin() + j);
				}

				
				//if(col.BoundingBoxTest(bulletVector[j]->GetBulletSprite(), meteorVector[i]->GetSprite()))//collision condition should be checked here bullet and other ships
				//{
				//	cout<<i<<"T";
				//	cout<<j<<"T";
				//	win.Close();
				//	bulletVector[j]->Hit();

				//}
			}
		}
		}
		}
		ship.UpdateAI();
		if(enemy.GetPositionY()>ship.GetPositionY()-700)
		enemy.UpdateAI();
		if(enemy.IsDestroyed())
		{
			lowestx=200, highestx=620; 
			rangex=(highestx-lowestx)+1; 
			random_integerx;
			random_integerx = lowestx+int(rangex*rand()/(RAND_MAX + 1.0)); 
			enemy.EnemyReset(random_integerx, ship);
			if(!ship.IsDestroyed())
			{
			ship.IncreaseScore(10);
			ship.UpdateScore();
			}
		}
		//win.Draw(sp);
		//win.Draw(sp2);
		//win.Draw(sp3);
		ship.UpdateHealth();
		Drawing drawing;
		drawing.win = &win;
		drawing.ship = &ship;
		drawing.enemy = &enemy;
		drawing.Bulletvectors = bulletVector;
		drawing.MeteorCounts = METEORCOUNT;
		for(int i = 0; i<5; i++)
		drawing.met[i] = met[i];
		win.Draw(sunSp);
		for(int i = 0; i< 10; i++)		win.Draw(spStar[i]);
		//win.Draw(spStar1);
		//ship.UpdateScore();
//		DrawingTHread(drawing);
		ship.Draw(&win);
		//score.SetPosition(100,100);
		//win.Draw(score);
		if(enemy.GetPositionY()>ship.GetPositionY()-700)
			enemy.Draw(&win, ship);
		
		for(int i = 0; i<METEORCOUNT; i++)
		{
			
			met[i]->Draw(&win);
		}
		
		if(!bulletVector.empty())
		{
			
		for(int i = 0; i<(int)bulletVector.size();i++)
		{
			//d.Bulletvectors[i]->ChangeType(type);
			bulletVector[i]->Fired();
			bulletVector[i]->Draw(&win, &ship);
			bulletVector[i]->UpdateBullet(win.GetFrameTime());
			
			
			
			bulletVector[i]->UpdateAI();
			//if(d.Bulletvectors[i]->GetPositiony()<=ship.GetPositionY()-500) win.Close();
			if(bulletVector[i]->GetPositiony()<ship.GetPositionY()-600)
			{
				delete bulletVector[i];
				bulletVector.erase(bulletVector.begin()+i);
				//count = d.Bulletvectors.size();
				//cout<<count<<"\n";

			}
		}
		}	
		
		win.SetView(ship.GetView());
		
		
		win.Display();
	}
	return 1;
}
Пример #7
0
 void Weapon::LoadSound(string file, SoundBuffer& buffer, Sound& sound){
     buffer.LoadFromFile(file.c_str());
     sound.SetBuffer(buffer);
 }