SFML community forums
Help => Graphics => Topic started by: Engborg on January 07, 2011, 04:48:54 pm
-
Hi, I just started with SFML and would like some comments on my first project.
#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!
-
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.
-
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!
-
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
-
It's not that I don't know how to use them, just haven't done it that much..
-
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 .
-
Okey, thanks for the comment, i will start using classes more often so i learn what when and how to use them properly.
-
np just trying to help :D .. Hope I didn't sound toooo negative
-
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 (;
-
If you want something simple as practice try emulating arkanoid, tetris, or something like that.
-
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.