Shooting Algorithm
OK Basically I want a ship to shoot propjectiles, kinda like galaga. But there is only one thread, so how would I design it. The game goes through a game main every loop, and I have a projectile class set up, with variabless x,y and velocity. Please give any suggestions, any help is better then none. Thanx in advane:>
Edited by - felisandria on March 27, 2001 8:41:21 PM
I can''t tell you that - if I teach you how to shoot a projectile you would then go to a school and start killing people.
On a second thought, since you probably don''t live anywere near where i do, here goes:
You need to check at the start of the cycle how much time has passed since the start of the last cycle - with this time you can update the projectile''s x and y position according to the velocity (newx = x + vx*time, newy = y + vy*time, vx and vy being the x and y components of the velocity) then you can proceed to draw the next frame, with the projectile in the new position
Was that what you wanted to hear?
You know, I never wanted to be a programmer...
Alexandre Moura
On a second thought, since you probably don''t live anywere near where i do, here goes:
You need to check at the start of the cycle how much time has passed since the start of the last cycle - with this time you can update the projectile''s x and y position according to the velocity (newx = x + vx*time, newy = y + vy*time, vx and vy being the x and y components of the velocity) then you can proceed to draw the next frame, with the projectile in the new position
Was that what you wanted to hear?
You know, I never wanted to be a programmer...
Alexandre Moura
To make things clearer, here it goes. I have a class with the x,y,velocity. What I tried was declaring 25 instances of the class, then checking if spacebar is pressed, if it is it goes to the shoot function.
if (weaponWait < 100)
weaponWait += 1;
if (weaponWait > 33 && bulletCount < 25)
{
bulletCount ++;
if (KEYDOWN(VK_SPACE))
{
bullet[bulletCount].fired = TRUE;
bullet[bulletCount].x = shipX;
bullet[bulletCount].y = shipY;
weaponWait = 0;
}
}
The weapon wait is just to make sure, that the gun doesn''t go crazy firing, because there are 30 frames a sec, and I don''t want the gun going off 30 times in 1 second. However the code isn''t really working, the problem is that since there are only 25 instances, and the projectile should be drawn and calculated every frame, well only if the spacebar was actually pressed, or the projectile is still on the screen and needs to be moved. Anyways I''m getting confused by this. Can somebody give me a better explanation or example. Previously, I had multi threads that would handle each projectile, but that was another language. Anyways, I NEED SOME HELP, so drop me a few lines if you know what you''re talking about. Thanx in advance:>
if (weaponWait < 100)
weaponWait += 1;
if (weaponWait > 33 && bulletCount < 25)
{
bulletCount ++;
if (KEYDOWN(VK_SPACE))
{
bullet[bulletCount].fired = TRUE;
bullet[bulletCount].x = shipX;
bullet[bulletCount].y = shipY;
weaponWait = 0;
}
}
The weapon wait is just to make sure, that the gun doesn''t go crazy firing, because there are 30 frames a sec, and I don''t want the gun going off 30 times in 1 second. However the code isn''t really working, the problem is that since there are only 25 instances, and the projectile should be drawn and calculated every frame, well only if the spacebar was actually pressed, or the projectile is still on the screen and needs to be moved. Anyways I''m getting confused by this. Can somebody give me a better explanation or example. Previously, I had multi threads that would handle each projectile, but that was another language. Anyways, I NEED SOME HELP, so drop me a few lines if you know what you''re talking about. Thanx in advance:>
Well, you need to update the bullet-array every loop.
Btw if you don''t have a fixed timestep on your game-logic you shouldn''t use a weapon wait like that, since then you''ll be able to fire more often on a faster machine, you should instead use time.
So here''s some psudo-code:
As I said this is psudo code so don''t think it will compile, but it''s the general idea that''s important, you initialise your bullet in one place (i.e. when someone fires) and then you update all the bullets in another. In the updating of the bullets you might want to check if a bullet should be removed (for instance if it has flown off the screen).
Hope this helps.
Btw if you don''t have a fixed timestep on your game-logic you shouldn''t use a weapon wait like that, since then you''ll be able to fire more often on a faster machine, you should instead use time.
So here''s some psudo-code:
|
As I said this is psudo code so don''t think it will compile, but it''s the general idea that''s important, you initialise your bullet in one place (i.e. when someone fires) and then you update all the bullets in another. In the updating of the bullets you might want to check if a bullet should be removed (for instance if it has flown off the screen).
Hope this helps.
This is code from my shootem-up. Email me at indyuk2000@yahoo.com if you need to know how it works./***********************************************************
FUNCTION : HeroFire()
INPUTS : none
RETURNS : none
DESCRIPTION : Initialise the hero''s missile(s)
VERSION : 0.1 - Initial module release
DATE : 25/6/00
AUTHOR : I. Singh
**********************************************************/
void HeroFire(void)
{
for(int slot=0;slot {
if(Player.FireAYpos[slot]==0 )
{
Player.FireAXpos[slot]=Player.Xpos+2; // setup the burst
Player.FireAYpos[slot]=Player.Ypos-20;
//FSOUND_PlaySound(/*FSOUND_FREE*/10, HeroFireWav);
return;
}
}
}
/***********************************************************
FUNCTION : UpdatePlayerKB()
INPUTS : none
RETURNS : none
DESCRIPTION : Update all player movement via keyboard
VERSION : 0.1 - Initial module release
DATE : 17/6/00
AUTHOR : I. Singh
**********************************************************/
void UpdatePlayerKB()
{
Input->Update();
if(PlayerAnimateTimer->Timer(30)==true)
{
Player.AnimFrame++;
if(Player.AnimFrame>2)
{
Player.AnimFrame=0;
}
PlayerShip->SetFrame(Player.AnimFrame);
Player.FireAAnimFrame+=1;
if((Player.FireAAnimFrame)>1)
{
Player.FireAAnimFrame=0;
}
PlayerBullet->SetFrame(Player.FireAAnimFrame);
}
if( Input->GetKeyState( CDXKEY_Q ) ) // is the up arrow key pressed ?
{
if(Player.Ypos<=20)
{
Player.Ypos=20;
}
else
{
Player.Ypos-=Player.YVelocity;
}
}
else if( Input->GetKeyState( CDXKEY_A ) ) // is the down arrow key pressed ?
{
if(Player.Ypos>=380)
{
Player.Ypos=380;
}
else
{
Player.Ypos+=Player.YVelocity;
}
}
if( Input->GetKeyState( CDXKEY_O ) ) // is the left arrow key pressed ?
{
PlayerShip->SetFrame(4);
if(Player.Xpos<=10)
{
Player.Xpos=10;
}
else
{
Player.Xpos-=Player.XVelocity;
}
}
if( Input->GetKeyState( CDXKEY_P ) ) // is the right arrow key pressed ?
{
PlayerShip->SetFrame(5);
if(Player.Xpos>=600)
{
Player.Xpos=600;
}
else
{
Player.Xpos+=Player.XVelocity;
}
}
if( Input->GetKeyState( CDXKEY_SPACE)) // is the left control key pressed ?
{
if(BurstDelay->Timer(Player.FireADelay)==true) // just a small burst delay.
{
HeroFire();
}
}
for(int slot=0;slot {
if(Player.FireAYpos[slot]>0 ) // check to see whether burst has gone off screen
{
Player.FireAYpos[slot]-=5;
PlayerBullet->SetPos(Player.FireAXpos[slot],Player.FireAYpos[slot]);
PlayerBullet->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
}
else // if off screen, turn it off
{
Player.FireAXpos[slot]=0;
Player.FireAYpos[slot]=0;
}
}
PlayerShip->SetPos(Player.Xpos,Player.Ypos);
PlayerShip->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
if(Player.Shields<=0)
{
Player.Exploding=true;
Player.Lives-=1; // deduct a life
Player.ExplosionX=Player.Xpos;
Player.ExplosionY=Player.Ypos;
//FSOUND_PlaySound(FSOUND_FREE, HeroExplodeWav);
}
}
FUNCTION : HeroFire()
INPUTS : none
RETURNS : none
DESCRIPTION : Initialise the hero''s missile(s)
VERSION : 0.1 - Initial module release
DATE : 25/6/00
AUTHOR : I. Singh
**********************************************************/
void HeroFire(void)
{
for(int slot=0;slot
if(Player.FireAYpos[slot]==0 )
{
Player.FireAXpos[slot]=Player.Xpos+2; // setup the burst
Player.FireAYpos[slot]=Player.Ypos-20;
//FSOUND_PlaySound(/*FSOUND_FREE*/10, HeroFireWav);
return;
}
}
}
/***********************************************************
FUNCTION : UpdatePlayerKB()
INPUTS : none
RETURNS : none
DESCRIPTION : Update all player movement via keyboard
VERSION : 0.1 - Initial module release
DATE : 17/6/00
AUTHOR : I. Singh
**********************************************************/
void UpdatePlayerKB()
{
Input->Update();
if(PlayerAnimateTimer->Timer(30)==true)
{
Player.AnimFrame++;
if(Player.AnimFrame>2)
{
Player.AnimFrame=0;
}
PlayerShip->SetFrame(Player.AnimFrame);
Player.FireAAnimFrame+=1;
if((Player.FireAAnimFrame)>1)
{
Player.FireAAnimFrame=0;
}
PlayerBullet->SetFrame(Player.FireAAnimFrame);
}
if( Input->GetKeyState( CDXKEY_Q ) ) // is the up arrow key pressed ?
{
if(Player.Ypos<=20)
{
Player.Ypos=20;
}
else
{
Player.Ypos-=Player.YVelocity;
}
}
else if( Input->GetKeyState( CDXKEY_A ) ) // is the down arrow key pressed ?
{
if(Player.Ypos>=380)
{
Player.Ypos=380;
}
else
{
Player.Ypos+=Player.YVelocity;
}
}
if( Input->GetKeyState( CDXKEY_O ) ) // is the left arrow key pressed ?
{
PlayerShip->SetFrame(4);
if(Player.Xpos<=10)
{
Player.Xpos=10;
}
else
{
Player.Xpos-=Player.XVelocity;
}
}
if( Input->GetKeyState( CDXKEY_P ) ) // is the right arrow key pressed ?
{
PlayerShip->SetFrame(5);
if(Player.Xpos>=600)
{
Player.Xpos=600;
}
else
{
Player.Xpos+=Player.XVelocity;
}
}
if( Input->GetKeyState( CDXKEY_SPACE)) // is the left control key pressed ?
{
if(BurstDelay->Timer(Player.FireADelay)==true) // just a small burst delay.
{
HeroFire();
}
}
for(int slot=0;slot
if(Player.FireAYpos[slot]>0 ) // check to see whether burst has gone off screen
{
Player.FireAYpos[slot]-=5;
PlayerBullet->SetPos(Player.FireAXpos[slot],Player.FireAYpos[slot]);
PlayerBullet->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
}
else // if off screen, turn it off
{
Player.FireAXpos[slot]=0;
Player.FireAYpos[slot]=0;
}
}
PlayerShip->SetPos(Player.Xpos,Player.Ypos);
PlayerShip->Draw(Screen->GetBack(),0,0,CDXBLT_TRANS);
if(Player.Shields<=0)
{
Player.Exploding=true;
Player.Lives-=1; // deduct a life
Player.ExplosionX=Player.Xpos;
Player.ExplosionY=Player.Ypos;
//FSOUND_PlaySound(FSOUND_FREE, HeroExplodeWav);
}
}
Indy
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement