版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、游戏所需图片: 设计游戏窗体、在Windows项目下添加一个图片控件picturebox1(游戏面板),1个菜单控件menustrip1属性设置如下表所示代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;namespace 推箱子游戏 public partial class Form1 : F
2、orm private int x; /工人当前位置(x,y) private int y; /private bool flag = true; /0代表墙,1代表人,2代表箱子,3代表路,4代表目的地 /5代表人在目的地,6代表放到目的地的箱子 private enum Map_State None=-1,Wall = 0, Worker, Box, Passageway, Destination,WorkerInDest,RedBox ; private Map_State, myArray; /private int, my; private int Order = 1; /游戏关的序
3、号 public Form1() InitializeComponent(); private void ReadMap(int n) string filename = "mapmap_" + n.ToString() + ".info" FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); BinaryReader r = new BinaryReader (fs); /读取数据 for (int i = 0; i < 7; i+) for (int
4、j = 0; j < 7; j+) myArrayi, j=(Map_State) r.ReadByte(); for (int i = 0; i < 7; i+) for (int j = 0; j < 7; j+) if(myArrayi, j =Map_State.Worker) x=i;y=j; r.Close(); fs.Close(); public void initdata() myArray = new Map_State7, 7; ReadMap(Order); private void Form1_Load(object sender, System.E
5、ventArgs e) initdata(); drawimage(); /绘制整个游戏区域图形 private void drawimage() Bitmap bit =new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height); Graphics g= Graphics.FromImage(bit); SolidBrush redBrush = new SolidBrush(Color.Red); System.Drawing.Image image=new Bitmap("worker.gif"); for(
6、int i =0;i<7;i+) for(int j=0;j<7;j+) if (myArrayi, j = Map_State.Wall) image = new Bitmap("wall.gif"); g.DrawImage(image,i*50,j*50,50,50); if (myArrayi, j = Map_State.Worker ) image = new Bitmap("worker.gif"); g.DrawImage(image,i*50,j*50,50,50); if (myArrayi, j = Map_State.
7、Box ) image=new Bitmap("box.gif"); g.DrawImage(image,i*50,j*50,50,50); if(myArrayi,j= Map_State.Passageway) image = new Bitmap("passageway.gif"); g.DrawImage(image,i*50,j*50,50,50); if(myArrayi,j= Map_State.Destination) image = new Bitmap("destination.gif"); g.DrawImage
8、(image,i*50,j*50,50,50); if (myArrayi, j = Map_State.WorkerInDest) image = new Bitmap("worker.gif"); g.DrawImage(image, i * 50, j * 50, 50, 50); if (myArrayi, j = Map_State.RedBox) image = new Bitmap("redbox.gif"); g.DrawImage(image, i * 50, j * 50, 50, 50); this.pictureBox1.Imag
9、e=bit; private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) int x1, y1, x2, y2; /工人当前位置(x,y) switch (e.KeyCode)/分析按键消息 /向上 case Keys.Up: x1 = x; y1 = y - 1; x2 = x; y2 = y - 2; /将所有位置输入以判断并作地图更新 MoveTo(x1, y1, x2, y2); break; /向下 case Keys.Down: x1 = x; y1 = y + 1; x2 = x;
10、y2 = y + 2; MoveTo(x1, y1, x2, y2); break; /向左 case Keys.Left : x1 = x - 1; y1 = y; x2 = x - 2; y2 = y; MoveTo(x1, y1, x2, y2); break; /向右 case Keys.Right: x1 = x + 1; y1 = y; x2 = x + 2; y2 = y; MoveTo(x1, y1, x2, y2); break; case Keys.Space:/空格键 重玩ToolStripMenuItem_Click(null, null); break; privat
11、e void MoveMan(int x, int y) if (myArrayx, y = Map_State.Worker) myArrayx, y = Map_State.Passageway; else if (myArrayx, y = Map_State.WorkerInDest) myArrayx, y = Map_State.Destination; private void MoveTo(int x1, int y1, int x2, int y2) Map_State P1,P2; P1 = P2 = Map_State.None; if(IsInGameArea(x1,
12、y1) /判断是否在游戏区域 P1=myArrayx1, y1; if (IsInGameArea(x2, y2) P2 = myArrayx2, y2; if (P1 = Map_State.Passageway)/P1处为通道 MoveMan(x,y); x = x1; y = y1; myArrayx1, y1 = Map_State.Worker; if (P1 = Map_State.Destination)/P1处为目的地 MoveMan(x, y); x = x1; y = y1; myArrayx1, y1 = Map_State.WorkerInDest; if (P1 =
13、Map_State.Wall | !IsInGameArea(x1, y1) /P1处为墙或出界 return; if (P1 = Map_State.Box )/P1处为箱子 if (P2 = Map_State.Wall |!IsInGameArea(x1, y1) | P2 = Map_State.Box)/P2处为墙或出界 return; /以下P1处为箱子 /P1处为箱子,P2处为通道 if (P1 = Map_State.Box && P2 = Map_State.Passageway) MoveMan(x, y); x = x1; y = y1; myArrayx
14、2,y2=Map_State.Box; myArrayx1, y1 = Map_State.Worker; if (P1 = Map_State.Box && P2 = Map_State.Destination) MoveMan(x, y); x = x1; y = y1; myArrayx2,y2=Map_State.RedBox; myArrayx1, y1 = Map_State.Worker; /P1处为放到目的地的箱子,P2处为通道 if (P1 = Map_State.RedBox && P2 = Map_State.Passageway) Mov
15、eMan(x, y); x = x1; y = y1; myArrayx2, y2 = Map_State.Box; myArrayx1, y1 = Map_State.WorkerInDest; /P1处为放到目的地的箱子,P2处为目的地 if (P1 = Map_State.RedBox && P2 = Map_State.Destination) MoveMan(x, y); x = x1; y = y1; myArrayx2, y2 = Map_State.RedBox; myArrayx1, y1 = Map_State.WorkerInDest; drawimage
16、(); /这里要验证是否过关 if (IsFinish() MessageBox.Show("恭喜你顺利过关","提示"); 下一关ToolStripMenuItem_Click(null, null); return; /判断是否在游戏区域 private bool IsInGameArea(int row, int col) return (row >= 0 && row < 7 && col >= 0 && col < 7); public bool IsFinish()/验证
17、是否过关 bool bFinish = true; for (int i = 0; i < 7; i+) for (int j = 0; j < 7; j+) if (myArrayi,j = Map_State.Destination | myArrayi,j = Map_State.WorkerInDest) bFinish = false; return bFinish; private void 编辑地图ToolStripMenuItem_Click(object sender, EventArgs e) FrmConfig f2 = new FrmConfig(); f2
18、.ShowDialog(); private void 重玩ToolStripMenuItem_Click(object sender, EventArgs e) this.Text = "第" + Order.ToString() + "关" initdata(); drawimage(); private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) Application.Exit(); private void 下一关ToolStripMenuItem_Click(object send
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 出差考勤制度
- 厂办办公室考勤制度
- 日化产品公司各分公司经理招聘全攻略
- 体育场馆电力与燃料供应管理主管面经
- 人大常委会考勤制度
- 学校各部门考勤制度
- 城管队伍考勤制度
- 广西壮族自治区河池市都安瑶族自治县2025-2026学年八年级上学期期末地理试题(无答案)
- 安徽亳州市蒙城县2025-2026学年七年级上学期期末地理试题(含答案)
- 小学生四年级考勤制度
- 融媒体中心内控制度
- 2026年消防设施操作员之消防设备基础知识考试题库500道及完整答案(各地真题)
- 2026年广西普高生单招文化素质提分题库含答案3个月冲刺计划适配
- (2026年)护理学会老年人误吸的预防护理团标解读课件
- 2025岩土工程勘察测量行业市场现状研究投资评估规划分析
- ICU患者体位的管理
- 黑钨矿选矿工艺流程图及设备
- 玻璃幕墙施工风险辨识和分析及应对措施
- 2025年高等自学教育考试马克思主义基本原理概论全真模拟试卷及答案(共七套)
- 汽轮机本体结构
- 国家电力监理安全培训课件
评论
0/150
提交评论