Advertisement

c# drawing sprites

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

you are  exactly correct

Advertisement

Looking at the last code you posted, I do see everything there that I suggested but just not quite as I had intended. I was thinking something more like below.


        private void timer2_Tick(object sender, EventArgs e)
        {
            if (y <= -510 && x >= -15 && x <= 15) 
            {
                draw_flag_coll = true;
                count=0;
            }
            
            if(draw_flag_coll==true)
            {
              	count++; 
                if(count >= 100)
                {
                    draw_flag_coll=false;
                }
            }
            this.Invalidate(true);            
        }

Phil, if you understand what's going on in the above code, I'm wondering if you would please describe to us in your own words how you understand it.

 

well kseh your code looks like it works, but it does not, it only draws the collision sprite, I am wondering if the problem is in my paint method. here is my current 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 = 0;
            }
            if (draw_flag_coll == true)
            {
                count++;
                if (count >= 100)
                {
                    draw_flag_coll = false;
                }
            }
            this.Invalidate(true);
        }
    }

 

My mistake. Thank-you for catching that.

Advertisement

well it draws the collision sprite and then after a  period of time it draws a bug and then the collision sprite again.

On ‎3‎/‎26‎/‎2019 at 5:13 PM, Daniel Ricci said:

Okay, so then do that.

1. Detect when there is a collision

2. When a collision occurs call a method that will count down `x` seconds

3. When `x` seconds has been reached, set the flag to false to prevent it from being drawn.

This is a 10 minute fix.

well I coded up the above suggestion, let me know if I am on the right track, also I read the debugger link several times and have worked with my code setting breakpoints and step into and step over features. it is very interesting, here is my code for the above suggestion.


        bool draw_flag_coll = true;
        int count = 10;
        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 (y <= -510 && x >= -15 && x <= 15)
            {
                e.Graphics.DrawImage(coll, 350, 0);
                if (count <= 0)
                {
                     draw_flag_coll = false;
                }
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            y -= 5;
            Invalidate();
        }
        private void timer2_Tick(object sender, EventArgs e)
        {
            count--;
            Invalidate();
        }

 

I don't believe this is going to do what you're wanting it to. However at this point, I believe you have all the basic parts that you need and now it is a matter of you continuing to work at it until it does what you want it to.

This topic is closed to new replies.

Advertisement