[ create a new paste ] login | about

Link: http://codepad.org/W7cG5vsx    [ raw code | fork ]

C++, pasted on Sep 4:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <windows.h>
#include <conio.h>

using namespace std;

//=========================================LOAD FONT SETTINGS======================================================
sf::Font loadFont(const string& fontFilename = "resources/Destroyer Aero.ttf")
{
  sf::Font myFont;
  if (!myFont.loadFromFile(fontFilename))
  {
    cout << "Can not load font from " << fontFilename << endl;
    exit(1);
  }
  return myFont;
}

//=========================================LOAD IMAGE SETTINGS======================================================
sf::Texture loadTexture(const string& imageFilename)
{
  sf::Texture myTexture;
  if (!myTexture.loadFromFile(imageFilename))
  {
    cout << "Can not load image from " << imageFilename << endl;
    exit(1);
  }
  return myTexture;
}

//=========================================DEFAULT RECTANGLE SETTINGS======================================================
sf::RectangleShape drawRectangle(float width = 1.0f, float height = 1.0f,
                                 sf::Vector2f position = sf::Vector2f(0,0),
                                 const sf::Color& myFillColor = sf::Color::White,
                                 const sf::Color& myOutlineColor = sf::Color::Black,
                                 float myOutlineThickness = -2)
 {
	sf::RectangleShape myRectangle;
	sf::Vector2f myRectangleSize(width, height);
	myRectangle.setSize(myRectangleSize);
	myRectangle.setPosition(position);
	myRectangle.setFillColor(myFillColor);
	myRectangle.setOutlineColor(myOutlineColor);
	myRectangle.setOutlineThickness(myOutlineThickness);
	return myRectangle;
 }

