当两个图片框之间的碰撞加上随机丢弃的图片框

时间:2017-11-15 21:04:21

标签: c#

我编写了一个非常简单的程序。

一个pictureBox从上面掉落而另一个pictureBox(位于按钮)必须抓住pictureBox(这两个pictureBoxes之间的碰撞),然后你得到一个点。 我的问题是当碰撞发生时,只要两个图片框之间发生碰撞,Point就会计数。

如何阻止这种情况并且只在发生碰撞时计算++? - 我是新手编码c#所以请不要回答非常高级的答案:)

namespace Animation
{
    public partial class Form1 : Form
    {
        Point p1 = new Point();
        Point p2 = new Point();
        int bredde;
        int højde;
        int score;
        int count = 0;

        public Form1()
        {
            InitializeComponent();
            p1.X = pictureBox1.Location.X;
            p1.Y = pictureBox1.Location.Y;
            p2.X = pictureBox2.Location.X;
            p2.Y = pictureBox2.Location.Y;
            højde = ClientSize.Height;
            bredde = ClientSize.Width;
        }

        Boolean max = true;

        void collision()
        {

            if (pictureBox1.Bounds.IntersectsWith(pictureBox2.Bounds)) 
            {
                score++;
                label1.Text = Convert.ToString(score);
                max = true;
            }


        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.W && p1.Y > 0)
                p1.Y -= 3;

            if (e.KeyData == Keys.D && p1.X + pictureBox1.Width < bredde)
                p1.X +=3;


            if (e.KeyData == Keys.A && p1.X > 0)
                p1.X -=3;


            if (e.KeyData == Keys.S && p1.Y + pictureBox1.Height < højde)
                p1.Y +=3;

            pictureBox1.Location = p1;


            if (e.KeyData == Keys.W && p2.Y > 0)


            if (e.KeyData == Keys.D && p2.X + pictureBox2.Width < bredde)


            if (e.KeyData == Keys.A && p2.X > 0)


            if (e.KeyData == Keys.S && p2.Y + pictureBox2.Height < højde)

            pictureBox2.Location = p2; 
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            collision();
            p2.Y +=5;
            pictureBox2.Location = p2;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }
    }
}

0 个答案:

没有答案