My code will not make use of the tmrMoving(Timer) at all even though the Timer is constantly on with an interval value of 100 so there is no reason I can see but I'm hoping it's just one of those days and it is simple. The code is as follows,
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();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
tmrMoving.Enabled = true;
Invalidate();
}
private void tmrMoving_Tick(object sender, object value, Type targetType,
object parameter, CultureInfo culture, EventArgs e)
{
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);
}
}
}
This link is to my coding in an image format if you prefer it that way.
Aucun commentaire:
Enregistrer un commentaire