Jump to content
  • Advertisement
Sign in to follow this  
rexTroianus

My first SDL application - need help

This topic is 4776 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

Hello guys, I started learning SDL, and here is what I made today. Source code and .exe are included. What I want to do is when user presses Space key, the bullet is fired, and it 'flies' up to the end of the screen. Check out my code, and tell me what to fix please. Download SDL_demo.zip Thank you, RexTroianus

Share this post


Link to post
Share on other sites
Advertisement
Okay, you're probably going to slap yourself when you see what the problem is [smile].

The problem is that your code draws a vertical line from the place the bullet is shot to the place it ends at y=0. This line is only drawn once the first time the bullet is shot, so you probably won't even see it. The next time the bullet is shot it starts from y=0 and ends at y=0 because you don't reset the starting position of the bullet. So, how can this all be fixed? Easily! [grin]

First we'll change this piece of code:

switch (keyPressed)
{
case SDLK_SPACE:
fire = true;
}



to:

switch (keyPressed)
{
case SDLK_SPACE:
{
if(fire==false)
{
fire = true;
BulletX = batX + batWidth / 2;
BulletY = batY + batHeight /2;
}
}break;
}




What this does is set fire to true and set the start position of the bullet only if the bullet isn't
current being fired.

And now we'll change this code:

if (fire == true)
{
while (BulletY > 0)
{
drawBullet(bullet,
screen,
BulletImageX, BulletImageY,
BulletX, BulletY,
BulletWidth, BulletHeight);
BulletY -= 1;
}

fire = false;
}



to:

if (fire == true)
{
if(BulletY > 0)
{
drawBullet(bullet,
screen,
BulletImageX, BulletImageY,
BulletX, BulletY,
BulletWidth, BulletHeight);
BulletY -= 1;
}
else
{
fire = false;
}
}




This will update the position of the bullet each frame and only draw it in it's new position.
When bulletY is less than 0, fire will be set to false so you can fire the bullet again.

It's not a perfect solution, you can only fire one bullet at a time and you will definitely want to
look in to a time-based movement system.

Share this post


Link to post
Share on other sites
Hehe, /me slaps himself... :)

Thank you very much!

I have one more question. How to load an image and set it as background image, so the sprites move over it?

RexTroianus

Share this post


Link to post
Share on other sites
The simplest way to create a background is just to blit any other image onto your surface before blitting stuff on top of it. Just like you would anything else [wink]

Just make sure that the src image is big enough to cover the screen (SDL doesn't "stretch" images to fit into the rect) and that its blitted before everything else, so everything else shows up on top of it.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!