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

Author Topic: First SFML game (snakes and apples)  (Read 4305 times)

0 Members and 1 Guest are viewing this topic.

Chocolatesheep

  • Newbie
  • *
  • Posts: 17
    • View Profile
First SFML game (snakes and apples)
« on: January 03, 2012, 01:13:23 pm »
Made it in a day. It's very simple, just a bit over 200 line long. The sourcecode is here. Don't think I did a great job with it, but oh well.
DOWNLOAD LINK: http://www.mediafire.com/?hfa7bcgbuufcxu5

Code: [Select]


#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include "windows.h"
#include <ctime>
using namespace std;

sf::VideoMode VMode(640,480,32);
sf::RenderWindow Screen(VMode,"The Game");

void UpdateScreen()
{
    Screen.Display();
}

int main()
{
    sf::Image Apple;
    if(!Apple.LoadFromFile("Apple.png"))
       return 2;

    sf::Image Background;
    Background.LoadFromFile("Background.png");

    sf::Image Worm;
    Worm.LoadFromFile("worm.png");

    sf::Image Start;
    Start.LoadFromFile("start.png");

    sf::Image Exit;
    Exit.LoadFromFile("exit.png");

    sf::SoundBuffer Buffer;
    Buffer.LoadFromFile("click.ogg");

    sf::Music Music;
    Music.OpenFromFile("music.ogg");

    sf::Sound Sound;
    Sound.SetBuffer(Buffer);

    sf::Clock Clock;
    sf::Clock Timer;

    sf::Sprite Sprite; // apple
    sf::Sprite Sprite2; // background
    sf::Sprite Sprite3; // worm
    sf::Sprite Sprite4; // second worm
    sf::Sprite Sprite5; // third worm
    sf::Sprite ExitSprite;
    sf::Sprite StartSprite;

    Sprite.SetImage(Apple);
    Sprite2.SetImage(Background);
    Sprite3.SetImage(Worm);
    ExitSprite.SetImage(Exit);
    StartSprite.SetImage(Start);

    Sprite3.SetCenter(0,Sprite3.GetSize().y/2);
    Sprite3.SetPosition(640,240);

    Screen.ShowMouseCursor(false);

    Sprite4 = Sprite3;
    Sprite5 = Sprite3;
    Sprite.SetCenter(Sprite.GetSize().x/2, Sprite.GetSize().y/2);

    StartSprite.SetPosition(100,220);
    ExitSprite.SetPosition(100,300);

    Screen.Draw(Sprite2);
    Screen.Draw(Sprite);
    Screen.Draw(Sprite3);
    Screen.Draw(Sprite4);
    Screen.Draw(Sprite5);
    Screen.Draw(StartSprite);
    Screen.Draw(ExitSprite);
    Screen.Display();

    int StartGame = 0;
    while(StartGame == 0) // MENU
    {
        sf::Event GayEvent;
        Sprite.SetPosition(GayEvent.MouseMove.X, GayEvent.MouseMove.Y);
        Screen.Draw(Sprite2);
        Screen.Draw(StartSprite);
        Screen.Draw(ExitSprite);
        Screen.Draw(Sprite);
        Screen.Display();

        while(Screen.GetEvent(GayEvent))
        {
             if(GayEvent.Type == sf::Event::Closed)
                Screen.Close();

/*startgame*/if(Sprite.GetPosition().x > StartSprite.GetPosition().x && Sprite.GetPosition().x < StartSprite.GetPosition().x + StartSprite.GetSize().x, Sprite.GetPosition().y > StartSprite.GetPosition().y && Sprite.GetPosition().y < StartSprite.GetPosition().y + StartSprite.GetSize().y)
              {
                if(GayEvent.Type == sf::Event::MouseButtonPressed)
                {
                 Sound.Play();
                 Sleep(500);
                 StartGame = 2;
                }
              }

              if(Sprite.GetPosition().x > ExitSprite.GetPosition().x && Sprite.GetPosition().x < ExitSprite.GetPosition().x + ExitSprite.GetSize().x, Sprite.GetPosition().y > ExitSprite.GetPosition().y && Sprite.GetPosition().y < ExitSprite.GetPosition().y + ExitSprite.GetSize().y)
              {
                  if(GayEvent.Type == sf::Event::MouseButtonPressed)
                  {
                   Sound.Play();
                   Sleep(500);
                   Screen.Close();
                   return EXIT_SUCCESS;
                  }
              }


            else{/* nothing */ }
        }
    }

    Clock.Reset();
    Timer.Reset();

    Music.Play();
    while(Screen.IsOpened() && StartGame == 2)
    {
        int RandomNumber = sf::Randomizer::Random(20,460);
        sf::Event Naruto;

        Sprite.SetPosition(Naruto.MouseMove.X, Naruto.MouseMove.Y);

        if (Clock.GetElapsedTime() > 0.01f)
        {
            Sprite4.Move(-6,0);
            Sprite5.Move(-15,0);
            Sprite3.Move(-10,0);
            Clock.Reset();

            if(Sprite3.GetPosition().x < -240)
            {
                Sprite3.SetPosition(640, RandomNumber);
            }

            if(Sprite4.GetPosition().x < -240)
            {
                Sprite4.SetPosition(640, RandomNumber);
            }

            if(Sprite5.GetPosition().x < -245)
            {
                Sprite5.SetPosition(640, RandomNumber);
            }
        }

        else{/*Nothing*/}

        Screen.Draw(Sprite2);
        Screen.Draw(Sprite);
        Screen.Draw(Sprite3);
        Screen.Draw(Sprite4);
        Screen.Draw(Sprite5);
        Screen.Display();

        if(Sprite3.GetPosition().x < Sprite.GetPosition().x+15.f && Sprite3.GetPosition().x > Sprite.GetPosition().x-15.f && Sprite3.GetPosition().y > Sprite.GetPosition().y-20.f && Sprite3.GetPosition().y < Sprite.GetPosition().y+20.f)
           {
               int x = Timer.GetElapsedTime();
               cout << "YOU LASTED " << x << " SECONDS\n\n\n\n\n";
               Screen.Close();
               system("pause");
               return 1;
           }

        if(Sprite4.GetPosition().x < Sprite.GetPosition().x+15.f && Sprite4.GetPosition().x > Sprite.GetPosition().x-15.f && Sprite4.GetPosition().y > Sprite.GetPosition().y-20.f && Sprite4.GetPosition().y < Sprite.GetPosition().y+20.f)
           {
               int x = Timer.GetElapsedTime();
               cout << "YOU LASTED " << x << " SECONDS\n\n\n\n\n";
               Screen.Close();
               system("pause");
               return 1;
           }

        if(Sprite5.GetPosition().x < Sprite.GetPosition().x+15.f && Sprite5.GetPosition().x > Sprite.GetPosition().x-15.f && Sprite5.GetPosition().y > Sprite.GetPosition().y-20.f && Sprite5.GetPosition().y < Sprite.GetPosition().y+20.f)
           {
               int x = Timer.GetElapsedTime();
               cout << "YOU LASTED " << x << " SECONDS\n\n\n\n\n\n\n";
               Screen.Close();
               system("pause");
               return 1;
           }


        else{/*Nothing*/}

        while(Screen.GetEvent(Naruto))
        {
            if(Naruto.Type == sf::Event::Closed)
               Screen.Close();

            else{/* nothing */ }
        }


    }//end of game loop


    return 0;
}

replicant

  • Newbie
  • *
  • Posts: 18
    • View Profile
First SFML game (snakes and apples)
« Reply #1 on: January 04, 2012, 03:14:17 am »
I can't play because you used SFML 1.6 but why code empty else statements? you know they aren't compulsory.

 

anything