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

Author Topic: Comments on first game.  (Read 4323 times)

0 Members and 1 Guest are viewing this topic.

Engborg

  • Newbie
  • *
  • Posts: 17
    • View Profile
Comments on first game.
« on: January 07, 2011, 04:48:54 pm »
Hi, I just started with SFML and would like some comments on my first project.

Code: [Select]
#include <SFML/Graphics.hpp>
#include "collision.h"
#include <iostream>

int main()
{
int random = sf::Randomizer::Random(50, 750);
int i, o, t, lvl, u, score, timeover;
double count, speed;
i = 3;
o = 4;
t = 0;
u = 3;
lvl = 5;
score = 0;
count = 1;
speed = -0.3;
timeover = 1000000;

sf::RenderWindow game(sf::VideoMode(1000, 800, 32), "Game Window");

sf::Image land;
if(!land.LoadFromFile("land.png"))
return EXIT_FAILURE;

sf::Sprite bg;
sf::Sprite bg2;

bg.SetImage(land);
bg2.SetImage(land);

bg.SetPosition(0, 0);
bg2.SetPosition(900, 0);

sf::Image Image;
    if (!Image.LoadFromFile("Engborg.png"))
        return EXIT_FAILURE;

sf::Sprite Bild(Image);

    Bild.SetPosition(50.f, 400.f);
Bild.Scale(.3, .3);

sf::Image bildbox;

if (!bildbox.LoadFromFile("box.png"))
return EXIT_FAILURE;

sf::Sprite Box[5];

for(int i = 0; i < 5; i++){
Box[i].SetImage(bildbox);
Box[i].SetPosition(1000.f, random);
Box[i].Scale(.5, .5);
}
sf::Clock Time;

sf::Font MyFont;
MyFont.LoadFromFile("WhiskeyTown.ttf", 50);

    sf::String gameover;
    gameover.SetText("Gameover!");
    gameover.SetFont(MyFont);
    gameover.SetColor(sf::Color(0, 128, 128));
    gameover.SetPosition(1000.f, 1000.f);
    gameover.SetRotation(15.f);
    gameover.SetSize(100.f);

sf::String win;
    win.SetText("You won!");
    win.SetFont(MyFont);
    win.SetColor(sf::Color(0, 128, 128));
    win.SetPosition(1000.f, 1000.f);
    win.SetRotation(15.f);
    win.SetSize(100.f);

while(game.IsOpened())
{

sf::Event Event;
while (game.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                game.Close();

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                game.Close();

            // Resize event : adjust viewport
            if (Event.Type == sf::Event::Resized)
                glViewport(0, 0, Event.Size.Width, Event.Size.Height);
        }

if(Bild.GetPosition().y > 0)
{
if(game.GetInput().IsKeyDown(sf::Key::Up))
Bild.Move(0, -0.3);
}
if(Bild.GetPosition().y < 670)
{
if(game.GetInput().IsKeyDown(sf::Key::Down))
Bild.Move(0, 0.3);
}

if(Collision::PixelPerfectTest(Bild, Box[0]) || Collision::PixelPerfectTest(Bild, Box[1]) ||
Collision::PixelPerfectTest(Bild, Box[2]) || Collision::PixelPerfectTest(Bild, Box[3]) ||
Collision::PixelPerfectTest(Bild, Box[4]))
{
gameover.SetPosition(200.f, 200.f);
i = 0;
o = 0;
timeover = Time.GetElapsedTime();
}
if(Time.GetElapsedTime() > (timeover + 1))
{
speed = 0;
for(int i = 0; i < 5;i++)
{
Box[i].SetPosition(1000, 1000);
}
}

random = sf::Randomizer::Random(100, 700);

if(Time.GetElapsedTime() > i && Time.GetElapsedTime() < o)
{
Box[t].SetPosition(1000, random);
i = i + 1;
o = o + 1;
t++;
if(t > 4)
t = 0;
}

if(Time.GetElapsedTime() > lvl)
{
lvl = lvl + 10;
speed = speed - 0.1;
}

if(floor(Time.GetElapsedTime()) == count)
{
if(i != 0)
{
score++;
std::cout << "You score is: " << score << std::endl;
count++;
}
}

game.Clear(sf::Color(255, 255, 255));

game.Draw(bg);
game.Draw(bg2);
game.Draw(Bild);
game.Draw(gameover);
for(int i = 0; i < 5; i++)
{
game.Draw(Box[i]);
Box[i].Move(speed, 0);
}

bg.Move(speed, 0);
bg2.Move(speed, 0);

if(bg.GetPosition().x < -900)
bg.SetPosition(900, 0);
if(bg2.GetPosition().x < -900)
bg2.SetPosition(900, 0);

game.Display();
}

}


