#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cstdlib>
int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 800, 32), "SFML Graphics");
// Music
sf::Music *bgsound = new sf::Music();
bgsound->OpenFromFile("track.ogg");
App.UseVerticalSync(true);
// GameScore
unsigned short int score = 0;
unsigned short int life = 3;
// Game Bar
sf::Shape bar = sf::Shape::Rectangle(0, 0, 100, 10, sf::Color(255, 255, 255) );
bar.Move(360, 760);
// Game Ball
sf::Shape ball = sf::Shape::Circle(0, 0, 10, sf::Color(255, 255, 255));
ball.Move(390, 600);
float ballOffsetX = 5, ballOffsetY = -9;
// Start Sound
bgsound->SetLoop(true);
bgsound->Play();
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
{
bgsound->Stop();
delete bgsound;
App.Close();
}
}
// Get elapsed time
float ElapsedTime = App.GetFrameTime();
// Move the sprite
if (App.GetInput().IsKeyDown(sf::Key::Left) && (bar.GetPosition().x > 10))
{
bar.Move(-400 * ElapsedTime, 0);
}
if (App.GetInput().IsKeyDown(sf::Key::Right) && (bar.GetPosition().x < 690))
{
bar.Move( 400 * ElapsedTime, 0);
}
// Collision Detection
// bar Collision
if ((ball.GetPosition().x >= bar.GetPosition().x) && (ball.GetPosition().x <= (bar.GetPosition().x + 100)))
{
if ((ball.GetPosition().y >= bar.GetPosition().y) && (ball.GetPosition().y <= (bar.GetPosition().y + 10)))
{
ballOffsetY = ballOffsetY * (-1.f);
score++;
}
}
if ((ball.GetPosition().x >= 790) || ball.GetPosition().x <= 10)
{
ballOffsetX = ballOffsetX * (-1.f);
}
if (ball.GetPosition().y <= 10)
{
ballOffsetY = ballOffsetY * (-1.f);
}
// DEBUG ONLY
if (ball.GetPosition().y > 800)
{
ball.Move(0,-790);
life--;
}
if (life <= 0)
{
break;
}
// Ball Movement
ball.Move(ballOffsetX, ballOffsetY);
// Clear screen
App.Clear();
// Display sprite in our window
App.Draw(bar);
App.Draw(ball);
// Display window contents on screen
App.Display();
}
bgsound->Stop();
const char* x = "teststring";
sf::String scr1("Your Score:", sf::Font::GetDefaultFont(), 50);
sf::String scr2(x, sf::Font::GetDefaultFont(), 60);
scr1.SetColor(sf::Color(255,255,255));
scr2.SetColor(sf::Color(255,255,255));
scr1.Move(300, 400);
scr2.Move(300, 500);
while (1)
{
App.Draw(scr1);
App.Draw(scr2);
App.Display();
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
{
bgsound->SetLoop(false);
bgsound->Stop();
delete bgsound;
App.Close();
}
}
}
bgsound->Stop();
delete bgsound;
return EXIT_SUCCESS;
}