【发布时间】:2018-07-29 08:38:35
【问题描述】:
我已经开始了一个新项目,并决定先完成程序中最难的部分,当我说最难时,我指的是琐碎的部分。
下面的代码让我的播放器跟随鼠标光标,但移动是恒定的,播放器的位置是相对于光标在屏幕上的位置而不是实际的程序窗口。窗口越靠近屏幕的上角光标和播放器越接近。
我正在寻求帮助来纠正这个问题,以便玩家的位置以一定的速度移动到光标处,但实际上跟随指针。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace games1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Form1_Click(object sender, MouseEventArgs e)
{
Invalidate();
}
private void tmrMoving_Tick_1(object sender, EventArgs e)
{
if (tmrMoving.Enabled == true)
{
var cursPoint = new System.Drawing.Point(Cursor.Position.X, Cursor.Position.Y);
var playerPoint = new System.Drawing.Point(player.Location.X, player.Location.Y);
var diff = new Point(Cursor.Position.X - playerPoint.X, Cursor.Position.Y - playerPoint.Y);
var speed = Math.Sqrt(diff.X * diff.X + diff.Y * diff.Y);
if (speed > 10)
{
diff.X /= (int)(speed / 10);
diff.Y /= (int)(speed / 10);
}
player.Location = new System.Drawing.Point(player.Location.X + diff.X, player.Location.Y + diff.Y);
}
}
}
}
【问题讨论】: