Ticker

6/recent/ticker-posts

Arkanoid Game in C++

Arkanoid Game in C++

Welcome to Programming Chaska! In this post, I will share the program in C++ to make Arkanoid Game. Make sure to Subscribe to Programming Chaska to get the latest updates and Don't forget to share it with friends.


Note- You must have the SFML library installed and included its path to your Programming IDE.

Game Preview:


Arkanoid game
Arkanoid Game


Game Controls:

Left arrow Key - To move the paddle Left 
Right arrow Key - To move the paddle Right

Steps to make Game:

  1. Make a new folder (say, Arkanoid).
  2. In the Arkanoid folder make your .cpp file (say main.cpp) and a folder whose name must be "images".
  3. Download All the images given below and keep them in the "images" folder with their specific name as given below each image.
  4. Copy and Compile the code given below to generate its executable (.exe) file.
  5. At last, Run that .exe file to play the game.

Images Used:

background.jpg
background.jpg


paddle.png
paddle.png


ball.png
ball.png


block01.png
block01.png

block02.png

block03.png

block04.png

block05.png

congrats.jpg

lost.png


Game Code:


#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;

int main()
{
    sf::Vector2f rightBoundary(430.0f440.0f);
    sf::Vector2f leftBoundary(0.0f440.0f);
    int endGame = 0;
    srand(time(0));

    RenderWindow app(VideoMode(520450), "Arkanoid!");
    app.setFramerateLimit(60);

    Texture t1t2t3t4t5t6;
    String s1 = "images/block0" + std::to_string((rand() % 5) + 1) + ".png";
    t1.loadFromFile(s1);
    t2.loadFromFile("images/background.jpg");
    t3.loadFromFile("images/ball.png");
    t4.loadFromFile("images/paddle.png");
    t5.loadFromFile("images/congrats.jpg");
    t6.loadFromFile("images/lost.png");

    Sprite sBackground(t2), sBall(t3), sPaddle(t4), scongrats(t5),slost(t6);

    sPaddle.setPosition(400440); 

    Sprite block[100];

    int n = 0;
    
    for (int i = 1i <= 10i++)
        for (int j = 1j <= 10j++)
        {
            if (i == j || i + j == 11)
                continue;
            block[n].setTexture(t1);
            block[n].setPosition(i * 43j * 20);
            n++;
        }

    float dx = 6dy = 5;
    float x = 300y = 300;

    while (app.isOpen())
    {
        Event e;
        while (app.pollEvent(e))
        {
            if (e.type == Event::Closed)
                app.close();
        }
        if (endGame == n)
        {
            app.clear();
            app.draw(scongrats);
            app.display();
        }
        else if(y >= 440){
            app.clear();
            app.draw(slost);
            app.display();
        }
        else
        {
            x += dx;
            for (int i = 0i < ni++)
                if (FloatRect(x + 3y + 366).intersects(block[i].getGlobalBounds()))
                {
                    block[i].setPosition(-1000);
                    dx = -dx;
                    endGame++;
                }

            y += dy;
            for (int i = 0i < ni++)
                if (FloatRect(x + 3y + 366).intersects(block[i].getGlobalBounds()))
                {
                    block[i].setPosition(-1000);
                    dy = -dy;
                    endGame++;
                }

            if (x < 0 || x > 520)
                dx = -dx;
            if (y < 0 || y > 450)
                dy = -dy;

            if (Keyboard::isKeyPressed(Keyboard::Right) && sPaddle.getPosition() != rightBoundary)
                sPaddle.move(100);
            if (Keyboard::isKeyPressed(Keyboard::Left) && sPaddle.getPosition() != leftBoundary)
                sPaddle.move(-100);

            if (FloatRect(xy1212).intersects(sPaddle.getGlobalBounds()))
                dy = -(rand() % 5 + 2);

            sBall.setPosition(xy);

            app.clear();
            app.draw(sBackground);
            app.draw(sBall);
            app.draw(sPaddle);

            for (int i = 0i < ni++)
                app.draw(block[i]);

            app.display();
        }
    }

    return 0;
}



If you have any doubts/questions related to Program, or if you want to give any suggestionsfeel free to comment below. Subscribe to Programming Chaska for the latest programming updates and Don't forget to share it with friends. 

Post a Comment

0 Comments