1
\$\begingroup\$

I am able to draw my space ship and a an enemy (called "bug_one" in the code below) and move my bullet. When the bullet hits the enemy, it draws a collision bmp.

But currently my code draws both the collision bmp and the enemy bmp after the enemy got hit.

What I want instead is that after the bullet hits the enemy, to no longer draw the enemy bmp and draw the collision bmp instead. I'd like also the collision bmp to disappear after some time. here is my reworked code

        bool draw_flag_coll = false;
    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.Clear(Color.Black);
        e.Graphics.DrawImage(ship, 350 + x, 530);
        e.Graphics.DrawImage(bullet, 375 + x, 520 + y);
        e.Graphics.DrawImage(bug_one, 350, 0);
        if(draw_flag_coll==true)
        {
            e.Graphics.DrawImage(coll, 350, 0);
        }
    }
    int count = 0;
    private void timer2_Tick(object sender, EventArgs e)
    {
        if (y <= -510 && x >= -15 && x <= 15) 
        {
            draw_flag_coll = true;
            count++;
            if(count == 100)
            {
                this.Invalidate(true);
                count = 0;
            }
        }
    }
\$\endgroup\$
  • 1
    \$\begingroup\$ Hello. What seems to be the problem? Please add some more details, so that we can help. \$\endgroup\$ – Evorlor Mar 9 '19 at 2:24
  • \$\begingroup\$ Your question doesn't really explain what your problem is. So I guessed it and edited the question so it can be reopened. Please verify that my guess is correct. \$\endgroup\$ – Philipp Mar 9 '19 at 9:40
  • \$\begingroup\$ yes your guess is correct \$\endgroup\$ – gamer67 Mar 9 '19 at 21:48
  • \$\begingroup\$ well my problem is that when I shoot a bullet it hits the bug and draws a collision sprite what I want is that when the collision sprite is drawn it is only drawn for a brief period of time and then the black sprite is drawn over it essentially erasing the collision sprite. \$\endgroup\$ – gamer67 Mar 11 '19 at 0:30
  • \$\begingroup\$ I see you have a timer related function in there, did you consider using a timer to draw an object only for a period of time? E.g. draw the thing and start a timer, then remove the thing and swap it for something else once the the time is up? \$\endgroup\$ – Vaillancourt Mar 23 '19 at 23:50
1
\$\begingroup\$

Well the solution can be quite simple, you already used it to draw the collision image when the bullet hits. This is one approach, just do everything with if else cases, its not the best way to go though. For example:

if(draw_flag_coll==true){
   e.Graphics.DrawImage(coll, 350, 0);
}else{
   e.Graphics.DrawImage(ship, 350 + x, 530);
}

For this solution you would need to delete the line e.Graphics.DrawImage(ship, 350 + x, 530); above and place it inside the 'else' part of your if/else structure.

This has 2 downsides. First of all your parameter values for the Coll image call are hard coded, it would always be drawn on the same spot. The other downside is the concept itself. Imagine you will add more animations (turning, casting shield etc). You would need many if/else structures, stacked into each other and so on.

I think a better approach is giving the ship an animation Object (current Animation) this animation has a timer controlling /looping the animation. you would always update this one animation, but switch it with different animations as you need. This involves just little work but grants high reusability and good code overview. You would at least need one extra class (animation) that describes how an animation works with different variables like number of frames, eventually adjustable, a texture image object (sprite sheet) which represents the used images, methods to start the animation, adjust time of a timer etc.

Then you could set the players current animation to an animation instance which you instantiate with your ship image. Upon game start you prepare an array of other animations with their corresponding images. When collision occurs you could do something like:

player.setCurrentAnimation(collisonAnimation);

still you would have to figure out how to return to an other animation when the currently one is not looped (like collision animation). But it should not be hard and there are many ways to do so.

\$\endgroup\$
0
\$\begingroup\$

Sorry just joined so I can't post as a comment.

You error most likely lies outside of the code you posted. You are using an if-else statement, so none of these three images can be drawn at the same time.

    if (draw_flag_bug == true) 
    {
        e.Graphics.DrawImage(bug_one, 350, 0);
    }
    else if (draw_flag_coll == true) 
    {
        e.Graphics.DrawImage(coll, 350, 0);
    }
    else if(draw_flag_bug == false)
    {
        e.Graphics.DrawImage(black, 350, 0);
    }

My best bet is your error comes from having multiple instances of the object. Try to print out the value of draw_flag_bug just before e.Graphics.Clear(Color.Black); I think you might find that both values will be printed.

Could you please provide some more code, otherwise it is hard to pinpoint the exact issue.

\$\endgroup\$
  • \$\begingroup\$ @gamer67 could you provide the code for the entire class? \$\endgroup\$ – Jannek Mar 24 '19 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.