Jump to content
  • Advertisement
Sign in to follow this  
xM1k3x

Shooting in a 2d game

This topic is 3623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have asked several questions about this subject before but I have not been able to solve my problem. I just cant seem to wrap my head around how I can program shooting in games. Perhaps someone could help rework my algoritm and help me better understand how to accomplish this task. Here is my Ships class:
#include "PlayerShip.h"

Fire FFire;
Gun Gun;

void PlayerShip::CallMovement()
{
	Gun.x = PlayerShip::x;
	Gun.y = PlayerShip::y;

  if(Key_Down(DIK_LEFT))
  {
	  if(PlayerShip::x != 0)
	{
		PlayerShip::x -= PlayerShip::movex;
		Gun.x -= PlayerShip::movex;
		PlayerShip::gunx -= PlayerShip::movex;
	}
  }
  if(Key_Down(DIK_RIGHT))
  {
	  if(PlayerShip::x < (SCREEN_WIDTH - PlayerShip::width))
	  {
		PlayerShip::x += PlayerShip::movex;
		Gun.x -= PlayerShip::movex;
		PlayerShip::gunx += PlayerShip::movex;
	}
  }
  if(Key_Down(DIK_UP))
  {
	  if(PlayerShip::y != 0)
	{
		PlayerShip::y -= PlayerShip::movey;
		Gun.y -= PlayerShip::movey;
		PlayerShip::guny -= PlayerShip::movey;
	}
  }
  if(Key_Down(DIK_DOWN))
  {
	  if(PlayerShip::y < (SCREEN_HEIGHT - PlayerShip::height))
	{
		PlayerShip::y += PlayerShip::movey;
		Gun.y += PlayerShip::movey;
		PlayerShip::guny += PlayerShip::movey;
	}
  }

}
void PlayerShip::CallFire()
{


                PlayerShip::guny -= PlayerShip::movey;


		PlayerShip::guny -= PlayerShip::movey;
		if(PlayerShip::guny > 0)
		{
			PlayerShip::guny -= PlayerShip::movey;
		}

	
}

Here is how I declare an object of my ships class:
Player.x = 270;
	Player.y = 300;
	Player.gunx = 280;
	Player.guny = 300;
	Player.width = 96;
	Player.height = 96;
	Player.gunwidth = 24;
	Player.gunheight = 32;
	Player.movex = 7;
	Player.movey = 7;

	for(int count = 0; count < NUMFIRE; count++)
	{
		PlayerGun[count].x = 270;
		PlayerGun[count].y = 300;
		PlayerGun[count].gunx = 280;
		PlayerGun[count].guny = 300;
		PlayerGun[count].gunwidth = 24;
		PlayerGun[count].gunheight = 32;
		PlayerGun[count].movex = 7;
		PlayerGun[count].movey = 7;
	}


As you can see in the declaration I am trying to use an array to accomplish the task of shooting.
 //move the sprite
 Player.CallMovement();
for(int count = 0; count < NUMFIRE;count++)
{
	PlayerGun[count].CallMovement();
}

			
  if(FIRE == true)
{
     int count = 0;
				
     PlayerGun[count].CallFire();

      count = count + 1;
			}

     if(Key_Down(DIK_SPACE))
     {
	isFire = true;
	  
     }
            if(isFire == true)
        	{

				int count = 0;                
				     FiringTheGun(PlayerGun[count].gunx,PlayerGun[count].guny);

				count = count + 1;

                                FIRE = true;

                }

And here is the function that draws the bullets:
void FiringTheGun(int x, int y)
{
	D3DXVECTOR3 position((float)x, (float)y, 0);   

	sprite_handler->Draw(
					gun_fire,
					NULL,
					NULL,
					&position,
					D3DCOLOR_XRGB(255,255,255));


	if(position.x == -2)
	{
		isFire = false;
		FIRE = false;
	}

}

I really need help with this it is kicking my butt and I dont see no tutorials or nothing to help me. Tell me what you guys think is the best way to do shooting in a game? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Hallo,

I did not read most of your code, so sorry is this is not right. But Im a bit short on time right now.

This is how I would do shooting:


Player.createNewBullet(startX, startY, velX, velY)



and then have a update function like this:


bullet.update()
{
for(each element in bulletList)
bulletList.xPos += bulletList.xVel;
bulletList.yPos += bulletList.yVel;
}


Share this post


Link to post
Share on other sites
You didn't specify what exactly was not working. Sopmetimes a code error is so trivial that you have to dig into your whole code to find it. That's why debugging is such a pain. Shooting shouldn't be hard to implement, is your drawing routine correctly setup?

Share this post


Link to post
Share on other sites
The best way to do shooting in games is to store the bullets in about the same place as you store every other object in the game. You can either have a Ship array and a Bullet array, or just base them all on an Object class and have an Object* array. When you fire a bullet, just dump it in the list and forget about it; the code that takes care of updating the bullet is also responsible for destroying it when it hits something or leaves the area.

I think I've said how I would do it. class Gun has a method fire(int xvel, int yvel) which creates a bullet with the passed velocity (it calculates its own position), and puts it in the list. Make the list global if you have to, you'll figure out how to de-globalize it later. To shoot a bullet, you call gun.fire(x, y). That simple.

It would be helpful it you thought very hard about what all your classes related to firing (PlayerShip, Gun, Fire) actually do, and explain it to us. class Fire has me totally mystified. What does it do? What does this variable bool FIRE do? Maybe in explaining it, you'll figure out what's wrong with it. We also need to see the declarations of your data structures like PlayerGun, the actual player, and exactly where they are.

And for crying out loud, you're still using this syntax: PlayerShip::x. If PlayerShip's members are static, then change them to normal. If not, I don't understand how that compiles.

Share this post


Link to post
Share on other sites
Sorry, I only skimmed through your code.

The best you can do is have a BulletManager that handles all bullets and collision with other objects in the level.

I suggest you use vectors instead of separate x, y components. If you use a vector, you can make your bullet travel using a formula like the following:

bullet.position += interval * bullet.speed * bullet.direction;


(This is assuming your bullet travels in a straight line and at constant speed). Possition is a 2D vector, interval the number of seconds elapsed since last update, speed is a float in units/second (units may be pixels) and bullet direction is a normalized (length = 1) vector.

Another tip, even if you're making a 2D game with positions meaning pixels on screen, you should use floats to store your vectors and positions and only round to int, if necessary, when showing the sprite on screen.

I hope this is helpful.

Share this post


Link to post
Share on other sites
theOcelot, you find it better to have a list of all the shots instead of a list of shots for each weapon or each ship?

Currently in my 2d shooter I have ships, which have weapons and each weapon has a list of shots that it fired.

if I switched to the former I guess it may be easier for bounds checking and collision detection, but then I would have to pass the shots to each ship then to each weapon.

Share this post


Link to post
Share on other sites
Sorry guys I wasnt too specific as to whats wrong because I was in a hurry and posted quickly.

Here is my problem I can shoot off only one bullet in game but when I try to draw a second bullet it wont shoot again. Do I have to release the sprite_handler->draw first before it can draw again?? I am not sure because I believe what I have should be working but its not.

Appreciate the help.

Share this post


Link to post
Share on other sites
Quote:
Original post by xM1k3x
Sorry guys I wasnt too specific as to whats wrong because I was in a hurry and posted quickly.

Here is my problem I can shoot off only one bullet in game but when I try to draw a second bullet it wont shoot again. Do I have to release the sprite_handler->draw first before it can draw again?? I am not sure because I believe what I have should be working but its not.

Appreciate the help.



Is there a better way to load an image with Directx than what I am doing??

Cause it seems that the sprite_handler wont let me draw a new bullet.

Share this post


Link to post
Share on other sites
Please post your entire source code with comments. Or at least post full functions and indicate when and in what order they are called. It is difficult to follow the code fragments that you posted. I am mystified by all of your variables.

I can definitely see a couple of things wrong with your code, but I can't tell you how to fix them, if I can't see the full interactions of your code.

Share this post


Link to post
Share on other sites
Quote:
Original post by dzeligman
theOcelot, you find it better to have a list of all the shots instead of a list of shots for each weapon or each ship?

Currently in my 2d shooter I have ships, which have weapons and each weapon has a list of shots that it fired.

if I switched to the former I guess it may be easier for bounds checking and collision detection, but then I would have to pass the shots to each ship then to each weapon.


What do you mean by passing the shots to the ships? When all collidable objects are in one place, you just collide each one with every other one. It seems you'd have more passing of bullets with your way. But you could do all collisions on a ship level. When two ships are tested, they also test all their bullets.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!