Hi guys!
I have tried to program some small things but when I run my application, the window grays out after around 5 seconds, but the application is still running and responding ... does anybody know how to get that fixed?
Thank you a lot guys!
Code:
#include <SFML/Graphics.hpp>
#include "ball.h"
#include <iostream>
#include "paddle.h"
int main()
{
sf::RenderWindow window(sf::VideoMode(1000, 700), "lets olearn");
window.setFramerateLimit(60);
ball ball;
paddle paddle;
while (window.isOpen())
{
window.clear();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
break;
ball.update();
paddle.update();
window.draw(ball.shape);
window.draw(paddle.shape);
window.display();
}
return 0;
}
/*
* ball.hpp
*
* Created on: May 30, 2015
* Author: huynh
*/
#ifndef BALL_HPP_
#define BALL_HPP_
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
/*--------------------------------------- Define class -------------------------------------------------*/
class ball
{
public:
sf::CircleShape shape;
sf::Vector2f velocity;
float x,y;
int d;
void update();
float left();
float right();
float top();
float bot();
ball();
~ball();
};
/*--------------------------------------- Define methods -------------------------------------------------*/
float ball::left()
{
sf::Vector2f cords = shape.getPosition();
cords.x -= shape.getRadius();
return cords.x;
}
float ball::right()
{
sf::Vector2f cords = shape.getPosition();
cords.x += shape.getRadius();
return cords.x;
}
float ball::top()
{
sf::Vector2f cords = shape.getPosition();
cords.y -= shape.getRadius();
return cords.y;
}
float ball::bot()
{
sf::Vector2f cords = shape.getPosition();
cords.y += shape.getRadius();
return cords.y;
}
ball::ball()
{
x= 500;
y= 600;
d= 10;
shape.setFillColor(sf::Color::Blue);
shape.setPosition(x,y);
shape.setRadius(10);
velocity.x = 5;
velocity.y = 5;
}
void ball::update()
{
if( bot() > 700)
velocity.y = -velocity.y;
if( top() < 0)
velocity.y = -velocity.y;
if( right() > 1000)
velocity.x = -velocity.x;
if( left() < 0)
velocity.x = -velocity.x;
shape.move(velocity);
/* if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
shape.setPosition(x-d, y);
x-=d;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
shape.setPosition(x+d, y);
x+=d;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
shape.setPosition(x, y+d);
y+=d;
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
shape.setPosition(x, y-d);
y-=5;
}*/
}
ball::~ball()
{
}
#endif /* BALL_HPP_ */
/*
* paddle.h
*
* Created on: May 31, 2015
* Author: huynh
*/
#ifndef PADDLE_H_
#define PADDLE_H_
//--------------------------------------class definition -------------------------
class paddle
{
public:
sf::RectangleShape shape;
sf::Vector2f velocity;
sf::Vector2f position;
void update();
paddle();
~paddle();
};
//----------------------------------------methods definition--------------------------
paddle::paddle()
{
sf::Vector2f size(25,100);
position.x = 950;
position.y = 350;
shape.setPosition(position);
shape.setFillColor(sf::Color::Red);
shape.setSize(size);
}
void paddle::update()
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
position.y -= 10;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
position.y += 10;
shape.setPosition(position);
}
paddle::~paddle(){};
#endif /* PADDLE_H_ */