Skip to main content
gamedev.net
🔒 Locked

Shooting routine code help?

Started by Viri- Jan 21, 2001 at 2:57 PM 5 replies 550+ views
Original Post
Viri-
Viri-
I am trying to make a devent shooting routine where the player (a ship) can shoot as many times as the player can press the space bar. I''m trying to use for loops to track which bullets are on the screen and to move then and draw them but I''m having trouble with this. If anyone would mind looking at my code and tell me what I ought to be doing I''d be very greatful. Here is my Code: // This struct sets up the speed variables and that sort. It // also sets the basic variables for each bullet #define BULLET_SPEED 8 #define MAX_BULLETS 3 // for now only allowing 3 to be on screen at once typedef struct { int x, y; // location of the bullet on screen int velocity; // veloity (set to BULLET_SPEED) RECT dstBulletRect; // destination RECT for blting RECT srcBulletRect; // src rect for blting int anim_count; // unused for now int onscreen; // is this bullet on the screen? And should be moved? }PLAYER_BULLET; PLAYER_BULLET player_bullet[MAX_BULLETS]; // sets the array up void Setup_PlayerBullet( void ); // sets up initial values void Draw_PlayerBullet( void ); // blt''s the bullet to the screen void Move_PlayerBullet( void ); // moves the bullet if onscreen == 1 void PlayerPreShoot( void ); // Sets up the bullet to shoot from the ship LPDIRECTDRAWSURFACE7 lpddsPlayerBullet; // ddsurface for the bullet //========================================= ////////////////////////////////////// // Player Bullet functions ////////////////////////// void Setup_PlayerBullet( void ) { for (int i = 0; i < 3; i++) //loop through it all and set up initial { // values for the bullts. // create the surface DD_CreateSurface(&lpddsPlayerBullet, 16, 16); // // load bitmap into the surface if(LoadBitmapFile(lpddsPlayerBullet, 0, 0, "bullet.bmp")!= TRUE) write_error("LoadBitmap Bullet Failed!"); // setup the src surface. Copying to NEWly created surface from top left to bottom right SetRect(&player_bullet.srcBulletRect, 0, 0, 16, 16); // set the starting point for the player to start on screen player_bullet[i].x = player_ship.x + 32; // hacked positions player_bullet[i].y = player_ship.y + 16; // " " SetRect(&player_bullet[i].dstBulletRect, player_bullet[i].x, player_bullet[i].y, (player_bullet[i].x+16), (player_bullet[i].y+16)); player_bullet[i].velocity = BULLET_SPEED; player_bullet[i].anim_count = 0; // not used player_bullet[i].onscreen = 0; // not on screen yet } } void Draw_PlayerBullet( void ) { for (int i = 0; i < 3; i++) // loop though all that have { // .onscreen set to 1 if (player_bullet[i].onscreen == 1) { // if it is set to 1 blt it to back surface hResult = lpddsback->Blt(&player_bullet[i].dstBulletRect, lpddsPlayerBullet, &player_bullet[i].srcBulletRect, DDBLT_WAIT, NULL); if(FAILED(hResult)) { write_error("Draw_PlayerBullet() 1 Blt Failed."); } } } } void Move_PlayerBullet( void ) { for (int i = 0; i < 3; i++) // loop and check all that have onscr { // set to 1 if (player_bullet[i].onscreen == 1) // is it to be moved? { player_bullet[i].y = player_bullet[i].y - player_bullet[i].velocity; // move it.... if (player_bullet[i].y < 0) { player_bullet[i].onscreen = 0; // off screen } SetRect(&player_bullet[i].dstBulletRect, player_bullet[i].x, player_bullet[i].y, player_bullet[i].x+16, player_bullet[i].y+16); // set the new rect for the draw func to draw it } } } void PlayerPreShoot( void ) { // when space is pressed this is called to keep the x cord of the bullet // the same as the player ship. for (int i = 0; i < 3; i++) // space was pressed so loop though { // all the ones that aren''t set to 1 // and set them up if (player_bullet[i].onscreen != 1) { player_bullet[i].x = player_ship.x + 32; player_bullet[i].y = player_ship.y + 16; player_bullet[i].onscreen = 1; } } } Ok now in my setup func the Setup_PlayerBullet() is called. And in the main function I have the following: //============== Main Function if (KEY_DOWN(VK_SPACE)) { PlayerPreShoot(); } Move_PlayerBullet(); Draw_PlayerBullet(); And for some reason it only allows me to shoot one bullet and it waits for me to get done. What am I doing wrong here?
------------thanks,Phoenix
atreyu
atreyu
I think you''re on the right track. I don''t have time to read all your code, but at a glance my guess is you''re not looping through all the bullets correctly. First "for (int i = 0; i < 3; i++) " should be for (int i = 0; i < MAX_BULLETS; i++). Second, your BULLET_SPEED is way to fast; That''s 8 pixels per frame. If you''re getting 80 fps then the bullet is moving 640 pixels every second. It''s probably jumping clear off the screen. Set it to something like 0.05. also, IMPORTANT: change your struct from:

  

typedef struct
{
int x, y; // location of the bullet on screen

int velocity; // veloity (set to BULLET_SPEED)

RECT dstBulletRect; // destination RECT for blting

RECT srcBulletRect; // src rect for blting

int anim_count; // unused for now

int onscreen; // is this bullet on the screen? And should be moved?

}PLAYER_BULLET;

to

typedef struct
{
float x, y; // location of the bullet on screen

float velocity; // veloity (set to BULLET_SPEED)

RECT dstBulletRect; // destination RECT for blting

RECT srcBulletRect; // src rect for blting

int anim_count; // unused for now

int onscreen; // is this bullet on the screen? And should be moved?

}PLAYER_BULLET;



Another thing, If you want your bullets to move in any direction other then up,down,left,right you''ll need to add "float xv, yv;" to that structure. These variables store the bullet''s vector (change in x, and change in y). So if i wanted my bullet to fire on a 45 degree angle you would do this:

  

int i = some number;
float angle = 45.0f;
if(player_bullet[i] isn''t currently used)
{
player_bullet[i].xv = cos(angle * (PI/180));
player_bullet[i].yv = sin(angle * (PI/180));
}

//then in my bullet update loop i''d do this


for(int i=0; i<MAX_BULLETS; i++)
{
player_bullet[i].x += (player_bullet[i].xv * velocity);
player_bullet[i].y += (player_bullet[i].yv * velocity);
}



also, this looks wrong:
player_bullet.y = player_bullet[i].y - player_bullet[i].velocity;

the velocity should be added. ie: if a car is moving 20mph and a baseball is thrown forward out the window at 10mph then the baseball''s velocity is 30mph.

Hope this helps. Another thing that might help you is to look up some 2D particle system source code.
--atreyu
Janju
Janju
Maybe this interests you.
Viri-
Viri-
What do you mean by the Xenon articles? The website didn''t make much sense. But thanks to you both. I''ll screw around with it some more and then write back if I have any other questions....

Viri-
------------thanks,Phoenix
Viri-
Viri-
Yes but how can I get this magazine? It started in November or sometime in 2000. Its now 2001 and I couldnt'' find any online articles or any place to buy back issues. How could I obtain this info, I''m sure it''d help.

Thanks,
Viri-
------------thanks,Phoenix
Janju
Janju
I mailed the Bitmapbrothers and they told me that the whole tutorial with all source,graphics etc will be released in 3 Month or something like on that webpage - thats all.

DX8 Samples are here

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.