拖拽工具箱里的PictureBox到窗口中
点击小闪电左侧属性里的KeyPreview打开tue
点击小闪电(事件)里的KeyDown双击右侧空白会弹出代码区
将代码复制替换下2“{”至上2“}”
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//人物初始位置
//动画空置状态 0站立 1左脚 2站立 3右脚
public int animation_ctrl = 0;
public int face = 1;
public int x = 0;
public int y = 150;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public Form1()
{
InitializeComponent();
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private void Form1_load(object sendet, EventArgs e)//设置图像
{
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 按钮到点击事件触发程序++++++++++++++++++++++++++++++++++++++++++++++++++
private void DrawHero()
{
animation_ctrl += 1;
Bitmap bitmap1 = new Bitmap(@"r1.png");
Rectangle r1 = new Rectangle(bitmap1.Width / 4 * (animation_ctrl % 4), bitmap1.Height / 4 * (face - 1), bitmap1.Width / 4, bitmap1.Height / 4);//0是x 3参宽度,4参高度
Bitmap bitmapRec = bitmap1.Clone(r1, bitmap1.PixelFormat);//克隆裁剪
bitmap1.SetResolution(700, 700); //分辨率
Graphics g1 = pictureBox1.CreateGraphics(); //通过图形绘制位图(创建一个图形g1)
//创建缓冲区
BufferedGraphicsContext bgc = BufferedGraphicsManager.Current;
//缓冲区图形对象
BufferedGraphics myBuffer = bgc.Allocate(g1, this.DisplayRectangle);
//缓冲区操作图形
Graphics g2 = myBuffer.Graphics;
g2.DrawImage(bitmapRec, x, y);
//显示图像并释放资源
myBuffer.Render();
myBuffer.Dispose();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//通过键位的判断,实现角色的位移
switch (e.KeyCode)
{
case Keys.Left:
x -= 5;
face = 2;
break;
case Keys.Up:
y -= 5;
face = 4;
break;
case Keys.Right:
x += 5;
face = 3;
break;
case Keys.Down:
y += 5;
face = 1;
break;
default:
break;
}
DrawHero();
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
打开本地保存bin的debug文件中
将 该精灵图放置debug文件包中
最后回到设计中运行
运行成功后即可行走
End.