Firing a bullet - what's wrong witht this??-
Well, now, after about a year without programming, I am back! I use to use DJGPP and Allegro, and that was fun for a while, but now onto bigger and better things.
Anyway, I am having a problem making my ship fire a bullet. This may seem very elementary, but oh well, I need help. Here is the code for a simple bullet firing:
if (KEY_DOWN(VK_SPACE) && activebullet == false)
{
activebullet = true;
bullet1.y = ship1.y;
bullet1.x = ship1.x + 15;
Draw_BOB(&bullet1,lpddsback);
while (activebullet = true)
{
bullet1.y = bullet1.y + 4;
DD_Flip();
if (bullet1.y <= 0)
{
activebullet = false;
}
}
Now, I am pretty sure it has to do with the time I am displaying it, or maybe the coords are wrong, but I can''t figure it out. Oh, if you need to know, which you shouldn''t in this case, I am using the GPDumb library from
Windows Game Programming for Dummies.
Thank you very much!!
Kirk
for me..first came QBASIC, then C w/ DJGPP and Allegro, now DirectX and C++.. what a life!!
for me..first came QBASIC, then C w/ DJGPP and Allegro, now DirectX and C++.. what a life!!
Hello, I noticed a couple of things you may want to try, so
here are the changes:
if (KEY_DOWN(VK_SPACE) && activebullet == false)
{
activebullet = true;
bullet1.y = ship1.y;
bullet1.x = ship1.x + 15;
}
// == instead of =, plus this whole while loop outside of
// the if(KEY_DOWN....) check.
while (activebullet == true) // == instead of =
{
bullet1.y = bullet1.y + 4;
Draw_BOB(&bullet1,lpddsback);
DD_Flip();
if (bullet1.y <= 0)
{
activebullet = false;
}
}
here are the changes:
if (KEY_DOWN(VK_SPACE) && activebullet == false)
{
activebullet = true;
bullet1.y = ship1.y;
bullet1.x = ship1.x + 15;
}
// == instead of =, plus this whole while loop outside of
// the if(KEY_DOWN....) check.
while (activebullet == true) // == instead of =
{
bullet1.y = bullet1.y + 4;
Draw_BOB(&bullet1,lpddsback);
DD_Flip();
if (bullet1.y <= 0)
{
activebullet = false;
}
}
Play free Java games at: www.infinitepixels.com
Ok, that worked, but just two more problems with the code.
1) when I fire, a "trail" of bullets go up the screen instead of 1. I mean, one bullet makes a trail that doesn''t clean up after itself
2) When I fire a bullet, I canno move my ship until it has met the end of the screen.. I think this has something to do with the code:
bullet1.y = ship1.y;
bullet1.x = bullet1.x + 17;
thanks for your help.. more is greatly appreciated!
Kirk
1) when I fire, a "trail" of bullets go up the screen instead of 1. I mean, one bullet makes a trail that doesn''t clean up after itself
2) When I fire a bullet, I canno move my ship until it has met the end of the screen.. I think this has something to do with the code:
bullet1.y = ship1.y;
bullet1.x = bullet1.x + 17;
thanks for your help.. more is greatly appreciated!
Kirk
for me..first came QBASIC, then C w/ DJGPP and Allegro, now DirectX and C++.. what a life!!
Try the following:
1) You probably aren''t saving the background underneath the bullet before drawing the bullet. You need to save the background underneath where you''re about to draw the bullet, then draw the bullet over it. When you''re ready to draw the bullet again, you need to replace the background that you overwritten with the bullet with the saved background.
2) Sounds like you might have a some kind of loop (for, while, etc.) that just moves the bullets, draws the bullets, and repeats until the bullet leaves the screen. Here''s the general loop in psuedo code that you might want to try using:
loop until game ends
get inputs
create bullets if necessary
move any created or bullets still on screen in the list
move the player
repeat at get inputs
1) You probably aren''t saving the background underneath the bullet before drawing the bullet. You need to save the background underneath where you''re about to draw the bullet, then draw the bullet over it. When you''re ready to draw the bullet again, you need to replace the background that you overwritten with the bullet with the saved background.
2) Sounds like you might have a some kind of loop (for, while, etc.) that just moves the bullets, draws the bullets, and repeats until the bullet leaves the screen. Here''s the general loop in psuedo code that you might want to try using:
loop until game ends
get inputs
create bullets if necessary
move any created or bullets still on screen in the list
move the player
repeat at get inputs
Just a coment on the previous post. Everything is right, with the exception that if you are drawing the ship n'' everything to the backbuffer (asumption: lpddsback = backbuffer on a complex, primary, flipping surface) the drawing the bullet, flipping, moving the bullet and drawing the bullet again, flipping, moving ... etc Then there''s no reason the backbuffer should keep the info (ship n'' all) after the first flip. I found out that some cards actually copy the backbuffer to the front buffer, so the scheme actually works, but on others that switch pointers to sufaces, you could get some heavy flickering (read: one time the screen has a ship, the other time it hasn''t) what you have to do, is to blit all the background to the backbuffer, draw the moving objects (ship,bullets) then flip, repeat ad nauseum.
There is the alternative of having a second screen size surface, working on it as described in the previous post, then blitting it to the backbuffer and flipping, but apart from necessity to address the surfaces directly (working on sys memory to speed the accesses) then blitting to the backbuffer, i don''t think it''s much used, since it''s slower that the first approach (one more blt, at least, more memory used)
There is the alternative of having a second screen size surface, working on it as described in the previous post, then blitting it to the backbuffer and flipping, but apart from necessity to address the surfaces directly (working on sys memory to speed the accesses) then blitting to the backbuffer, i don''t think it''s much used, since it''s slower that the first approach (one more blt, at least, more memory used)
Blitting, or Bit Block Transfers, are where blocks (chunks) of memory are copied from one place to another. Flipping is where only the addresses of the surfaces are switched.
This means that when you flip, the video card merely switches the address of what it''s showing onscreen (the front buffer) with another piece of memory (the back buffer). Flipping when the monitor is in the middle of drawing is what causes "tearing," (where half of the front buffer it drawn and half of the back buffer). So, when you flip the back buffer becomes the front buffer and vice versa. So use the back buffer however you like after the flip!
Good Luck!
- null_pointer
This means that when you flip, the video card merely switches the address of what it''s showing onscreen (the front buffer) with another piece of memory (the back buffer). Flipping when the monitor is in the middle of drawing is what causes "tearing," (where half of the front buffer it drawn and half of the back buffer). So, when you flip the back buffer becomes the front buffer and vice versa. So use the back buffer however you like after the flip!
Good Luck!
- null_pointer
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement