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!