All comments appreciated!

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Comments on first game.
« Reply #1 on: January 07, 2011, 05:52:28 pm »
The code looks good, but writing everything in one big function is usually not the best thing to do. Using more separate functions an/or classes would help to increase the readability dramatically.
To test it i need the collision.h file though.

Engborg

  • Newbie
  • *
  • Posts: 17
    • View Profile
Comments on first game.
« Reply #2 on: January 07, 2011, 06:21:34 pm »
You can find that file here on the SFML website.
I'll remember to use for functions/classes, although i'm not that strong on classes...

Well, thanks alot for your reply!

CodingMadeEasy

  • Newbie
  • *
  • Posts: 38
    • MSN Messenger - petetheheat_represent@msn.com
    • AOL Instant Messenger - 622+Amaretto+Ave
    • Yahoo Instant Messenger - petetheheat_baller@yahoo.com
    • View Profile
    • http://www.youtube.com/CodingMadeEasy
Comments on first game.
« Reply #3 on: January 07, 2011, 10:01:41 pm »
If you aren't that strong in classes then I would advise you to go back to the basics because it doesn't make sense using sfml if you don't know how to use classes because you're going to run into a lot of problems later on

Engborg

  • Newbie
  • *
  • Posts: 17
    • View Profile
Comments on first game.
« Reply #4 on: January 08, 2011, 05:41:05 am »
It's not that I don't know how to use them, just haven't done it that much..

CodingMadeEasy

  • Newbie
  • *
  • Posts: 38
    • MSN Messenger - petetheheat_represent@msn.com
    • AOL Instant Messenger - 622+Amaretto+Ave
    • Yahoo Instant Messenger - petetheheat_baller@yahoo.com
    • View Profile
    • http://www.youtube.com/CodingMadeEasy
Comments on first game.
« Reply #5 on: January 08, 2011, 06:36:40 am »
But you have to have used them a lot to use them effectively that's why you should go and practice with them more because you may think that you've got them down packed but there's a lot to know about classes and you have to know when and how to use certain things to effectively use sfml .

Engborg

  • Newbie
  • *
  • Posts: 17
    • View Profile
Comments on first game.
« Reply #6 on: January 08, 2011, 06:50:03 am »
Okey, thanks for the comment, i will start using classes more often so i learn what when and how to use them properly.

CodingMadeEasy

  • Newbie
  • *
  • Posts: 38
    • MSN Messenger - petetheheat_represent@msn.com
    • AOL Instant Messenger - 622+Amaretto+Ave
    • Yahoo Instant Messenger - petetheheat_baller@yahoo.com
    • View Profile
    • http://www.youtube.com/CodingMadeEasy
Comments on first game.
« Reply #7 on: January 10, 2011, 10:42:55 pm »
np just trying to help :D .. Hope I didn't sound toooo negative

Engborg

  • Newbie
  • *
  • Posts: 17
    • View Profile
Comments on first game.
« Reply #8 on: January 13, 2011, 01:52:43 pm »
That's why i posted it here, wanted feedback, negative feedback is what makes us better. "learn from your misstakes" ^^

thanks again, btw you got any new ideas on a "game" i can start with? kinda dry on ideas...
nothing too complicated though (;

AlexM

  • Newbie
  • *
  • Posts: 18
    • View Profile
Comments on first game.
« Reply #9 on: January 14, 2011, 05:17:44 am »
If you want something simple as practice try emulating arkanoid, tetris, or something like that.

Engborg

  • Newbie
  • *
  • Posts: 17
    • View Profile
Comments on first game.
« Reply #10 on: January 14, 2011, 11:50:32 pm »
Well, thoose example are'nt really in the same experience group as my last project as you can see above.
Dont think i could pull of a project like that..

Appreciate the reply though!

Btw, if someone that reads this have a little time over maby you can rewrite my "game" how it should look, maby with some classes or extra functions.