想实现一个小游戏,先做地图移动。步骤记录如下:

1、百度到一张大的迷宫地图,放在项目的debug目录下,备用。

2、创建一个winform项目,不添加任何界面元素。

3、添加数据成员如下:

        PictureBox pictureBox1;
Bitmap myBitmap;
Bitmap currBitmap;
Point mypoint;

  

分别用于显示图片、存储地图,存储界面上的地图,界面上的地图显示的左上角坐标。

4、设置窗体启动后最大化

public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
}

  5、窗口启动后添加图片和做其他各种预备工作。

        private void Form1_Shown(object sender, EventArgs e)
{
this.MaximumSize = this.Size;
this.MinimumSize = this.Size;
pictureBox1 = new PictureBox();
pictureBox1.Location = new System.Drawing.Point(0, 0);
pictureBox1.Size = this.Size;
this.Controls.Add(pictureBox1); myBitmap = new Bitmap("map.jpeg");
currBitmap = new Bitmap(this.Size.Width, this.Size.Height);
pictureBox1.Image = currBitmap;
mypoint = new Point(0, 0);
var g = Graphics.FromImage(pictureBox1.Image);
g.DrawImage(myBitmap, mypoint);
}

  6、处理asdw按键按下消息,让地图可以移动。

        private void Form1_KeyDown(object sender, KeyEventArgs e)
{
var key = e.KeyValue;
var g = Graphics.FromImage(pictureBox1.Image);
if (key >= 'a' && key <= 'z') key -= ('a' - 'A');
switch(key)
{
case 'A':
mypoint.X -= 10;
if (mypoint.X + myBitmap.Size.Width <currBitmap.Size.Width) mypoint.X = currBitmap.Size.Width-myBitmap.Size.Width;
break;
case 'D':
mypoint.X += 10;
if (mypoint.X>0) mypoint.X = 0;
break;
case 'W':
mypoint.Y -= 10;
if (mypoint.Y + myBitmap.Size.Height < currBitmap.Size.Height) mypoint.Y = currBitmap.Size.Height - myBitmap.Size.Height;
break;
case 'S':
mypoint.Y += 10;
if (mypoint.Y>0) mypoint.Y = 0;
break;
default: break;
}
g.DrawImage(myBitmap, mypoint);
pictureBox1.Refresh();
}

  

完毕。

显示效果如下:

下一步找一些小人加上去。

最新文章

  1. WPF菜单
  2. BAT批量处理 命令
  3. 找到n中最小的k个数
  4. ZOJ 3725 Painting Storages(DP+排列组合)
  5. TCP的三次握手和四次挥手(转)
  6. Vsphere client 无法登陆VCenter 处理的方法
  7. android UI线程安全问题
  8. C#中如何给PDF添加可见的数字签名
  9. 如何严格设置php中session过期时间
  10. python抢火车票的脚本
  11. python利用socketserver实现并发套接字功能
  12. 毕业回馈-89C51之GPIO使用(流水灯)
  13. 俗话:MySQL索引
  14. 帝国cms更新报错解决办法
  15. Python基础:四、python的优缺点
  16. JMeter上架标的(yyb-csg)
  17. Python 练习:使用 * 输出直角三角形
  18. WebStorm: The Smartest JavaScript IDE by JetBrains
  19. linux内核的双链表list_head、散列表hlist_head
  20. 响应式图片 (responsive image)

热门文章

  1. What&#39;s new in Dubbo 3.1.4 and 3.2.0-beta.3
  2. [OpenCV实战]22 使用EigenFaces进行人脸重建
  3. 如何用 Python 隐藏你的 API 密钥
  4. CLISP学习(二)
  5. python之路53 ajax补充返回序列化数据,多对多创建三种方式,django内置序列化组件(drf前身),批量操作数据,自定义分页器,form组件
  6. [C++标准模板库:自修教程与参考手册]关于deque
  7. WeetCode4 —— 二叉树遍历与树型DP
  8. 模块化编程相关知识-引入- 异步加载JS - CommonJS-AMD-CMD-ES6-
  9. vue-admin-template 单页面刷新 404 问题
  10. 2023牛客寒假算法基础集训营4 A-H+JLM