Minor Assignment-07 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 member declaration:
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.
2. Public member function prototypes of the Bullet class
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.
3. SFML-C++ constructor for bullet of size 20×20 and color red
Bullet::Bullet() {
m_Shape.setSize(sf::Vector2f(20, 20));
m_Shape.setFillColor(sf::Color::Red);
}
4. Set bullet position and compute gradient
void Bullet::shoot(float startX, float startY, float targetX, float targetY) {
m_InFlight = true;
m_Position.x = startX;
m_Position.y = startY;
float gradient = (startY - targetY) / (startX - targetX);
float ratio = sqrt((1 + pow(gradient, 2)));
m_BulletSpeedX = m_BulletSpeed / ratio;
m_BulletSpeedY = m_BulletSpeed * gradient / ratio;
if (targetX < startX) {
m_BulletSpeedX *= -1;
m_BulletSpeedY *= -1;
}
m_Shape.setPosition(m_Position);
}
5. Compute bullet speed horizontally and vertically
m_BulletSpeedX = m_BulletSpeed / sqrt(1 + gradient * gradient);
m_BulletSpeedY = m_BulletSpeedX * gradient;
6. Set max horizontal and vertical bullet range (1200 px)
m_MaxX = startX + 1200;
m_MinX = startX - 1200;
m_MaxY = startY + 1200;
m_MinY = startY - 1200;
7. Check if bullet is out of range
if (m_Position.x < m_MinX || m_Position.x > m_MaxX ||
m_Position.y < m_MinY || m_Position.y > m_MaxY) {
m_InFlight = false;
}
8. Handle left mouse click to fire a bullet
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.
9. Loop through bullet array to draw bullets in flight
for (int i = 0; i < maxBullets; ++i) {
if (bullets[i].isInFlight()) {
window.draw(bullets[i].getShape());
}
}
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
for (int i = 0; i < numZombies; ++i) {
for (int j = 0; j < maxBullets; ++j) {
if (bullets[j].isInFlight() && bullets[j].getPosition().intersects(zombies[i].getPosition()))
{
bullets[j].stop();
zombies[i].die();
}
}
}
bool allDead = true;
for (int i = 0; i < numZombies; ++i) {
if (zombies[i].isAlive()) {
allDead = false;
break;
}
}
if (allDead) {
gameState = WIN;
}
12. Public inheritance: A → B
class B : public A {
public:
void bFunction();
};
Public members accessible in B: 4 (from A) + 1 (from B) = 5
13. Private inheritance: A → B
class B : private A {
private:
int bPrivate;
};
Private members in B (including inherited): 4 (from A, now private) + 1 = 5
14. Multiple inheritance from A, B, C, D into E
class E : public A, public B, private C, protected D {
};
15. Output of constructor inheritance
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