void Player::Update(RenderWindow &Window, Player player, int MapFile[10][10]) { playerAnimation.setActive(true); if (Window.GetInput().IsKeyDown(Key::Right)) { currentFrameY = 2; if (CollDroite(MapFile, player, x, y)==false) { x+= moveSpeed * Window.GetFrameTime(); } } else if(Window.GetInput().IsKeyDown(Key::Left)) { currentFrameY = 1; if (CollGauche(MapFile, player, x, y)==false) { x-= moveSpeed * Window.GetFrameTime(); } } else if(Window.GetInput().IsKeyDown(Key::Up)) { currentFrameY = 3; if (CollHaut(MapFile, player, x, y)==false) { y-= moveSpeed * Window.GetFrameTime(); } } else if(Window.GetInput().IsKeyDown(Key::Down)) { currentFrameY = 0; if (CollBas(MapFile, player, x, y)==false) { y+= moveSpeed * Window.GetFrameTime(); } } else playerAnimation.setActive(false); playerAnimation.setPosition(1, x); playerAnimation.setPosition(2, y); playerAnimation.setCurrentFrame(2, currentFrameY); playerAnimation.Update(Window); }
void Animation::update(RenderWindow &app) { //Permet de faire défiler les images if(active) frameCounter += 10*(float)app.GetFrameTime(); else frameCounter += clock.GetElapsedTime(); if(frameCounter >= switchFrame) { frameCounter = 0; clock.Reset(); currentFrameX += getFrameWidth(); } if(currentFrameX >= sprite.GetImage()->GetWidth()) currentFrameX = 0; sprite.SetSubRect(IntRect(currentFrameX, currentFrameY*getFrameHeight(), currentFrameX + getFrameWidth(),currentFrameY*getFrameHeight() + getFrameHeight())); sprite.SetPosition(x,y); }
void Update(RenderWindow & Target) { Last = Current; Current = Target.GetInput().IsKeyDown(KeyCode); Pressed = (Current && !Last); Released = (!Current && Last); Hold = (Current && Last); Untouched = (!Current && !Last); if(Pressed) { Timer = 0.f; ShortHold = true; } else if(Hold && Timer < 0.15f) { Timer += Target.GetFrameTime(); } else if(ShortHold == true) { ShortHold = false; } }
void noyau::run() { //------------------------------------------------------------------------------------------------------ // Ensemble des gestionnaires de sprites/sons/musiques spriteHandler *instSpriteHandler = spriteHandler::getInstance(); soundHandler *instSoundHandler = soundHandler::getInstance(); musicHandler *instMusicHandler = musicHandler::getInstance(); //Instanciation du gestionnaire de la BDD interne bddInternalHandler *instBddInternalHandler = bddInternalHandler::getInstance(); //Gestionnaire des maps mapHandler *instMapHandler = mapHandler::getInstance(); //------------------------------------------------------------------------------------------------------ //Test de musique //instMusicHandler->getMusic("SkinnyLove")->Play(); //Test de son Sound soundActivate; soundActivate.SetBuffer(*instSoundHandler->getSound("activated")); soundActivate.Play(); //------------------------------------------------------------------------------------------------------ /* //Test d'affichage d'un sprite Sprite spriteIn; spriteIn.SetImage(*instSpriteHandler->getImage("scientifiqueFront")); spriteIn.SetPosition(200, 100); spriteIn.SetScale(2, 2); */ animation firstAnim = animation("1",true); //firstAnim.play(); firstAnim.stop(); // Start game loop while (fenetrePrincipal.IsOpened()) { // Process events Event Event; while (fenetrePrincipal.GetEvent(Event)) { // Close window : exit if (Event.Type == Event::Closed || fenetrePrincipal.GetInput().IsKeyDown(Key::Escape)) fenetrePrincipal.Close(); } // Get elapsed time float ElapsedTime = fenetrePrincipal.GetFrameTime(); if (Event.Key.Code == Key::F12) { fenetrePrincipal.Capture().SaveToFile("Screenshot\\screenshot1.jpg"); cout<<"screen effectué"<<endl; } // Move the sprite if (fenetrePrincipal.GetInput().IsKeyDown(sf::Key::Left)) firstAnim.move(-100 * ElapsedTime, 0); if (fenetrePrincipal.GetInput().IsKeyDown(sf::Key::Right)) firstAnim.move( 100 * ElapsedTime, 0); if (fenetrePrincipal.GetInput().IsKeyDown(sf::Key::Up)) firstAnim.move(0, -100 * ElapsedTime); if (fenetrePrincipal.GetInput().IsKeyDown(sf::Key::Down)) { firstAnim.move(0, 100 * ElapsedTime); firstAnim.play(); } // Rotate the sprite //if (fenetrePrincipal.GetInput().IsKeyDown(sf::Key::Add)) Sprite.Rotate(- 100 * ElapsedTime); /*if (fenetrePrincipal.GetInput().IsKeyDown(sf::Key::R)) firstAnim.Rotate(+ 100 * ElapsedTime);*/ if (fenetrePrincipal.GetInput().IsKeyDown(sf::Key::T)) cout<<"Phase : "<<timerC.GetElapsedTime()<<endl; // Clear screen fenetrePrincipal.Clear(); // Display sprite of the map in our window instMapHandler->afficherAll(fenetrePrincipal); //few test on the Animation classe firstAnim.afficher(fenetrePrincipal); board titi = board(); titi.init(300,300,330,330,sf::Color(255,255,255),"coucou",30,sf::Color(0,0,0),3,sf::Color(0,0,0)); titi.affiche(fenetrePrincipal); /* if(timerC.GetElapsedTime()>=10) { if(timerC.GetElapsedTime()<=20) firstAnim.stop(); else firstAnim.play(); } */ /* */ /* sf::Shape Line = sf::Shape::Line(30,30,60,60,3,sf::Color(255,255,255)); sf::Shape Circle = sf::Shape::Circle(90,90,20,sf::Color(255,255,255),1,sf::Color(0,0,0)); sf::Shape Rect = sf::Shape::Rectangle(200,200,220,220,sf::Color(255,255,255)); fenetrePrincipal.Draw(Line); fenetrePrincipal.Draw(Circle); fenetrePrincipal.Draw(Rect); */ //fenetrePrincipal.Draw(spriteIn); // Display window contents on screen fenetrePrincipal.Display(); //fenetrePrincipal.SetView(View); firstAnim.stop(); } }
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; }
static VALUE RenderWindow_get_frameTime(VALUE vSelf) { // Get C++ object pointer from vSelf RenderWindow *pSelf; Data_Get_Struct(vSelf, RenderWindow, pSelf); return rb_float_new(pSelf->GetFrameTime()); }