2

我目前拥有的是两个可以通过导航键播放的对象,另一个可以通过 wasd 播放。重点是拿到第3个物体并得分,抓到后随机出一个新的pos,这行得通。

现在......我希望 npc2 不再被人类控制,并创建一个方法让它自己向得分对象移动。我怎么可能做到这一点?我是 C# 的新手 :)

下面的 FORM.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SPEL
{
    public partial class Form1 : Form
    {
        npc npc1 = new npc();
        npc npc2 = new npc();
        sten ste1 = new sten();
        int poang1 = 0;
        int poang2 = 0;
        private _keyControl cc = new _keyControl();

        public Form1()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            InitializeComponent();

            // Hantera tangentbordet
            #region captute evenents
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(cc.addKey);
            this.KeyUp += new System.Windows.Forms.KeyEventHandler(cc.delKey);
            #endregion
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            npc1.Rita(e.Graphics);
            npc2.Rita(e.Graphics);
            ste1.Draw(e.Graphics);



        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            //MessageBox.Show(e.KeyData.ToString());




        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //spelare 1
            this.Text = cc.keyStr;
            if (cc.getKey("Right"))
            {
                npc1.Flytta("Right");
                this.Invalidate();
            }
            if (cc.getKey("Left"))
            {
                npc1.Flytta("Left");
                this.Invalidate();
            }
            if (cc.getKey("Up"))
            {
                npc1.Flytta("Up");
                this.Invalidate();
            }
            if (cc.getKey("Down"))
            {
                npc1.Flytta("Down");
                this.Invalidate();
            }
                //Spelare 2
                if (cc.getKey("D"))
                {
                    npc2.Flytta("Right");
                    this.Invalidate();
                }
                if (cc.getKey("A"))
                {
                    npc2.Flytta("Left");
                    this.Invalidate();
                }
                if (cc.getKey("W"))
                {
                    npc2.Flytta("Up");
                    this.Invalidate();
                }
                if (cc.getKey("S"))
                {
                    npc2.Flytta("Down");
                    this.Invalidate();
                }

            if (npc1.checkkoll().IntersectsWith(ste1.checkkoll()))
            {
                poang1++;
                ste1.randomxy(this.Width -30, this.Height -30);


            }
            if (npc2.checkkoll().IntersectsWith(ste1.checkkoll()))
            {
                poang2++;
                ste1.randomxy(this.Width -30, this.Height -30);
            }


        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (this.BackColor == Color.Red)
                this.BackColor = Color.Blue;
            else
                this.BackColor = Color.Red;
        }

        private void flytta_Tick(object sender, EventArgs e)
        {


            }
        }
    }
}

这是我的运动课

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace SPEL
{
    class npc
    {
        private Bitmap strid;
        private Point pt;
        public npc()
        {
            strid = new Bitmap("hej.gif");

            pt.X = 20;
            pt.Y = 20;

        }

        public void Rita(Graphics f)
        {
            Rectangle re = new Rectangle(pt.X, pt.Y, strid.Width, strid.Height);
            f.DrawImage(strid, re);
        }

        public Rectangle checkkoll()
        {
            Rectangle re = new Rectangle(pt.X, pt.Y, strid.Width, strid.Height);
            return re;
        }

        public void Flytta(string dir)
        {

            if (dir == "Left")
            {
                pt.X = pt.X - 2;
            }
            if (dir == "Right")
            {
                pt.X = pt.X + 2;
            }
            if (dir == "Up")
            {
                pt.Y = pt.Y - 2;
            }
            if (dir == "Down")
            {
                pt.Y = pt.Y + 2;
            }
        }

    }
}

我确实知道要求很多,但如果您能帮助我,我将不胜感激!

4

2 回答 2

2

尝试这样的事情

    public void FlyttaMot(int x, int y, float speed)
    {
        float tx = x - pt.X;
        float ty = y - pt.Y;
        float length = Math.Sqrt(tx*tx + ty*ty);
        if (length > speed)
        {
            // move towards the goal
            pt.X = (int)(pt.X + speed* tx/length);
            pt.Y = (int)(pt.Y + speed* ty/length);
        }
        else
        {
            // already there
            pt.X = x;
            pt.Y = y;
        }
    }

例如,从您的计时器滴答代码中调用它Form.cs

npc2.FlyttaMot(200, 200, 0.5f);

我基于线性代数。例如,看看这个视频。是npc 应该去的方向(tx,ty)向量。除以向量的长度,我们得到一个长度为 1 的向量。我将它与速度参数相乘,这样你就可以调整 npc 的速度。

于 2012-11-12T14:57:19.770 回答
0

最简单、最准确的方法,就是依靠矢量几何。

定义类

public class Vector2 {

     public float x {get;set;}; 
     public float y {get;set;}; 

     public Vector2(float a_x, float a_y, float b_x, float b_y)
     {
        x = b_x - a_x;
        y = b_y - a_y;
     }

     //calculate vector length
     public float Length {
          get {
               return Math.Sqrt((x * x) + (y * y));
          }
     }

     public void SetVectorLength(float desiredLength){
            double r = desiredLength/ this.Length;
            this.x *= r;
            this.y *= r;
     }

}

这只是这种情况下的一组函数,但您可以添加其他函数,其中很多函数需要您的计算的各个部分。

在你要做的只是,计算你的顶点朝向方向向量的下一个位置。

//hypothetical X coordinate
float x_coord = 1.34f; 

//move towards the vector 
var vector = new Vector2(10, 10, 20, 20);

//set vector move step 
vector.SetLength(0.1f);

var movedX = x_coord + vector.x;

重复:这只是假设的示例,您需要对此进行处理。很难将所有相关的矢量几何东西都放在小答案中。

于 2012-11-12T14:41:32.313 回答