So here is my first "real" SFML project. Nothing really incredible :lol: Now I need to figure out how to make the ship shoot stuff
PS The code is half french half english, sorry about that. And the code is probably messy
#include <cstdlib>
#include <SFML/Graphics.hpp>
#include <iostream>
#define HAUTEURECRAN 576
#define LARGEURECRAN 776
using namespace sf;
using namespace std;
int main()
{
RenderWindow app(VideoMode(800, 600, 32), "Jeu 2");
Image vaisseau;
Sprite spaceship;
Image background;
Sprite fond;
Image atterre;
if(!vaisseau.LoadFromFile("spaceship.png"))
{
cout<<"Error while loading image"<<endl;
return EXIT_FAILURE;
}
else
{
vaisseau.CreateMaskFromColor(Color(168, 230, 29));
}
if(!atterre.LoadFromFile("landed.png"))
{
cout<<"Error while loading image"<<endl;
return EXIT_FAILURE;
}
else
{
atterre.CreateMaskFromColor(Color(168, 230, 29));
}
if(!background.LoadFromFile("stars.png"))
{
cout<<"Error while loading image"<<endl;
return EXIT_FAILURE;
}
else
{
fond.SetImage(background);
}
spaceship.SetPosition(400, 300);
spaceship.SetCenter(24, 24);
//---------------------------------------------------------------------------------------------
bool showFlying = false;
//-------------------------------------------------------------------------------------------
// Boucle principale
while (app.IsOpened())
{
Event event;
while (app.GetEvent(event))
{
switch (event.Type) // Type de l'évènement en attente :
{
case Event::Closed : // Croix en haut à droite
app.Close();
break;
case Event::KeyPressed : // Appui sur une touche du clavier
{
switch (event.Key.Code)
{
case Key::Escape : // Appui sur la touche echap
app.Close();
break;
default :
break;
}
}
break;
default :
break;
}
// Fenêtre fermée : on quitte
if (event.Type == Event::Closed)
app.Close();
}
//--------------------------------------------------------------------------------------------
if(app.GetInput().IsKeyDown(Key::Up))
{
spaceship.Move(cos(spaceship.GetRotation()*3.14159265/180)*1, sin(spaceship.GetRotation()*3.14159265/180)*-1);
}
if(app.GetInput().IsKeyDown(Key::Down))
{
spaceship.Move(cos(spaceship.GetRotation()*3.14159265/180)*-1, sin(spaceship.GetRotation()*3.14159265/180)*1);
}
if(app.GetInput().IsKeyDown(Key::Left))
{
spaceship.Rotate(1);
}
if(app.GetInput().IsKeyDown(Key::Right))
{
spaceship.Rotate(-1);
}
if((app.GetInput().IsKeyDown(Key::Up)) || (app.GetInput().IsKeyDown(Key::Down)) || (app.GetInput().IsKeyDown(Key::Left)) || (app.GetInput().IsKeyDown(Key::Right)) )
{
showFlying = true;
}
else
{
showFlying = false;
}
//---------------------------------------------------------------------------------------
Vector2f PosPerso = spaceship.GetPosition();
if(PosPerso.x<16)
{
PosPerso.x=16;
spaceship.SetPosition(PosPerso.x, PosPerso.y);
}
if(PosPerso.y<16)
{
PosPerso.y=16;
spaceship.SetPosition(PosPerso.x, PosPerso.y);
}
if(PosPerso.x>(LARGEURECRAN))
{
PosPerso.x=LARGEURECRAN;
spaceship.SetPosition(PosPerso.x, PosPerso.y);
}
if(PosPerso.y>(HAUTEURECRAN))
{
PosPerso.y=HAUTEURECRAN;
spaceship.SetPosition(PosPerso.x, PosPerso.y);
}
//----------------------------------------------------------------------------
if(showFlying == true)
{
spaceship.SetImage(vaisseau);
}
else
{
spaceship.SetImage(atterre);
}
//----------------------------------------------------------------
// Remplissage de l'écran (couleur noire par défaut)
app.Clear();
app.Draw(fond);
app.Draw(spaceship);
app.Display();
}
return EXIT_SUCCESS;
}