无法使物体碰撞

时间:2018-08-18 18:49:52

标签: c# winforms inheritance collision bounding-box

对于我的一生,我无法弄清楚自己在做错什么。我设法使一个球员,并使其射击。我还设法使图片框中的球以随机的位置/速度产生。

我无法获得任何Collision的帮助。我试图将所有球都留在图片框内,然后反弹并相互反弹,然后在射击时销毁它们。

我一直在尝试使用Ball.Bounds.IntersectsWith(pictureBox2.Bounds),但似乎Ball.bounds是错误的。

我有一个模糊的想法,为什么它是错误的但不是确定的。我在box.cs中的财产范围错误,或者我不知道如何将其与我所有的球的位置相关。

由于我在这个问题上已经停留了一段时间,所以朝正确的方向稍加推动将不胜感激。


ball.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shootyarcher
{
    class Ball : Box
    {
        public string bounds { get; set; }

        static private Random generator = new Random();
        private int countdown;

        public Ball ()
        {
            pic = Properties.Resources.Balloon;
            x = generator.Next(60, 1920);
            y = generator.Next(100, 1000);
            xspeed = generator.Next(-10, 4);
            yspeed = generator.Next(-10, 4);
            countdown = generator.Next(100, 200);
        }
        public new void Move() 
        {
            countdown--;
            if (countdown == 0)
            {
                xspeed = generator.Next(-4, 4);
                yspeed = generator.Next(-4, 4);
                countdown = generator.Next(20, 40);
            }
            x = x + xspeed;
            y = y + yspeed;
        }
    }
}

box.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing; //pictures

namespace shootyarcher
{
    class Box
    {
        public float x;
        public float y;
        public float w;
        public float h;
        public float xspeed;
        public float yspeed;
        public Image pic;

        public Box() // constructor
        {
            x = 0;
            y = 0;
            xspeed = 0;
            yspeed = 0;
        }

        public void Move()
        {
            x = x + xspeed;
            y = y + yspeed;
        }

        public void Draw(Graphics g)
        {
            g.DrawImage (pic, x, y);
        }

        public float Width()
        {
            return pic.Width;
        }

        public float Height()
        {
            return pic.Height;
        }

        public float Left()
        {
            return x;
        }

        public float Right()
        {
            return x + Width();
        }

        public float Top()
        {
            return y;
        }

        public float Bottom()
        {
            return y + Height();
        }
    }
}

Form1.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace shootyarcher
{
    public partial class Form1 : Form
    {
        Archer player;
        List<Arrow> Shiv;
        List<Ball> Kill;

        public Form1()
        {
            InitializeComponent();

            Cursor.Hide();

            this.FormBorderStyle = FormBorderStyle.None;
            this.TopMost = true;
            this.Bounds = Screen.PrimaryScreen.Bounds;


            player = new Archer(0, 0);
            Shiv = new List<Arrow>();
            Kill = new List<Ball>();
            for (int i = 0; i < 400; i++)
            {
                Ball temp = new Ball();
                Kill.Add(temp);
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            player.Move();
            foreach (Arrow t in Shiv)
            {
                t.Move();
            }
            foreach (Ball m in Kill)
            {
                m.Move();
            }

            pictureBox1.Invalidate();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.W)
            {
                player.moveup = true;
            }
            if (e.KeyCode == Keys.S)
            {
                player.movedown = true;
            }
            if (e.KeyCode == Keys.Space)
            {
                Arrow temp = new Arrow(player.x, player.y);
                Shiv.Add(temp);
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.W)
            {
                player.moveup = false;
            }
            if (e.KeyCode == Keys.S)
            {
                player.movedown = false;
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            player.Draw(e.Graphics);
            foreach (Arrow t in Shiv)
            {
                t.Draw(e.Graphics);
            }
            foreach (Ball m in Kill)
            {
                m.Draw(e.Graphics);
            }
        }
    }
}

0 个答案:

没有答案