2
\$\begingroup\$

Using my universities API, I'm making a basic 2D game where pacman can shoot ghosts

I have a bullet class but at the moment, I only have one instance of the bullet

could anyone help edit this code/give me hints so that whenever I click i add a new bullet to the screen and after a certain amount of time it is deleted

void Bullet::update(int elapsedTime, Input::MouseState* mouseState, Player* pacman)
{


    if (mouseState->LeftButton == Input::ButtonState::PRESSED && !shooting)
    {
        setX(mouseState);
        setY(mouseState);
        shooting = true;
    }
    if (shooting)
    {
        float xDistance = mouseX - position->X;
        float yDistance = mouseY - position->Y;
        float hypotenuse = sqrt((xDistance * xDistance) + (yDistance * yDistance));

        position->X += elapsedTime * ((xDistance) / hypotenuse) / 2;
        position->Y += elapsedTime * ((yDistance) / hypotenuse) / 2;
        currentTime += (float)elapsedTime / 1000;

        if (currentTime >= maxTime)
        {
            setPosition(pacman->getPosition()->X + pacman->getRect()->Width / 2, pacman->getPosition()->Y + pacman->getRect()->Height / 2);
            currentTime = 0;
            shooting = false;
        }

    }
    else
    {
        setPosition(pacman->getPosition()->X + pacman->getRect()->Width / 2, pacman->getPosition()->Y + pacman->getRect()->Height / 2);
    }
}

and the bullet is loaded here

Texture2D* bulletTex = new Texture2D();
bulletTex->Load("Textures/bullet.png", false);
Vector2* bulletPos = new Vector2(pacman->getPosition()->X, pacman->getPosition()->Y);
Rect* bulletRect = new Rect(0.0f, 0.0f, 5, 5);
bullet = new Bullet(bulletPos, bulletRect, bulletTex);

Thanks in advance

\$\endgroup\$
2
  • \$\begingroup\$ Is your problem that you don't know how to create a list(vector) of bullet objects, or you don't know how to organize your game engine to keep track of all of the bullets? \$\endgroup\$ Commented Dec 8, 2015 at 23:31
  • \$\begingroup\$ @spectacularbob dont know how to organize the engine to keep track of all of the bullets \$\endgroup\$
    – Rhino1181
    Commented Dec 8, 2015 at 23:32

2 Answers 2

5
\$\begingroup\$

It looks like your bullet class is doing too much work.

You are right to load the bullet texture only once, but you need one instance of a Bullet per Bullet on the screen. It is good that you are passing the bulletTex into the bullet so that all of these bullets can share the texture.

Your bullet should not be reacting to mouse input at all. In real life, bullets don't shoot themselves. Once a bullet is fired, it will just go in one direction until it hits something or, in the case of your game, time out. Instead, the bullet creation code needs to be higher up the chain. I suggest creating a shoot() method in your player that looks like this (bulletlist would be a std::vector of all bullets on the screen).

void Player::shoot()
{
   Vector2* bulletPos = new Vector2(this->getPosition()->X, this->getPosition()->Y);
   Rect* bulletRect = new Rect(0.0f, 0.0f, 5, 5);
   Bullet * bullet = new Bullet(bulletPos, bulletRect, bulletTex);
   bulletList.push_back(bullet);
}

then in your main game loop you can check if the mouse is pressed and call shoot() if it is. You can also add logic to the shoot function to add a delay that won't let the user shoot until x seconds have passed since the last shot so that you don't just get a steady stream of bullets.

Once a bullet is created, it's position is no longer dependent on the Player's position. Instead, it has a velocity vector that it will travel on until it hits something or times out. So in Bullet::Update() you do not need to check the player's position to determine where the bullet should be.

Lastly, "killing" your bullets after x seconds is easily done in the update() call. Every frame, update will be called and elapsed time is passed in. In your Bullet class you can add an timeTillDeath integer. Every update call you can subtract elapsedTime from timeTillDeath. Once timeTillDeath <= 0 the bullet is considered dead and should be removed from the bullet list.

\$\endgroup\$
1
  • \$\begingroup\$ in addition, a more data driven design would improve performance. Rather than having each bullet update itself, bullets are just data containers and there is a game loop method/section that updates all of them in one go \$\endgroup\$
    – CobaltHex
    Commented Dec 9, 2015 at 20:58
1
\$\begingroup\$

Use vector to hold multiple Bullet instances. It's also easy and safer to use.

Documentation: http://www.cplusplus.com/reference/vector/vector/

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .