MinorAssignment07 Solutions
MinorAssignment07 Solutions
1. Name the SFML class to visually represent a bullet and declare a private
member in Bullet class
In SFML, the class used to visually represent a bullet is `sf::RectangleShape`.
private:
sf::RectangleShape m_Shape; // Represents the bullet visually
We use `sf::RectangleShape` because bullets are usually small rectangles or circles, and
SFML provides `RectangleShape` for simple 2D shapes.
class Bullet {
public:
Bullet(); // Constructor
void shoot(float startX, float startY, float targetX, float targetY);
void stop();
bool isInFlight();
sf::FloatRect getPosition();
sf::RectangleShape getShape();
void update(float elapsedTime);
};
These functions handle shooting, stopping, updating position, checking if bullet is in flight,
and retrieving its shape for drawing.
Bullet::Bullet() {
m_Shape.setSize(sf::Vector2f(20, 20));
m_Shape.setFillColor(sf::Color::Red);
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
bullets[currentBullet].shoot(playerPosition.x, playerPosition.y, mouseWorldPos.x,
mouseWorldPos.y);
currentBullet++;
if (currentBullet >= maxBullets) currentBullet = 0;
}
Place this inside the main game loop, under the input-handling section.
10. Check if zombie touched player and change game state if health ≤ 0
if (zombie.getPosition().intersects(player.getPosition())) {
player.reduceHealth();
if (player.getHealth() <= 0) {
gameState = GAME_OVER;
}
}
11. Check if zombie is shot and end game if all zombies are dead
if (allDead) {
gameState = WIN;
}
class B : public A {
public:
void bFunction();
};
class B : private A {
private:
int bPrivate;
};
class B1 {
public:
B1() { cout << "B1" << endl; }
};
class B2 {
public:
B2() { cout << "B 2" << endl; }
};
class Derived : public B1, public B2 {
public:
Derived() { cout << "D" << endl; }
};
int main() {
Derived d;
return 0;
}
Output:
B1
B2
D