i think its easier when i let u see the code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace sf;
using namespace std;
////////////////////////////////////////////////////////////
/// Entry point of application
////////////////////////////////////////////////////////////
class ball: public Shape
{
public:
Vector2f dir;
float speed;
int width;
int height;
ball();
void increase_speed();
void bounce_y();
void bounce_x();
};
ball::ball()
{
AddPoint(0,0);
AddPoint(10,0);
AddPoint(10,10);
AddPoint(0,10);
width = height = 10;
dir.x = dir.y = -1;
speed = 50;
}
void ball::increase_speed()
{
speed = speed +50;
}
void ball::bounce_y()
{
dir.y = -dir.y;
}
void ball::bounce_x()
{
dir.x = -dir.x;
}
class bat : public sf::Shape
{
public:
bat();
bool exist;
int width;
int height;
};
bat::bat()
{
AddPoint(0, 0, sf::Color(255, 0, 0), sf::Color(0, 128, 128));
AddPoint(50, 0, sf::Color(255, 85, 85), sf::Color(0, 128, 128));
AddPoint(50, 50, sf::Color(255, 170, 170), sf::Color(0, 128, 128));
AddPoint(0, 50, sf::Color(255, 170, 170), sf::Color(0, 128, 128));
SetColor(Color(255, 255, 255, 255));
Scale(1, 3);
SetPosition(0,0);
width = 50;
height = 150;
exist = 1;
}
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
int main()
{
// Create main window
RenderWindow App;
App.Create(sf::VideoMode(WINDOW_WIDTH,WINDOW_HEIGHT,32), "SFML Window", Style::Fullscreen);
// Cursor is not being shown
ShowCursor(0);
// create bats
bat left;
bat right;
right.SetPosition(WINDOW_WIDTH - right.width,0);
// INput
bool left_up ;
bool left_down ;
bool right_up ;
bool right_down;
// Ball
ball Ball;
Ball.SetPosition((WINDOW_WIDTH - Ball.width)/2, (WINDOW_HEIGHT - Ball.height)/2);
// goals
int goal1;
int goal2;
goal1 = goal2 = 0;
// Uhr
Clock Uhr;
// im spiel
bool gameover;
bool goal;
gameover = goal = 0;
// TExte
String Text;
Text.SetFont(sf::Font::GetDefaultFont());
Text.SetSize(20);
Text.SetPosition(350,0);
std::ostringstream out;
out << setprecision(2) <<fixed;
std::string str;
while (App.IsOpened())
{
// Process events
Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == Event.Closed)
App.Close();
// keybord-events
if ((Event.Type == Event.KeyPressed) && (Event.Key.Code == Key::Escape))
{
App.Close();
}
if ((Event.Type == Event.KeyPressed) && (Event.Key.Code == Key::F1))
{
right.SetPosition(WINDOW_WIDTH - right.width,0);
left.SetPosition(0,0);
Ball.SetPosition((WINDOW_WIDTH - Ball.width)/2, (WINDOW_HEIGHT - Ball.height)/2);
Ball.speed = 50;
goal = 0;
goal1 = goal2 = 0;
gameover = 0;
}
}
out << goal1 << " : " << goal2;
str = out.str();
out.seekp(0);
Text.SetText(str);
App.Draw(Text);
// handle bat-movements
const Input& Input = App.GetInput();
left_up = Input.IsKeyDown(Key::W);
left_down = Input.IsKeyDown(Key::S);
right_up = Input.IsKeyDown(Key::Up);
right_down = Input.IsKeyDown(Key::Down);
if( right_down)
{
right.Move(0, 400* App.GetFrameTime());
}
if(right_up)
{
right.Move(0, - 400 * App.GetFrameTime());
}
if(left_down)
{
left.Move(0, 400* App.GetFrameTime());
}
if(left_up)
{
left.Move(0, -400* App.GetFrameTime());
}
if(Uhr.GetElapsedTime() > 5.0)
{
Ball.increase_speed();
Uhr.Reset();
}
// tor
if( Ball.GetPosition().x < 0 && gameover == 0)
{
goal2 = goal2 +1;
goal = 1;
}
if( Ball.GetPosition().x > WINDOW_WIDTH && gameover == 0)
{
goal1 = goal1+1;
goal = 1;
}
if( goal1 >4 || goal2 >4)
gameover =1 ;
if(!gameover)
{
if(goal)
{
Ball.SetPosition((WINDOW_WIDTH - Ball.width)/2, (WINDOW_HEIGHT - Ball.height)/2);
Ball.speed = 50;
goal = 0;
}
// check bounce
if( Ball.GetPosition().y <= 0 || Ball.GetPosition().y +10 >= WINDOW_HEIGHT)
Ball.bounce_y();
if((Ball.GetPosition().x <= left.width) && Ball.GetPosition().y +10 >= left.GetPosition().y && Ball.GetPosition().y <= left.GetPosition().y + left.height)
Ball.bounce_x();
if((Ball.GetPosition().x +10 >= WINDOW_WIDTH - right.width) && Ball.GetPosition().y +10 >= right.GetPosition().y && Ball.GetPosition().y <= right.GetPosition().y + right.height)
Ball.bounce_x();
}
out << goal1 << " : " << goal2;
str = out.str();
out.seekp(0);
Text.SetText(str);
App.Draw(Text);
// move Ball
Ball.Move(Ball.speed * Ball.dir * App.GetFrameTime());
// Draw
App.Draw(left);
App.Draw(right);
App.Draw(Ball);
// Finally, display the rendered frame on screen
App.Display();
}
return 0;
}