Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a working version of an Arkanoid game. (Win32 program) Steps: Create sing

ID: 3747579 • Letter: C

Question

Create a working version of an Arkanoid game. (Win32 program)

Steps:

Create single horizontal paddle and a ball, and walls. Make the ball move and bounce from all walls and the padde. Ball should start of in a random direction (srand(time(0)) initializes pseudo-random value generator and rand() % 10 function returns random value between 0 and 10).

Make paddle’s horizontal movement controlled by a player’s mouse.

Make a wall of bricks of different colors (use a thick line).

Bricks should be destroyed, when hit by the ball. They should also bounce the ball.

When the ball hits the bottom wall (it was not caught by the player’s paddle), the game restarts and you can continue playing it.

Explanation / Answer

Please find all the C++ and Header Files related to the Arkanoid Game:-

1. Ball.cpp

#include "Ball.hpp"
Ball::Ball(float radius, const sf::Vector2f & position, const sf::Color & color, float speed, float angle) {
circle.setRadius(radius);
circle.setPosition(position);
circle.setFillColor(color);
circle.setOrigin(radius, radius);

this->speed = speed;
setAngle(angle);
}

void Ball::setAngle(float angle) {
velocity.x = speed * std::cos(angle * M_PI / 180);
velocity.y = -speed * std::sin(angle * M_PI / 180);
}
float Ball::getAngle() {
// angle in range [-180; 180]
float angle = std::atan2(-velocity.y, velocity.x) * 180 / M_PI;
// angle in range [0; 360]
if (angle < 0.f)
angle += 360.f;
return angle;
}

bool Ball::checkColission(const Block & block) {
if ( (getX() >= block.left() && getX() <= block.rigth()) ||
(getY() >= block.top() && getY() <= block.bottom()) ) {
if (left() < block.rigth() && rigth() > block.left() && top() < block.bottom() && bottom() > block.top()) {
if (getX() < block.left() || getX() > block.rigth()) {
velocity.x *= -1;
} else {
velocity.y *= -1;
}
return true;
}
} else {
auto distance = [](sf::Vector2f x1, sf::Vector2f x2)->float { return std::sqrt( pow(x1.x - x2.x, 2) + pow(x1.y - x2.y, 2) ); };

sf::Vector2f corner;

if ( distance(getPosition(), sf::Vector2f(block.left(), block.top()) ) <= getRadius() )
corner = sf::Vector2f(block.left(), block.top());
else if ( distance(getPosition(), sf::Vector2f(block.left(), block.bottom())) <= getRadius() )
corner = sf::Vector2f(block.left(), block.bottom());
else if ( distance(getPosition(), sf::Vector2f(block.rigth(), block.top()) ) <= getRadius() )
corner = sf::Vector2f(block.rigth(), block.top());
else if ( distance(getPosition(), sf::Vector2f(block.rigth(), block.bottom())) <= getRadius() )
corner = sf::Vector2f(block.rigth(), block.bottom());
else
return false;

float a = (-getY() + corner.y) / (getX() - corner.x);
a = -1 / a;
float alpha = std::atan(a) * 180 / M_PI;
if (alpha < 0)
alpha += 180;
float beta = getAngle();

// angle in range [0; 360]
float angle = 2 * alpha - beta;
// angle in range [-180; 180]
if (angle > 180.f)
angle -= 360.f;
else if (angle <= -180.f)
angle += 360.f;

if (angle <= 0.f && angle > -8.f)
angle = -8.f;
else if (angle > 0.f && angle < 8.f)
angle = 8.f;

setAngle(angle);

return true;
}

return false;
}

bool Ball::checkColission(const Paddle & paddle) {
if (left() < paddle.rigth() && rigth() > paddle.left() && top() < paddle.bottom() && bottom() >= paddle.top()) {
/*float minAngle = 120.f, maxAngle = 60.f;
float percantage = getX() - paddle.left() / paddle.getSize().x;
float angle = minAngle - (minAngle - maxAngle) * percantage;
setAngle(angle);*/

float deviation = 50.f;
bool leftSide = getX() < paddle.getPosition().x;
float distanceFromCenter = std::abs(getX() - paddle.getPosition().x);
float percantage = distanceFromCenter / (paddle.getSize().x / 2.f);
float angle = 90.f - percantage * deviation * (leftSide ? -1.f : 1.f);
setAngle(angle);

return true;
}
return false;
}

void Ball::Update(float deltaTime) {
circle.move(velocity * deltaTime);
if (left() <= 0.f)
velocity.x = -velocity.x;
if (rigth() >= GlobalObjects::windowWidth)
velocity.x = -velocity.x;
if (top() <= 0.f)
velocity.y = -velocity.y;
}

void Ball::Draw(sf::RenderWindow & window) {
window.draw(circle);
}

2. Ball.hpp

#ifndef BALL_HPP_
#define BALL_HPP_

#include <SFML/Graphics.hpp>
#include "Block.hpp"
#include "GlobalObjects.hpp"
#include "Paddle.hpp"

class Ball {
private:
sf::CircleShape circle;
float speed;
sf::Vector2f velocity;

void setAngle(float ang);
float getAngle();

public:
Ball(float radius, const sf::Vector2f & position, const sf::Color & color, float speed, float angle);

bool checkColission(const Block & block);
bool checkColission(const Paddle & paddle);
bool exist() {
return bottom() < GlobalObjects::windowHeight;
}

void Update(float deltaTime);

void Draw(sf::RenderWindow & window);

float left() const {
return circle.getPosition().x - circle.getRadius();
}
float rigth() const {
return circle.getPosition().x + circle.getRadius();
}
float top() const {
return circle.getPosition().y - circle.getRadius();
}
float bottom() const {
return circle.getPosition().y + circle.getRadius();
}

float getX() const {
return circle.getPosition().x;
}
float getY() const {
return circle.getPosition().y;
}
sf::Vector2f getPosition() const {
return circle.getPosition();
}
float getRadius() const {
return circle.getRadius();
}
sf::Vector2f getVelocity() const {
return velocity;
}
};

#endif // BALL_HPP_

3. Block.cpp

#include "Block.hpp"

Block::Block(const sf::Vector2f & size, const sf::Vector2f & position, const sf::Color & color) {
rect.setSize(size);
rect.setPosition(position);
rect.setFillColor(color);
}

void Block::Draw(sf::RenderWindow & window) {
window.draw(rect);
}

4. Block.hpp

#ifndef BLOCK_HPP_
#define BLOCK_HPP_

#include <cmath>
#define M_PI 3.14159265358979323846

#include <SFML/Graphics.hpp>

class Block
{
private:
sf::RectangleShape rect;

public:
Block(const sf::Vector2f & size, const sf::Vector2f & position, const sf::Color & color);

void Draw(sf::RenderWindow & window);

float left() const { return rect.getPosition().x; }
float rigth() const { return rect.getPosition().x + rect.getSize().x; }
float top() const { return rect.getPosition().y; }
float bottom() const { return rect.getPosition().y + rect.getSize().y; }
};

#endif // BLOCK_HPP_

5. BlocksField.cpp

#include <iostream>
#include "BlocksField.hpp"

BlocksField::BlocksField(const sf::Vector2f & size, const sf::Vector2f & position, const sf::Color & color, int columns, int rows) {
sf::Vector2f blockSize(size.x / columns, size.y / rows);

for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
blocks.push_back(Block(blockSize - sf::Vector2f(4.f, 4.f), position + sf::Vector2f(blockSize.x * i + 2.f, blockSize.y * j + 2.f), color));
}
}
}

void BlocksField::Update(Ball & ball) {
blocks.remove_if([&ball, this](const Block & block)->bool { return ball.checkColission(block); });
}

void BlocksField::Draw(sf::RenderWindow & window) {
for (auto &block : blocks) {
block.Draw(window);
}
}

6. BlocksField.hpp

#ifndef BLOCKSFIELD_HPP_
#define BLOCKSFIELD_HPP_

#include <list>
#include "Ball.hpp"

class BlocksField
{
private:
std::list<Block> blocks;

public:
BlocksField(const sf::Vector2f & size, const sf::Vector2f & position, const sf::Color & color, int columns, int rows);

void Update(Ball & ball);

void Draw(sf::RenderWindow & window);
};

#endif // BLOCKSFIELD_HPP_

7. Game.cpp

#include "Game.hpp"
#include <iostream>

Ball * Game::ball = nullptr;
Paddle * Game::paddle = nullptr;

bool Game::Update(float deltaTime, BlocksField & blocksField)
{
if (paddle != nullptr)
paddle->Update(deltaTime);

if (ball != nullptr && ball->exist())
{
ball->Update(deltaTime);
ball->checkColission(*paddle);
blocksField.Update(*ball);
return true;
}
else
{
delete ball;
ball = nullptr;
return false;
}
}

void Game::Draw(sf::RenderWindow & window)
{
if (ball != nullptr)
ball->Draw(window);
if (paddle != nullptr)
paddle->Draw(window);
}

8. Game.hpp

#ifndef GAME_HPP_
#define GAME_HPP_

#include <SFML/Graphics.hpp>
#include "BlocksField.hpp"

class Game
{
private:
static Ball * ball;
static Paddle * paddle;
Game() = delete;

public:
static void createBall(const Ball & bll) { ball = new Ball(bll); }
static void createPaddle(const Paddle & padd) { paddle = new Paddle(padd); }
static bool Update(float deltaTime, BlocksField & blocksField);
static void Draw(sf::RenderWindow & window);
};

#endif // GAME_HPP_

9. GlobalObjects.cpp

#include "GlobalObjects.hpp"

const float GlobalObjects::windowWidth = 512.f;
const float GlobalObjects::windowHeight = 512.f;
sf::RenderWindow GlobalObjects::window(sf::VideoMode(windowWidth, windowHeight), "Arkanoid", sf::Style::Close | sf::Style::Titlebar);

10. GlobalObjects.hpp

#ifndef GLOBALOBJECTS_HPP_
#define GLOBALOBJECTS_HPP_

#include <SFML/Graphics.hpp>

class GlobalObjects
{
private:
GlobalObjects() = delete;

public:
static const float windowWidth, windowHeight;
static sf::RenderWindow window;
};

#endif // GLOBALOBJECTS_HPP_

11. Paddle.cpp

#include "Paddle.hpp"

Paddle::Paddle(const sf::Vector2f & size, const sf::Vector2f & position, const sf::Color & color, float speed)
{
paddle.setSize(size);
paddle.setPosition(position);
paddle.setFillColor(color);
paddle.setOrigin(size.x / 2.f, size.y / 2.f);

this->speed = speed;
}

void Paddle::Update(float deltaTime)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && paddle.getPosition().x > 0.f)
paddle.move(-speed * deltaTime, 0.f);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && paddle.getPosition().x < GlobalObjects::windowWidth)
paddle.move(speed * deltaTime, 0.f);
}

void Paddle::Draw(sf::RenderWindow & window)
{
window.draw(paddle);
}

12. Paddle.hpp

#ifndef PADDLE_HPP_
#define PADDLE_HPP_

#include <SFML/Graphics.hpp>
#include "GlobalObjects.hpp"

class Paddle
{
private:
sf::RectangleShape paddle;
float speed;

public:
Paddle(const sf::Vector2f & size, const sf::Vector2f & position, const sf::Color & color, float speed);

void Update(float deltaTime);
sf::Vector2f getPosition() const { return paddle.getPosition(); }
sf::Vector2f getSize() const { return paddle.getSize(); }

void Draw(sf::RenderWindow & window);

float left() const { return paddle.getPosition().x - paddle.getSize().x / 2.f; }
float rigth() const { return paddle.getPosition().x + paddle.getSize().x / 2.f; }
float top() const { return paddle.getPosition().y - paddle.getSize().y / 2.f; }
float bottom() const { return paddle.getPosition().y + paddle.getSize().y / 2.f; }
};

#endif // PADDLE_HPP_

13. main.cpp [Run this class to execute the application]

#include <SFML/Graphics.hpp>
#include <iostream>
#include "GlobalObjects.hpp"
#include "BlocksField.hpp"
#include "Paddle.hpp"
#include "Game.hpp"

int main()
{
sf::Event event;
sf::Clock clock;
float deltaTime;

//BlocksField blocksField(sf::Vector2f(512.f, 150.f), sf::Vector2f(0.f, 0.f), sf::Color::Yellow, 11, 6);
//BlocksField blocksField(sf::Vector2f(20.f, 50.f), sf::Vector2f(200.f, 50.f), sf::Color::Yellow, 0, 0);
BlocksField blocksField(sf::Vector2f(GlobalObjects::windowWidth, 200.f), sf::Vector2f(0.f, 0.f), sf::Color::Yellow, 9, 7);

//Game::createBall(Ball(10.f, sf::Vector2f(300.f, 400.f), sf::Color::Red, 50.f));
Game::createBall(Ball(10.f, sf::Vector2f(225.0710f, 400.f), sf::Color::Red, 180.f, 110.f));

//Paddle paddle(sf::Vector2f(100.f, 10.f), sf::Vector2f(256.f, 450.f), sf::Color::Green, 200.f);
Game::createPaddle(Paddle(sf::Vector2f(100.f, 10.f), sf::Vector2f(256.f, 450.f), sf::Color::Green, 200.f));

while (GlobalObjects::window.isOpen())
{
deltaTime = clock.restart().asSeconds();

while (GlobalObjects::window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
GlobalObjects::window.close();
}


Game::Update(deltaTime, blocksField);

GlobalObjects::window.clear(sf::Color::Black);

blocksField.Draw(GlobalObjects::window);
Game::Draw(GlobalObjects::window);

GlobalObjects::window.display();
}

return 0;
}

Please let me know in case of any clarifications required. Thanks!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote