以下是一个简单的示例,演示如何使用C#编写点击鼠标移动人物的基本代码。这只是一个基础示例,实际游戏中可能需要更复杂的逻辑和输入处理。
- using System;
- using System.Windows.Forms;
-
- namespace MouseClickMovement
- {
- public partial class MainForm : Form
- {
- private int playerX;
- private int playerY;
-
- public MainForm()
- {
- InitializeComponent();
- playerX = 100; // 初始人物X坐标
- playerY = 100; // 初始人物Y坐标
- }
-
- protected override void OnMouseDown(MouseEventArgs e)
- {
- base.OnMouseDown(e);
-
- // 当鼠标点击时,移动人物到点击的位置
- playerX = e.X;
- playerY = e.Y;
-
- // 重绘界面以更新人物位置
- Invalidate();
- }
-
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
-
- // 绘制人物(这里使用简单的矩形表示)
- e.Graphics.FillRectangle(System.Drawing.Brushes.Blue, playerX, playerY, 20, 20);
- }
-
- [STAThread]
- static void Main()
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- Application.Run(new MainForm());
- }
- }
- }
-
在这个示例中,我们创建了一个简单的WinForms应用程序。当鼠标点击窗口时,人物的位置会更新到鼠标点击的位置,然后我们通过重绘来显示人物的移动。在实际的游戏中,你需要更复杂的逻辑来处理人物的移动、寻路、碰撞检测等。