winform实现人物移动
效果如下:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-85M0QOe1-1615975089140)(C:\Users\admin\AppData\Roaming\Typora\typora-user-images\image-20210317175319074.png)]
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Gluttonous
{
public partial class Tes : Form
{
public Tes()
{
InitializeComponent();
User_Init();
}
void User_Init()//初始化人物
{
img_top.Show();
img_lef.Hide();
img_rig.Hide();
img_ind.Hide();
pic[0] = img_top; pic[1] = img_lef; pic[2] = img_rig; pic[3] = img_ind;
}
int speed = 3;//人物移动速度
int figure_x = 23;//人物的x轴
int figure_y = 287;//人物的y轴
PictureBox[] pic = new PictureBox[4];//存储人物各方位图片
//按下
List<Keys> list = new List<Keys>();
string type = null;
private void Test_KeyDown(object sender, KeyEventArgs e)
{
list.Add(e.KeyCode);
switch (e.KeyCode)
{
case Keys.W:
type = "W";
break;
case Keys.S:
type = "S";
break;
case Keys.A:
type = "A";
break;
case Keys.D:
type = "D";
break;
}
tim_Tick(sender, e);
}
//释放
private void Test_KeyUp(object sender, KeyEventArgs e)
{
tim.Stop();
}
//用做判断人物图片显示和隐藏
void Conceal(ref PictureBox img)
{
for (int i = 0; i < pic.Length; i++)
{
if (img.Name.Equals(pic[i].Name))
{
img.Show();
img.Location = new Point(figure_x, figure_y);
}
else
{
pic[i].Hide();
}
}
}
//刷新:人物基本移动
private void tim_Tick(object sender, EventArgs e)
{
tim.Start();
switch (type)
{
case "W":
figure_y = figure_y - speed;
Conceal(ref img_top);
break;
case "S":
if (figure_y + speed < 309)
{
Conceal(ref img_ind);
figure_y = figure_y + speed;
}
break;
case "D":
figure_x = figure_x + speed;
Conceal(ref img_rig);
break;
case "A":
if (figure_x - speed > 0)
{
figure_x = figure_x - speed;
}
Conceal(ref img_lef);
break;
}
}
private void Test_KeyPress(object sender, KeyPressEventArgs e)
{
}
private void Test_Load(object sender, EventArgs e)
{
}
private void toolStripComboBox1_Click(object sender, EventArgs e)
{
}
}
}
控件代码如下:
namespace Gluttonous
{
partial class Tes
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.tim = new System.Windows.Forms.Timer(this.components);
this.img_ind = new System.Windows.Forms.PictureBox();
this.img_top = new System.Windows.Forms.PictureBox();
this.img_rig = new System.Windows.Forms.PictureBox();
this.img_lef = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.img_ind)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.img_top)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.img_rig)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.img_lef)).BeginInit();
this.SuspendLayout();
//
// tim
//
this.tim.Enabled = true;
this.tim.Interval = 1;
this.tim.Tick += new System.EventHandler(this.tim_Tick);
//
// img_ind
//
this.img_ind.Image = global::Gluttonous.Properties.Resources.mla___副本1;
this.img_ind.Location = new System.Drawing.Point(12, 281);
this.img_ind.Name = "img_ind";
this.img_ind.Size = new System.Drawing.Size(35, 63);
this.img_ind.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.img_ind.TabIndex = 72;
this.img_ind.TabStop = false;
//
// img_top
//
this.img_top.Image = global::Gluttonous.Properties.Resources.mla___副本___副本1;
this.img_top.Location = new System.Drawing.Point(12, 281);
this.img_top.Name = "img_top";
this.img_top.Size = new System.Drawing.Size(35, 63);
this.img_top.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.img_top.TabIndex = 71;
this.img_top.TabStop = false;
//
// img_rig
//
this.img_rig.Image = global::Gluttonous.Properties.Resources.mla___副本___副本2;
this.img_rig.Location = new System.Drawing.Point(3, 242);
this.img_rig.Name = "img_rig";
this.img_rig.Size = new System.Drawing.Size(63, 35);
this.img_rig.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.img_rig.TabIndex = 70;
this.img_rig.TabStop = false;
//
// img_lef
//
this.img_lef.Image = global::Gluttonous.Properties.Resources.mla___副本___副本;
this.img_lef.Location = new System.Drawing.Point(3, 242);
this.img_lef.Name = "img_lef";
this.img_lef.Size = new System.Drawing.Size(63, 35);
this.img_lef.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.img_lef.TabIndex = 69;
this.img_lef.TabStop = false;
//
// Tes
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(702, 372);
this.Controls.Add(this.img_ind);
this.Controls.Add(this.img_top);
this.Controls.Add(this.img_rig);
this.Controls.Add(this.img_lef);
this.Name = "Tes";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Test";
this.Load += new System.EventHandler(this.Test_Load);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Test_KeyDown);
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Test_KeyPress);
this.KeyUp += new System.Windows.Forms.KeyEventHandler(this.Test_KeyUp);
((System.ComponentModel.ISupportInitialize)(this.img_ind)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.img_top)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.img_rig)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.img_lef)).EndInit();
this.ResumeLayout(false);
}
#endregion
public System.Windows.Forms.Timer tim;
private System.Windows.Forms.PictureBox img_lef;
private System.Windows.Forms.PictureBox img_rig;
private System.Windows.Forms.PictureBox img_top;
private System.Windows.Forms.PictureBox img_ind;
}
}