Missile Player::fireMissile() { if (!canFireMissile()) { throw string("Impossible de tirer un missile"); } ofPoint center = rect.getCenter(); lastMissileTime = ofGetElapsedTimeMillis(); return Missile(center.x, center.y); }
Alien::Alien(){ _xVel = ALIENVITESSE; _x = SCREEN_WIDTH; _yVel = 5; _y = 0; _vie = 1; _OutFlag = true; for(size_t i = 0; i<ALIENMISSILE_N; ++i) { _MissileList.push_back(Missile(i,1,BULLET_WIDTH,BULLET_HEIGHT)); } }
void play(int i, Window& win) { nodelay(stdscr, TRUE); i = get_difficult(i); Player p(win); EnnemyHorde n = EnnemyHorde(i, "^v^"); Missile miss = Missile(10, "|"); Rocket rock = Rocket(2, "[]"); n.randomPOP(win); int input = 0; p.setScoreZero(); while (input != KEY_ECHAP && p.getHP() > 0) { clear(); miss.print_score(win); win.takeSize(); win.printBorder(); input = getch(); miss.activate(p.getX(), p.getY(), input); rock.activate(p.getX(), p.getY(), input); miss.checkDamages(n.getHorde(), n.getNumber(), win); rock.checkDamages(n.getHorde(), n.getNumber(), win); p.checkEnemies(n.getHorde(), n.getNumber(), win); p.print_life(win); miss.print_chargeur(win); rock.print_chargeur(win); if (p.getHP() > 0) { n.printit(win); miss.move(win); rock.move(win); miss.printit(win); rock.printit(win); p.printit(win); p.inputDirection(input); wrefresh(win.getWin()); p.move(win); p.printit(win); n.move(win); } while(getch() != ERR); usleep(60000); } if (p.getHP() <= 0) { clear(); print_die(win, p); } if (p.getScore() > hight_score) hight_score = p.getScore(); }
void Game::MakeMissiles() { int missileCount = max(level - 1 + rand()%level, 0); if (missileCount > MAX_MISSILES) missileCount = MAX_MISSILES; cout << " Missiles: " << missileCount << endl; missiles.clear(); for (int i = 0; i < missileCount; i++) { Missile::Side side = rand()%2 == 1 ? Missile::SIDE_LEFT : Missile::SIDE_RIGHT; missiles.push_back(Missile(&objgrid, &viewport, side)); } }
Alien::Alien(int id,int y,int w, int h){ _id = id; _xVel = ALIENVITESSE; _yVel = 5; _x = SCREEN_WIDTH; _y = y; _vie = 1; _OutFlag = true; //Initialize le taille _w = w; _h = h; for(size_t i = 0; i<ALIENMISSILE_N; ++i) { _MissileList.push_back(Missile(i,1,BULLET_WIDTH,BULLET_HEIGHT)); } }
//Tries to shoot at a given enemy bool GameLogic::shootAt(char c){ //Did we hit the target? if (target->canShoot(c)) { //Is it dead? //target->hit(5); missiles.push_back(Missile(playerPosition,target)); target->shoot(); if (target->isDead()) { score += KILL_SCORE+(target->getText().size()/2) + waveCount; target = 0; remainingEnemyCount--; } return true; } return false; }
/** Func: makeEnemiesShoot Purpose: Makes the enemies randomly shoot Params - enemies: The list of enemies that change shoot Params - enemyMissiles: The list of missiles that gets populated when they shoot */ void makeEnemiesShoot(std::vector<Enemy> enemies, std::vector<sf::Sprite> &enemyMissiles) { // int numOfEnemies = getAliveEnemies(enemies); for ( unsigned int i = 0; i < enemies.size(); i++ ) { if ( !enemies.at(i).isAlive() ) continue; if ( rand() % 100 == 1 ) { //create a missile sf::Sprite Missile(EnemyMissileSprite); Missile.SetPosition(enemies.at(i).getSprite().GetPosition()); enemyMissiles.push_back(Missile); } } }
void MissileList::addMissile (int x, int y) { if (recharge_ < cooldown) { missiles_.push_back(Missile(x, y)); recharge_ += 200; } }
//////////////////////////////////////////////////////////// /// Entry point of application /// /// \return Application exit code /// //////////////////////////////////////////////////////////// int main() { srand((unsigned)time(0)); bool spaceIsPressed = false; bool finishedEnteringName = false; bool savedToFile = false; sf::Clock clock; float currentTime, deathTime = 0.0f; sf::String whatever; whatever.SetPosition(300, 300); sf::String youMadeIt("You made it to the high score list! Enter your name:"); youMadeIt.SetPosition(300, 275); std::string inputString; initHighScores(highScoreList); sf::String highScoreString(convertScoresToString(highScoreList)); bool gameOver = false; sf::Music backgroundMusic; if ( !backgroundMusic.OpenFromFile("Music/02 - Salt In The Wounds.ogg")) return -9; backgroundMusic.Play(); // Create the main rendering window sf::RenderWindow App(sf::VideoMode(1280, 720, 32), "Space Invaders!"); App.UseVerticalSync(true); // Load the sprite image from a file sf::Image PlayerImage; if (!PlayerImage.LoadFromFile("Images/player.psd")) return EXIT_FAILURE; sf::Sprite Player(PlayerImage); Player.SetPosition(200.f, 100.f); Player.SetCenter(32.f, 32.f); sf::Image BackgroundImage; if ( !BackgroundImage.LoadFromFile("Images/space.psd")) return -1; sf::Sprite Background(BackgroundImage); std::vector<sf::Sprite> missiles; sf::Image MissileImage; if ( !MissileImage.LoadFromFile("Images/Missle.psd")) return -1; std::vector<Enemy> enemies; if ( !EnemyImage.LoadFromFile("Images/Bad Guy!.psd")) return -2; createWave(enemies); std::vector<sf::Sprite> enemyMissiles; if ( !EnemyMissileSprite.LoadFromFile("Images/BadGuyMissile.psd") ) return -3; sf::String help("Use WASD to move and change your direction.\nUse the arrow keys to move without changing your direction.\nPress space to fire."); help.SetSize(16.0f); help.SetColor(sf::Color::Red); help.SetPosition(848, 0); // Start game loop while (App.IsOpened()) { // Process events sf::Event Event; while (App.GetEvent(Event)) { // Close window : exit if (Event.Type == sf::Event::Closed) App.Close(); if (Event.Key.Code == sf::Key::F1) { sf::Image Screen = App.Capture(); Screen.SaveToFile("screenshot.png"); } if ( gameOver && score > getLastScore(highScoreList)) { if (Event.Type == sf::Event::TextEntered) { char c = Event.Text.Unicode; if (c == 8)//8 is backspace { if (inputString.size() > 0)//check that there is something to delete { inputString.erase(inputString.end() - 1);//delete the last character } } else if (c < 128 && c > 31)//pretty much anything you want should be between 31 and 128 { inputString.push_back(c); } } whatever.SetText(inputString); } } // Get elapsed time float ElapsedTime = App.GetFrameTime(); currentTime = clock.GetElapsedTime(); if ( !gameOver ) { // Move the sprite if ((App.GetInput().IsKeyDown(sf::Key::A) || App.GetInput().IsKeyDown(sf::Key::Left)) && Player.GetPosition().x > 32) { Player.Move(-250 * ElapsedTime, 0); } if ((App.GetInput().IsKeyDown(sf::Key::D) || App.GetInput().IsKeyDown(sf::Key::Right)) && Player.GetPosition().x < 300) { Player.Move( 250 * ElapsedTime, 0); } if ((App.GetInput().IsKeyDown(sf::Key::W) || App.GetInput().IsKeyDown(sf::Key::Up)) && Player.GetPosition().y > 32) { Player.Move(0, -250 * ElapsedTime); } if ((App.GetInput().IsKeyDown(sf::Key::S) || App.GetInput().IsKeyDown(sf::Key::Down)) && Player.GetPosition().y < 688) { Player.Move(0, 250 * ElapsedTime); } //rotate the sprite if (App.GetInput().IsKeyDown(sf::Key::A)) { Player.SetRotation(180); } if (App.GetInput().IsKeyDown(sf::Key::D) ) { Player.SetRotation(0); } if (App.GetInput().IsKeyDown(sf::Key::W) ) { Player.SetRotation(90); } if (App.GetInput().IsKeyDown(sf::Key::S) ) { Player.SetRotation(270); } if ( !App.GetInput().IsKeyDown(sf::Key::Space) ) { spaceIsPressed = false; } if ( App.GetInput().IsKeyDown(sf::Key::Space) && !spaceIsPressed) { sf::Sprite Missile(MissileImage); Missile.SetPosition(Player.GetPosition()); Missile.SetRotation(Player.GetRotation()); missiles.push_back(Missile); spaceIsPressed = true; } makeEnemiesShoot(enemies, enemyMissiles); moveMissiles(missiles, ElapsedTime); moveEnemyMissiles(enemyMissiles, ElapsedTime); checkMissiles(missiles); checkMissiles(enemyMissiles); if ( !enemiesInPlace ) moveEnemiesIn(enemies, ElapsedTime); testCollisions(enemies, missiles); if ( checkCollisions(Player, enemyMissiles) ) { // Player has been hit! Clear the screen of missiles, reset his position, and decrease the lives lives--; enemyMissiles.clear(); Player.SetPosition(200.0f, 32.0f); deathTime = clock.GetElapsedTime(); if ( lives < 0 ) gameOver = true; } if ( getAliveEnemies(enemies) == 0 ) createWave(enemies); } else { if ( App.GetInput().IsKeyDown(sf::Key::Escape) ) App.Close(); if ( App.GetInput().IsKeyDown(sf::Key::Return)) finishedEnteringName = true; } std::string sStats; std::stringstream out; out<< "Lives: "; if ( lives < 0) out<< "Dead"; else out<< lives; out << " Score: " << score; sStats = out.str(); sf::String StringStats(sStats); StringStats.SetPosition(10,680); // Clear screen App.Clear(); // Display sprite in our window App.Draw(Background); for ( unsigned int i = 0; i < missiles.size(); i++ ) App.Draw(missiles.at(i)); if ( !gameOver ) App.Draw(Player); for ( unsigned int i = 0; i < enemies.size(); i++) if ( enemies.at(i).isAlive() ) App.Draw(enemies.at(i).getSprite()); for ( unsigned int i = 0; i < enemyMissiles.size(); i++ ) App.Draw(enemyMissiles.at(i)); App.Draw(StringStats); App.Draw(help); if ( currentTime - deathTime < 1.0f && clock.GetElapsedTime() > 1.0f) { sf::String deathInfo("You've been killed!"); deathInfo.SetPosition(360, 0); App.Draw(deathInfo); } if ( gameOver ) { sf::String gameOverString("Game Over! You lose! Press Esc to quit."); std::string scoreMsg; if ( score == 0 ) scoreMsg = "Wow you suck"; else if ( score < 750 ) { scoreMsg = "You're getting there! Better luck next time."; } else { scoreMsg = "You died, but you died after killing a lot of people. Good job!"; } sf::String ScoreMsg(scoreMsg); gameOverString.SetPosition(350, 600); ScoreMsg.SetPosition(350, 625); App.Draw(gameOverString); App.Draw(ScoreMsg); App.Draw(highScoreString); if ( score > getLastScore(highScoreList) && !finishedEnteringName ) { App.Draw(youMadeIt); App.Draw(whatever); } if ( finishedEnteringName && !savedToFile) { if ( score > getLastScore(highScoreList)) { for ( unsigned int i = 0; i < highScoreList.size(); i++ ) { Score temp = highScoreList.at(i); if ( score > temp.Getscore()) { //add the new score before this one Score newScore(inputString, score); highScoreList.insert(highScoreList.begin()+i, newScore); highScoreList.erase(highScoreList.end()-1); break; } } saveScoresToFile(highScoreList); highScoreString.SetText(convertScoresToString(highScoreList)); savedToFile = true; } } } // Display window contents on screen App.Display(); } return EXIT_SUCCESS; }