使一个对象移向另一个对象位置

使一个对象移向另一个对象位置

本文介绍了使一个对象移向另一个对象位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有两个可以通过导航键播放的对象,另一个可以通过wasd播放.关键是要获得第三个对象并得分,然后在捕获到新的pos之后将其随机分配,这是可行的.

What I have currently is two objects that can be played though nav keys and the other one with wasd. The point is to get the 3rd object and score a point, and it randoms a new pos after catching it, which works.

现在...我希望npc2不再受人类控制,并创建一种方法使其可以自行向计分对象移动.我应该怎么做才能做到这一点?我是C#的新手:)

Now... I want the npc2 to not longer be controlled by human, and create a method for it to move on its own TOWARDS the scoring object. How would I possibly do to achieve this? Im new to 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;
            }
        }

    }
}

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

I do understand that it's much to ask for, but i'd be more than thankful if you could help me!

推荐答案

尝试类似的方法

    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中的计时器刻度代码中调用它.

Call it from your timer tick code in Form.cs like this for example.

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

我基于线性代数.例如,看看此视频. (tx,ty)是npc应该沿着的方向.用向量的长度除以得到长度为1的向量.我将其与速度参数相乘,以便您可以调整npc的速度.

I based this on linear algebra. Take a look at this video for example. The (tx,ty) is the vector in which direction the npc should go. Dividing with the length of the vector gives us a vector of length 1. I multiply this with a speed parameter so you can adjust the speed of the npc.

这篇关于使一个对象移向另一个对象位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 18:13