int main()
{
    //=========================================VARIABLES======================================================
    int const screenWidth = 800;
    int const screenHeight = 600;

    const int gridSize = 10;

    int playerGridX = 32;
	int playerGridY = 64;

	int enemyGridX = 448;
	int enemyGridY = 64;

	int mouseX = 0;
    int mouseY = 0;

    bool play = true;
    bool mouseClicked = false;
    bool mouseInsideRect = false;
    bool draggingShip1 = false ;
    bool draggingShip2 = false ;
    bool draggingShip3 = false ;
    bool draggingShip4 = false ;
    bool draggingShip5 = false ;

    //=========================================DRAW TEXT======================================================
    sf::Font font;
    font.loadFromFile("resources/Destroyed Aero.ttf");

    sf::Text playerText("PLAYER", font);
    playerText.setPosition(32,32);
    playerText.setCharacterSize(32);
    playerText.setStyle(sf::Text::Bold);
    playerText.setColor(sf::Color::Black);

    sf::Text enemyText("ENEMY", font);
    enemyText.setPosition(448,32);
    enemyText.setCharacterSize(32);
    enemyText.setStyle(sf::Text::Bold);
    enemyText.setColor(sf::Color::White);

    //=========================================DRAW PLAYER'S GRID======================================================
    sf::RectangleShape playerGrid[gridSize][gridSize];
    sf::RectangleShape playerGridBackground = drawRectangle(400,600, sf::Vector2f(0,0));
    playerGridBackground.setFillColor(sf::Color(51, 102, 153));
    playerGridBackground.setOutlineColor(sf::Color(51, 102, 153));
    for (int i=0; i<gridSize; i++)
    {
        for (int j=0; j<gridSize; j++)
        {
            playerGrid[i][j] = drawRectangle(32, 32, sf::Vector2f(playerGridX, playerGridY));
            playerGrid[i][j].setFillColor(sf::Color::Black);
            playerGrid[i][j].setOutlineColor(sf::Color(51, 102, 153));
            playerGridX += 32;
        }
        playerGridY += 32;
        playerGridX = 32;
    }

    //=========================================DRAW ENEMY'S GRID======================================================
    sf::RectangleShape enemyGrid[gridSize][gridSize];
    sf::RectangleShape enemyHighlightCell = drawRectangle(32, 32);
    enemyHighlightCell.setFillColor(sf::Color::Black);
    enemyHighlightCell.setOutlineColor(sf::Color::Black);
    sf::RectangleShape enemyGridBackground = drawRectangle(400,600, sf::Vector2f(400,0));
    enemyGridBackground.setFillColor(sf::Color(0, 51, 102));
    enemyGridBackground.setOutlineColor(sf::Color(0, 51, 102));
    for (int i=0; i<gridSize; i++)
    {
        for (int j=0; j<gridSize; j++)
        {
            enemyGrid[i][j] = drawRectangle(32, 32, sf::Vector2f(enemyGridX, enemyGridY));
            enemyGrid[i][j].setFillColor(sf::Color::White);
            enemyGrid[i][j].setOutlineColor(sf::Color(0, 51, 102));
            enemyGridX += 32;
        }
        enemyGridY += 32;
        enemyGridX = 448;
    }

    //=========================================DRAW SHIP'S GRID======================================================
    sf::RectangleShape ship1 = drawRectangle(160, 32, sf::Vector2f(32, 416));
    sf::Texture ship1Texture = loadTexture("resources/ship1.png");
    ship1.setTexture(&ship1Texture);
    ship1.setFillColor(sf::Color(255,255,255));
    ship1.setOutlineColor(sf::Color(0,0,0));

    sf::RectangleShape ship2 = drawRectangle(128, 32, sf::Vector2f(32, 453));
    sf::Texture ship2Texture = loadTexture("resources/ship2.png");
    ship2.setTexture(&ship2Texture);
    ship2.setFillColor(sf::Color(255,255,255));
    ship2.setOutlineColor(sf::Color(0,0,0));

    sf::RectangleShape ship3 = drawRectangle(96, 32, sf::Vector2f(32, 490));
    sf::Texture ship3Texture = loadTexture("resources/ship3.png");
    ship3.setTexture(&ship3Texture);
    ship3.setFillColor(sf::Color(255,255,255));
    ship3.setOutlineColor(sf::Color(0,0,0));

    sf::RectangleShape ship4 = drawRectangle(96, 32, sf::Vector2f(32, 527));
    sf::Texture ship4Texture = loadTexture("resources/ship4.png");
    ship4.setTexture(&ship4Texture);
    ship4.setFillColor(sf::Color(255,255,255));
    ship4.setOutlineColor(sf::Color(0,0,0));

    sf::RectangleShape ship5 = drawRectangle(64, 32, sf::Vector2f(32, 564));
    sf::Texture ship5Texture = loadTexture("resources/ship5.png");
    ship5.setTexture(&ship5Texture);
    ship5.setFillColor(sf::Color(255,255,255));
    ship5.setOutlineColor(sf::Color(0,0,0));

    //=========================================OPEN WINDOW======================================================
    sf::RenderWindow window(sf::VideoMode(screenWidth, screenHeight, 32), "BattleShip");
    sf::Vector2f mouseRectOffset ;

    while(play == true)
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            switch (event.type)
			{
				case sf::Event::Closed:
					window.close();
					break;
            }

            //=========================================LEFT MOUSE BUTTON PRESSED======================================================
            if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
            {
                mouseClicked = true;

                if ( ship1.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y) )
                {
                    draggingShip1 = true ;
                    mouseRectOffset.x = event.mouseButton.x - ship1.getGlobalBounds().left - ship1.getOrigin().x ;
                    mouseRectOffset.y = event.mouseButton.y - ship1.getGlobalBounds().top - ship1.getOrigin().y ;
                }

                 if ( ship2.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y) )
                {
                    draggingShip2 = true ;
                    mouseRectOffset.x = event.mouseButton.x - ship2.getGlobalBounds().left - ship2.getOrigin().x ;
                    mouseRectOffset.y = event.mouseButton.y - ship2.getGlobalBounds().top - ship2.getOrigin().y ;
                }

                 if ( ship3.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y) )
                {
                    draggingShip3 = true ;
                    mouseRectOffset.x = event.mouseButton.x - ship3.getGlobalBounds().left - ship3.getOrigin().x ;
                    mouseRectOffset.y = event.mouseButton.y - ship3.getGlobalBounds().top - ship3.getOrigin().y ;
                }

                 if ( ship4.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y) )
                {
                    draggingShip4 = true ;
                    mouseRectOffset.x = event.mouseButton.x - ship4.getGlobalBounds().left - ship4.getOrigin().x ;
                    mouseRectOffset.y = event.mouseButton.y - ship4.getGlobalBounds().top - ship4.getOrigin().y ;
                }

                 if ( ship5.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y) )
                {
                    draggingShip5 = true ;
                    mouseRectOffset.x = event.mouseButton.x - ship5.getGlobalBounds().left - ship5.getOrigin().x ;
                    mouseRectOffset.y = event.mouseButton.y - ship5.getGlobalBounds().top - ship5.getOrigin().y ;
                }
            }

            //=========================================LEFT MOUSE BUTTON RELEASED======================================================
            if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left)
            {
                mouseClicked = false;
                draggingShip1 = false ;
                draggingShip2 = false ;
                draggingShip3 = false ;
                draggingShip4 = false ;
                draggingShip5 = false ;
            }

            if (event.type == sf::Event::MouseMoved)
            {
                mouseX = event.mouseMove.x;
                mouseY = event.mouseMove.y;
            }

            if (event.type == sf::Event::Closed)
            {
                play = false;
            }
        }

        //=========================================DRAG AND DROP======================================================
        if (draggingShip1 == true)
        {
        ship1.setPosition(mouseX - mouseRectOffset.x, mouseY - mouseRectOffset.y);
        }

        if (draggingShip2 == true)
        {
        ship2.setPosition(mouseX - mouseRectOffset.x, mouseY - mouseRectOffset.y);
        }

        if (draggingShip3 == true)
        {
        ship3.setPosition(mouseX - mouseRectOffset.x, mouseY - mouseRectOffset.y);
        }

        if (draggingShip4 == true)
        {
        ship4.setPosition(mouseX - mouseRectOffset.x, mouseY - mouseRectOffset.y);
        }

        if (draggingShip5== true)
        {
        ship5.setPosition(mouseX - mouseRectOffset.x, mouseY - mouseRectOffset.y);
        }

        //=========================================RENDER======================================================
        window.clear(sf::Color(51, 153, 255));
        window.draw(playerGridBackground);
        window.draw(enemyGridBackground);
        window.draw(playerText);
        window.draw(enemyText);

        sf::Vector2i pointerPos = sf::Mouse::getPosition(window);

        for(int i=0; i < gridSize; i++)
            for (int j = 0; j < gridSize; j++)
            {
                window.draw(playerGrid[i][j]);
                sf::FloatRect gridBounds = playerGrid[i][j].getGlobalBounds();
            }

        for(int i=0; i < gridSize; i++)
            for (int j = 0; j < gridSize; j++)
            {
                window.draw(enemyGrid[i][j]);
                sf::FloatRect gridBounds = enemyGrid[i][j].getGlobalBounds();

                if(gridBounds.contains((sf::Vector2f)pointerPos))
                {
                    enemyHighlightCell.setPosition(enemyGrid[i][j].getPosition());
                    window.draw(enemyHighlightCell);
                }
            }
        window.draw(ship1);
        window.draw(ship2);
        window.draw(ship3);
        window.draw(ship4);
        window.draw(ship5);
        window.display();
    }
}


Create a new paste based on this one


Comments: