Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Block.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "Block.h"

Block::Block(float x, float y, BlockType t, int h, bool hb, BonusType bt):
type(t), health(h), maxHealth(h), hasBonus(hb), bonusType(bt), destroyed(false)
{
shape.setPosition(x, y);
shape.setSize({ BLOCK_WIDTH, BLOCK_HEIGHT });
shape.setOrigin(BLOCK_WIDTH / 2, BLOCK_HEIGHT / 2);
updateColor();
}

void Block::updateColor() {
switch (type) {
case BlockType::UNBREAKABLE:
shape.setFillColor(sf::Color(150, 150, 150));
break;
case BlockType::REGULAR: {
float ratio = static_cast<float>(health) / maxHealth;
sf::Uint8 green = static_cast<sf::Uint8>(255 * ratio);
shape.setFillColor(sf::Color(0, green, 0));
break;
}
case BlockType::SPEED_CHANGE:
shape.setFillColor(sf::Color(255, 255, 0));
break;
case BlockType::WITH_BONUS:
shape.setFillColor(sf::Color(255, 0, 255));
break;
}
}

void Block::hit() {
if (type != BlockType::UNBREAKABLE) {
health--;
updateColor();
if (health <= 0) {
destroyed = true;
}
}
}

bool Block::isDestroyed() const {
return destroyed;
}

sf::FloatRect Block::getBounds() const {
return shape.getGlobalBounds();
}

BlockType Block::getType() const {
return type;
}

bool Block::hasBonusInside() const {
return hasBonus;
}

BonusType Block::getBonusType() const {
return bonusType;
}

sf::Vector2f Block::getPosition() const {
return shape.getPosition();
}

void Block::draw(sf::RenderWindow& window) const {
if (!destroyed) {
window.draw(shape);
}
}
27 changes: 27 additions & 0 deletions Block.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include "Types.h"
#include "Constants.h"

class Block {
public:
Block(float x, float y, BlockType t, int h = 1, bool hb = false, BonusType bt = BonusType::BOTTOM_SHIELD);

void updateColor();
void hit();
bool isDestroyed() const;
sf::FloatRect getBounds() const;
BlockType getType() const;
bool hasBonusInside() const;
BonusType getBonusType() const;
sf::Vector2f getPosition() const;
void draw(sf::RenderWindow& window) const;

private:
sf::RectangleShape shape;
BlockType type;
int health;
int maxHealth;
bool hasBonus;
BonusType bonusType;
bool destroyed;
};
72 changes: 72 additions & 0 deletions Bonus.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "Bonus.h"

Bonus::Bonus(float x, float y, BonusType t)
: type(t), active(true), used(false) {
shape.setPosition(x, y);
shape.setSize({ BONUS_WIDTH, BONUS_HEIGHT });
shape.setOrigin(BONUS_WIDTH / 2, BONUS_HEIGHT / 2);

switch (type) {
case BonusType::PADDLE_SIZE_INCREASE:
shape.setFillColor(sf::Color(0, 255, 0));
break;
case BonusType::PADDLE_SIZE_DECREASE:
shape.setFillColor(sf::Color(255, 0, 0));
break;
case BonusType::BALL_SPEED_INCREASE:
shape.setFillColor(sf::Color(255, 165, 0));
break;
case BonusType::BALL_SPEED_DECREASE:
shape.setFillColor(sf::Color(0, 255, 255));
break;
case BonusType::STICKY_PADDLE:
shape.setFillColor(sf::Color(255, 190, 200));
break;
case BonusType::BOTTOM_SHIELD:
shape.setFillColor(sf::Color(255, 255, 255));
break;
case BonusType::RANDOM_TRAJECTORY_CHANGE:
shape.setFillColor(sf::Color(128, 0, 128));
break;
}
}

void Bonus::update(float deltaTime) {
if (isActive()) {
shape.move(0, BONUS_SPEED * deltaTime);
}
}

void Bonus::draw(sf::RenderWindow& window) const {
if (isActive()) {
window.draw(shape);
}
}

void Bonus::activate() {
active = true;
}

void Bonus::deactivate() {
active = false;
}

void Bonus::use() {
used = true;
}

bool Bonus::isActive() const {
return active && !used;
}

sf::FloatRect Bonus::getBounds() const {
return shape.getGlobalBounds();
}

BonusType Bonus::getType() const {
return type;
}

sf::Vector2f Bonus::getPosition() const {
return shape.getPosition();
}
23 changes: 23 additions & 0 deletions Bonus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include "Types.h"
#include "Constants.h"

class Bonus {
public:
Bonus(float x, float y, BonusType t);
void update(float deltaTime);
void draw(sf::RenderWindow& window) const;
void activate();
void deactivate();
void use();
bool isActive() const;
sf::FloatRect getBounds() const;
BonusType getType() const;
sf::Vector2f getPosition() const;

private:
sf::RectangleShape shape;
BonusType type;
bool active;
bool used;
};
23 changes: 23 additions & 0 deletions Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include <SFML/Graphics.hpp>
#include <cmath>

#define M_PI 3.1415926535897932384f

const unsigned int WINDOW_WIDTH = 800;
const unsigned int WINDOW_HEIGHT = 600;
const float INITIAL_PADDLE_WIDTH = 100.0;
const float PADDLE_HEIGHT = 20.0;
const float BALL_RADIUS = 10.0;
const float BLOCK_WIDTH = 80.0;
const float BLOCK_HEIGHT = 30.0;
const float BONUS_WIDTH = 30.0;
const float BONUS_HEIGHT = 20.0;
const float INITIAL_BALL_SPEED = 300.0;
const float PADDLE_SPEED = 500.0;
const float BONUS_SPEED = 150.0;
const int LOSE_PENALTY = 10;
const int WIN_SCORE = 50;
const unsigned int FONT_SIZE = 24;
const sf::Color TEXT_COLOR = sf::Color::White;
const int INITIAL_LIVES = 3;
Loading