项目01【回合制02游戏】Visual C#

这篇博客介绍了如何在Windows Forms应用中使用PictureBox控件结合键盘事件处理程序,实现一个简单的游戏角色移动功能。通过设置KeyPreview属性,监听KeyDown事件,根据按键方向改变角色坐标,并利用缓冲图形技术更新角色图片,从而实现角色在窗口中的行走效果。
摘要由CSDN通过智能技术生成

拖拽工具箱里的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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值