Advertisement

c# drawing sprites

Started by March 16, 2019 12:23 AM
55 comments, last by phil67rpg 5 years, 11 months ago

I think he knows that and now so do you. So, don't draw the black sprite.

hmm...this is how I see it at that point. The way you have it, the draw_flag belongs to the bug. Forget about time. When you want to draw the bug set draw_flag to true. But don't let time decide when you draw your bug. you always draw a bug when it's true and time does not effect it any longer. take it out. 

So how do we make this thing happen? The bullet decides when the bug is drawn? That might be okay...one more bool should do it right? so we add another bool called draw_flag_coll = false to begin with. and draw_flag which belongs to the bug should start out true. right? we see the bug, we don't see the collision image. Try this now and make it how you want it.


private void Form1_Paint(object sender, PaintEventArgs e)
{   // update
	if(y < = -510)
	{
		// todo : do better collision in the other axis, okay I got this
		if( (x > (350 - 30)) && (x < (350 + 30)) ) // hard coding again
		{
			draw_flag_bug = false; // bug will never come back....it's gone for good.
			draw_flag_coll = true; // timer will kill this
		} 
		else
		{
			// advance your bullet here perhaps
			// y--;
		}
	}

	// draw
	e.Graphics.Clear(Color.Black);
	e.Graphics.DrawImage(ship, 350 + x, 530);
	e.Graphics.DrawImage(bullet, 375 + x, 520 + y);

	if(draw_flag_bug == true)
	{
		e.Graphics.DrawImage(bug_one, 350, 0); // don't hard code values
	}

	if(draw_flag_coll == true)
	{
		e.Graphics.DrawImage(coll, 350, 0); // here either
	}
}

private void timer2_Tick(object sender, EventArgs e)
{
	draw_flag_coll = false; // this tick time could be set to 2 seconds or so...

	// this could be better...
	// instead of just turning it off, 
	// we do something better so it stays on the same amount of time
}

Hope that helps.. 

oh, and watch out for that rouge bullet, it's still active somewhere. :P 

Dev careful. Pixel on board.
Buckle up. Everything will be revealed.

well I adjusted your code, when the bullet hits the bug it draws a collision sprite but does not erase it or black it out.


        bool draw_flag_bug = true;
        bool draw_flag_coll = false;
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (y <= -510) 
            {
                if (x >= -15 && x <= 15) 
                {
                    draw_flag_coll = true;
                    draw_flag_bug = false;
                }
                else
                {
                    y--;
                }
                Invalidate();
            }
            e.Graphics.Clear(Color.Black);
            e.Graphics.DrawImage(ship, 350 + x, 530);
            e.Graphics.DrawImage(bullet, 375 + x, 520 + y);
            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);
            }
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
                draw_flag_coll = false;
        }

 

Advertisement

That's because your logic tells it to.  Remove the part that draws the black box and it will work.  

Long term though, I think you need to start over from scratch because there's a lot of nonsense in your draw method.  Just blit the sprite you want to see.  Obviously if you immediately draw a black box over it, or only draw it every 4th frame, you're going to have a bad time. 

 

 

did it work before you adjusted it? did you start your timer?

almost there. you are so close :)

Dev careful. Pixel on board.
Buckle up. Everything will be revealed.

no it did not work before or after I adjusted it, I believe I started it. I agree I am so close.

Phil, now you need to show your latest code and describe the problem you're having now.

-- Tom Sloper -- sloperama.com

Advertisement

well the above code is my latest code, it almost works, the bullet hits the bug but then draws the collision sprite, what I want it to do is draw the collision sprite for a little time then draw the black sprite over it or erase it altogether. 

First, I don't think you want that one call to Invalidate(); inside your paint function.

Second, you don't need to draw a black sprite over your collision sprite and you do not need to erase the collision sprite either. You just need to stop drawing your collision sprite.

If a collision occurs, set a draw flag to true and start counting the number of times you enter the timer function. When that number gets to some value (maybe start with 100 then adjust for duration you want) then set the draw flag to false and reset the count. If the draw flag is set, then draw the collision sprite.

This is the best and simplest advice I can suggest without providing actual code for an example.

Third, I would also like to repeat @GoliathForge's suggestion about keeping notes. I keep a lot of notes in a separate file and I find it useful both in making a plan before I start coding and also when I want to remember why I approached a problem a certain way. I recommend that while you are working, write down what you are doing and why. And find a way to keep your notes organized that makes them the most effective for you.

Good luck on your project.

@kseh is correct.  If you want to stop drawing something, then just stop drawing it.  You do not need to ever draw a black sprite to Erase something, because you're clearing the window on the first line anyway.

He's is also correct in noting that you should not be calling Invalidate in the paint function (that will cause the CPU usage to be very high, in my experience). -- You should instead be calling "this.Invalidate(true);" in your timer method (you do have to call it somewhere, or else the window won't repaint -- so you need to call it every tick).

His logic also sounds correct for what you are describing. -- If you want something to happen for a bit, then start a counter, and only draw the thing you want to draw while that counter is in a given range of values, and then just stop drawing it after that.

I finished reading my book called C# game programming,  hopefully this will help.

This topic is closed to new replies.

Advertisement