Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: My 1st project xD  (Read 6019 times)

0 Members and 1 Guest are viewing this topic.

guiguithebest

  • Newbie
  • *
  • Posts: 6
    • View Profile
My 1st project xD
« on: June 11, 2011, 10:59:38 pm »
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  
Code: [Select]
#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;
}

AdventWolf

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
My 1st project xD
« Reply #1 on: June 12, 2011, 01:45:33 am »
I'd try it out but I don't have your images :P.

guiguithebest

  • Newbie
  • *
  • Posts: 6
    • View Profile
My 1st project xD
« Reply #2 on: June 12, 2011, 03:23:33 pm »
Oh sorry I forgot  :lol:

The first image is landed.png , attere in the code;
the second image is spaceship.png, vaisseau in the code;
the third image is stars.png, background in the code.

http://www.myimages.comxa.com/

David

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
My 1st project xD
« Reply #3 on: June 15, 2011, 12:52:07 pm »
Can't you just compile it all together?

guiguithebest

  • Newbie
  • *
  • Posts: 6
    • View Profile
My 1st project xD
« Reply #4 on: June 15, 2011, 09:44:26 pm »
humm how do you do that?  :lol:

Haikarainen

  • Guest
My 1st project xD
« Reply #5 on: July 03, 2011, 05:39:41 am »
Quote from: "guiguithebest"
humm how do you do that?  :lol:


Compile the game so you get an executable(an .exe-file in windows that you can run), then put that exe, with all the dll's you are using,  the sourcecode, and the data (images ), in a zipfile/rarfile, and upload it.

guiguithebest

  • Newbie
  • *
  • Posts: 6
    • View Profile
My 1st project xD
« Reply #6 on: July 04, 2011, 04:29:12 am »
Ok I will thanx ;)

Naufr4g0

  • Full Member
  • ***
  • Posts: 112
    • View Profile
My 1st project xD
« Reply #7 on: September 12, 2011, 12:14:14 pm »
I suggest you to add this symbol with a #define directive:

Code: [Select]

#define PI 3.14159265f


You can also change this code:

Code: [Select]

 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);
        }


with this:

Code: [Select]

if  (spaceship.GetPosition().x < 16.f) spaceship.SetX(16.f);
if  (spaceship.GetPosition().y < 16.f) spaceship.SetY(16.f);
if  (spaceship.GetPosition().x > LARGEURECRAN) spaceship.SetX(LARGEURECRAN);
if  (spaceship.GetPosition().y > HAUTEURECRAN) spaceship.SetY(HAUTEURECRAN);


As you can note, there are two methods to access X and Y coords of a sf::Sprite directly: SetX(), SetY()
Easier, isn't it?

Anyway I'm curious to test your game! ;)

easy

  • Full Member
  • ***
  • Posts: 146
    • MSN Messenger - easy82.contact@gmail.com
    • View Profile
    • Email
My 1st project xD
« Reply #8 on: September 12, 2011, 03:46:30 pm »
You could upload the .zip to SFMLUploads!