//Author: George Roman
//Copyright © 2014 . All rights reserved.
//Built with SFML 2.1
//Graphics
#include <SFML/Graphics.hpp>
//Window
#include <SFML/Window.hpp>
//Input and output
#include <iostream>
//Random numbers
#include <ctime>
//Window input
#include <string>
class Bounce_Game
{
public:
//Class constructor
Bounce_Game();
//Function for running the game
void run();
//SFML declarations
private:
//Window
sf::RenderWindow window;
//Paddle
sf::RectangleShape paddle;
//Ball
sf::CircleShape ball;
//Background texture
sf::Texture* background_Texture;
//Paddle texture
sf::Texture* paddle_Texture;
//Ball texture
sf::Texture* ball_Texture;
//Event
sf::Event event;
//Font
sf::Font font;
//Play button text
sf::Text play_Button_Text;
//Exit button text
sf::Text exit_Button_Text;
//Speed options button text
sf::Text speed_Button_Text;
//Enter speed text
sf::Text enter_Speed_Text;
//Play button
sf::RectangleShape play_Button;
//Exit button
sf::RectangleShape exit_Button;
//Speed option button
sf::RectangleShape speed_Button;
//String for input
sf::Text enter_Speed_Input;
//C++ declarations
private:
//Width of the screen
const unsigned int SCREEN_WIDTH = 600;
//Height of the screen
const unsigned int SCREEN_HEIGHT = 400;
// -- display --
// case 'a': menu is displayed
// case 'b': speed option is displayed
// case 'c': game is displayed
char display;
//Random number for ball x coordinates
unsigned int random_number_1;
//Random number for ball y coordinates
unsigned int random_number_2;
//Speed of the ball
float speed = 1.0f;
//x coordinate for moving the ball
float x = speed;
//y coordinate for moving the ball
float y = speed;
//String for input
std::string speed_String;
//Functions declaration
private:
//Function for processing events
void process_Events();
//Function for rendering
void render();
//Function for moving and updates
void update();
};
//Bounce_Game class constructor
Bounce_Game::Bounce_Game():
window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Bounce 1.0.2"),
paddle(), ball(),
background_Texture(), paddle_Texture(), ball_Texture(),
play_Button_Text("Play", font), exit_Button_Text("Exit", font), speed_Button_Text("Speed options", font), enter_Speed_Text("Enter speed: ", font),
play_Button(), exit_Button(), speed_Button(),
enter_Speed_Input()
{
//Random numbers generator
srand(time(0));
//random_number_1 between 100 and 900
random_number_1 = (rand() % 900) + 100;
//random_number_2 between 100 and 400
random_number_2 = (rand() % 400) + 100;
//Background texture
if(!background_Texture->loadFromFile("background.jpg"))
{
std::cerr<<"Background image couldn't be loaded.";
}
//Paddle texture
if(!paddle_Texture->loadFromFile("paddle.jpg"))
{
std::cerr<<"Paddle image couldn't be loaded.";
}
//Ball texture
if(!ball_Texture->loadFromFile("ball.jpg"))
{
std::cerr<<"Ball image couldn't be loaded.";
}
//Font
if(!font.loadFromFile("arial.ttf"))
{
std::cerr<<"Font couldn't be loaded.";
}
//Paddle starting position
paddle.setPosition(440.0f, 800.0f);
//Paddle size
paddle.setSize(sf::Vector2f(120.0f, 15.0f));
//Paddle texture
paddle.setTexture(paddle_Texture);
//Ball starting position
ball.setPosition(random_number_1, random_number_2);
//Ball radius
ball.setRadius(10.0f);
//Ball texture
ball.setTexture(ball_Texture);
//Menu is displayed
display = 'a';
//Play button position
play_Button.setPosition(420.0f, 450.0f);
//Play button size
play_Button.setSize(sf::Vector2f(160.0f, 50.0f));
//Play button text position
play_Button_Text.setPosition(470.0f, 480.0f);
//Play button text size
play_Button_Text.setCharacterSize(20.0f);
//Exit button position
speed_Button.setPosition(420.0f, 520.0f);
//Exit button size
speed_Button.setSize(sf::Vector2f(160.0f, 50.0f));
//Exit button text position
speed_Button_Text.setPosition(470.0f, 510.0f);
//Exit button text size
speed_Button_Text.setCharacterSize(20.0f);
//Speed option button position
exit_Button.setPosition(420.0f, 580.0f);
//Speed option button size
exit_Button.setSize(sf::Vector2f(160.0f, 50.0f));
//Speed option button text position
exit_Button_Text.setPosition(440.0f, 530.0f);
//Speed option button text size
exit_Button_Text.setCharacterSize(20.0f);
//Enter speed text position
enter_Speed_Text.setPosition(300.0f, 480.0f);
//Enter speed text size
enter_Speed_Text.setCharacterSize(20.0f);
}
//Function for running
void Bounce_Game::run()
{
while(window.isOpen())
{
process_Events();
update();
render();
}
}
//Function for event processing
void Bounce_Game::process_Events()
{
while(window.pollEvent(event))
{
//If mouse was pressed
if(event.type == sf::Event::MouseButtonPressed)
{
if(event.mouseButton.button == sf::Mouse::Left &&
sf::Mouse::getPosition(window).x >= 420.0f &&
sf::Mouse::getPosition(window).x <= 580.0f &&
sf::Mouse::getPosition(window).y >= 450.0f &&
sf::Mouse::getPosition(window).y <= 500.0f)
{
//Play game display
display = 'c';
}
else if(event.mouseButton.button == sf::Mouse::Left &&
sf::Mouse::getPosition(window).x >= 420.0f &&
sf::Mouse::getPosition(window).x <= 580.0f &&
sf::Mouse::getPosition(window).y >= 520.0f &&
sf::Mouse::getPosition(window).y <= 570.0f)
{
//Speed option display
display = 'b';
}
else if(event.mouseButton.button == sf::Mouse::Left &&
sf::Mouse::getPosition(window).x >= 420.0f &&
sf::Mouse::getPosition(window).x <= 580.0f &&
sf::Mouse::getPosition(window).y >= 580.0f &&
sf::Mouse::getPosition(window).y <= 630.0f)
{
//Exit
window.close();
}
}
//If key was pressed
if(event.type == sf::Event::KeyPressed)
{
if(event.key.code == sf::Keyboard::Return &&
display == 'b')
{
display = 'a';
}
}
//Speed input
if(event.type == sf::Event::TextEntered &&
display == 'b')
{
//Handle only ASCII characters
if(event.text.unicode < 128)
{
speed_String += static_cast<char>(event.text.unicode);
}
//Set speed_String to enter_Speed_Input
enter_Speed_Input.setString(speed_String);
}
}
}
//Function for updates
void Bounce_Game::update()
{
switch(display)
{
case 'a':
{
//Set mouse to invisible
window.setMouseCursorVisible(false);
//Set paddle position
paddle.setPosition(sf::Mouse::getPosition(window).x, 800.0f);
//Colision detecting for x (with window borders)
if(ball.getPosition().x <= 0)
{
x = speed;
}
else if(ball.getPosition().x >= SCREEN_WIDTH)
{
x = - speed;
}
//Colision detecting for y (with window borders)
if(ball.getPosition().y <= 0)
{
y = speed;
}
else if(ball.getPosition().y >= SCREEN_HEIGHT)
{
y = - speed;
}
//Colision detecting with paddle
if(ball.getPosition().x >= paddle.getPosition().x &&
ball.getPosition().x <= paddle.getPosition().x - 120.0f &&
ball.getPosition().y <= paddle.getPosition().y + 1.0f &&
ball.getPosition().y >= paddle.getPosition().y - 1.0f)
{
y = - speed;
}
//Moving the ball
ball.move(x, y);
break;
}
case 'b':
{
//Set mouse cursor visible again
window.setMouseCursorVisible(true);
break;
}
}
}
//Function for rendering
void Bounce_Game::render()
{
switch(display)
{
case 'a':
{
//Clear the screen
window.clear();
//Drawing "Play" button
window.draw(play_Button);
window.draw(play_Button_Text);
//Drawing "Speed options" button
window.draw(speed_Button);
window.draw(speed_Button_Text);
//Drawing "Exit" button
window.draw(exit_Button);
window.draw(exit_Button_Text);
//Displaying the window
window.display();
break;
}
case 'b':
{
//Clear the screen
window.clear();
//Drawing "Enter speed: " text
window.draw(enter_Speed_Text);
//Displaying the window
window.display();
break;
}
case 'c':
{
//Closing the window
window.close();
}
}
}
//Main function
int main(int argc, char** argv)
{
//Declaring the game
Bounce_Game game;
//Running the game
game.run();
}