基本介绍
游戏界面
准备工作
修改图标
添加MenuStripe
设置属性 添加快捷键(ShortCutKeys)
添加Click事件
添加三个label及其相关click事件
主要代码
//枚举小人运动方向
public enum Direction
{
Up = 1, Down = 2, Left = 3, Right = 4
}
//小人初始默认方向
public Direction _direction = Direction.Down;
//初始关卡为1
int leval = 1;
//游戏状态
int[,] num = new int[10, 10];//初始化数组,用于画map
public const int height = 50, width = 50;//墙的长宽
int row, col;//小人的位置
int x, y, nx, ny;
int bitmap;
//开启窗体获取键盘事件的焦点
this.KeyPreview = true;
命名空间:using System.Windows.Forms;
窗口接收所有键盘事件,则为true,默认为false
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
Move(Direction.Up);
else if (e.KeyCode == Keys.Down)
Move(Direction.Down);
else if (e.KeyCode == Keys.Left)
Move(Direction.Left);
else if (e.KeyCode == Keys.Right)
Move(Direction.Right);
}
//初始化数组
public void initnum()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
num[i, j] = 0;
if (i == 0 || j == 0 || i == 9 || j == 9)
num[i, j] = 1;
}
}
switch (leval)//根据等级变化地图
{
case 1:
{
num[1, 2] = num[3, 3] = num[4, 6] = num[5, 6] = num[5, 7] = num[5, 4] = num[2, 5] = num[7, 2] = num[6, 2] = num[6, 4] = num[7, 5] = num[7, 6] = num[6, 1] = num[7, 1] = num[8, 1] = num[6, 2] = num[7, 2] = num[8, 2] = 1;
num[3, 5] = 4;
num[3, 2] = num[2, 4] = 2;
num[3, 7] = num[6, 5] = 3;
break;
}
case 2:
{
num[1, 3] = num[4, 3] = num[4, 7] = num[2, 7] = num[5, 6] = num[2, 5] = num[2, 6] = num[6, 1] = num[7, 1] = num[8, 1] = num[6, 2] = num[7, 2] = num[8, 2] = num[3, 3] = num[6, 8] = num[7, 8] = num[8, 8] = num[7, 7] = num[8, 7] = num[6, 4] = num[7, 4] = num[1, 4] = 1;
num[2, 1] = 4;
num[3, 2] = num[2, 2] = 2;
num[1, 8] = num[3, 7] = 3;
break;
}
case 3:
{
num[1, 8] = num[2, 8] = num[2, 9] = num[1, 1] = num[3, 6] = num[2, 6] = num[2, 5] = num[4, 6] = num[5, 6] = num[3, 3] = num[5, 2] = num[1, 3] = num[4, 5] = num[7, 4] = num[7, 5] = num[7, 6] = 1;
num[3, 5] = 4;
num[3, 2] = num[4, 3] = 2;
num[1, 7] = num[2, 7] = 3;
break;
}
case 4:
{
num[1, 8] = num[2, 8] = num[2, 9] = num[1, 1] = num[2, 6] = num[2, 5] = num[4, 6] = num[5, 6] = num[3, 3] = num[5, 2] = num[1, 3] = num[4, 5] = 1;
num[7, 4] = num[7, 5] = num[7, 6] = 0;
num[5, 5] = 4;
num[4, 2] = num[6, 2] = 2;
num[4, 7] = num[3, 6] = 3;
break;
}
case 5:
{
num[2, 3] = num[1, 5] = num[3, 4] = num[4, 4] = num[4, 6] = num[5, 6] = num[7, 6] = num[5, 2] = num[6, 2] = num[6, 3] = num[8, 6] = num[4, 2] = num[6, 5] = 1;
num[3, 7] = num[7, 6] = 2;
num[1, 1] = num[8, 2] = 3;
num[1, 2] = 4;
break;
}
case 6:
{
num[3, 5] = 4;
num[5, 6] = num[5, 7] = num[5, 8] = num[5, 4] = num[3, 3] = num[4, 3] = num[7, 5] = num[8, 5] = num[6, 2] = num[7, 7] = 1;
num[6, 6] = num[4, 7] = 2;
num[7, 2] = num[3, 4] = 3;
break;
}
case 7:
{
num[1, 8] = num[2, 8] = num[2, 9] = num[1, 1] = num[3, 6] = num[2, 6] = num[2, 5] = num[4, 6] = num[5, 6] = num[3, 3] = num[5, 2] = num[1, 3] = num[4, 5] = num[7, 4] = num[7, 5] = num[7, 6] = num[7, 3] = num[7, 2] = num[7, 8] = num[5, 1] = num[5, 5] = 1;
num[4, 2] = 4;
num[3, 2] = num[4, 3] = num[5, 4] = 2;
num[1, 7] = num[3, 7] = num[8, 8] = 3;
break;
}
}
}
rivate void view()
{
Bitmap bmp = new Bitmap(pictureBoxView.Width, pictureBoxView.Height);
label1.Text = "第 " + leval + " 关";//显示第几关
Bitmap bitmap;//用于装载图片
Graphics g = Graphics.FromImage(bmp);//
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
bitmap = new Bitmap("Images\\floor.gif");
if (num[i, j] == 0)
{
bitmap = new Bitmap("Images\\floor.gif");
g.DrawImage(bitmap, i * 50, j * 50, 50, 50);
}
else if (num[i, j] == 1)
{
bitmap = new Bitmap("Images\\zhuantou.gif");
g.DrawImage(bitmap, i * 50, j * 50, 50, 50);
}
else if (num[i, j] == 2)
{
bitmap = new Bitmap("Images\\box.gif");
g.DrawImage(bitmap, i * 50, j * 50, 50, 50);
}
else if (num[i, j] == 3)
{
bitmap = new Bitmap("Images\\goal.gif");
g.DrawImage(bitmap, i * 50, j * 50, 50, 50);
}
else if (num[i, j] == 4)
{
if (_direction == Direction.Up)
{
bitmap = new Bitmap("Images\\up.gif");
bitmap.MakeTransparent(Color.White);
g.DrawImage(bitmap, i * 50, j * 50, 50, 50);
}
else if (_direction == Direction.Down)
{
bitmap = new Bitmap("Images\\down.gif");
bitmap.MakeTransparent(Color.White);
g.DrawImage(bitmap, i * 50, j * 50, 50, 50);
}
else if (_direction == Direction.Left)
{
bitmap = new Bitmap("Images\\left.gif");
bitmap.MakeTransparent(Color.White);
g.DrawImage(bitmap, i * 50, j * 50, 50, 50);
}
else
{
bitmap = new Bitmap("Images\\right.gif");
bitmap.MakeTransparent(Color.White);
g.DrawImage(bitmap, i * 50, j * 50, 50, 50);
}
row = i;
col = j;
}
else if (num[i, j] == 5)
{
bitmap = new Bitmap("Images\\on.gif");//人在goal上
g.DrawImage(bitmap, i * 50, j * 50, 50, 50);
}
else if (num[i, j] == 6)
{
bitmap = new Bitmap("Images\\getgoal.gif");//箱子已推到目标地方
g.DrawImage(bitmap, i * 50, j * 50, 50, 50);
}
}
pictureBoxView.Image = bmp;
}
}
点击进入下一关
private void labelLeval_Click(object sender, EventArgs e)
{
leval++;
if (leval == 8)
{
DialogResult result2 = MessageBox.Show("当前已是最后一关!!是否重新开始", "(p≧w≦q)", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result2 == DialogResult.OK)
{
num = new int[10, 10];
leval = 1;
}
}
_direction = Direction.Down;
num = new int[10, 10];
initnum();
view();
}
MessageBox相关用法:MessageBox.Show(<字符串> Text, <字符串> Title, <整型> nType,MessageBoxIcon);
第一个表示提示框里面的 内容;
第二个表示提示框的 标题;
第三个表示消息框的 类型 ;
第四个是提示框的 图标 ,它的几种图标样式如下:
MessageBoxIcon.Question
MessageBoxIcon.Asteris
MessageBoxIcon.Information
MessageBoxIcon.Error
MessageBoxIcon.Stop
MessageBoxIcon.Hand
MessageBoxIcon.Exclamation
MessageBoxIcon.Warning
MessageBoxIcon.None
//小人移动,x,y为当前下一步走到的位置,nx,ny为当前下两步走到的位置
public void Move(Direction direction)
{
int x = y = nx = ny = 0;
//保存运动方向
_direction = direction;
//如果向上运动
if (_direction == Direction.Left)
{
x = row - 1;
y = col;
nx = row - 2;
ny = col;
}
//如果向下运动
if (_direction == Direction.Down)
{
x = row;
y = col + 1;
nx = row;
ny = col + 2;
}
//如果向左运动
if (_direction == Direction.Up)
{
x = row;
y = col - 1;
nx = row;
ny = col - 2;
}
//如果向右运动
if (_direction == Direction.Right)
{
x = row + 1;
y = col;
nx = row + 2;
ny = col;
}
PlayerMove(x, y, nx, ny);
view();
Win();
}
public void PlayerMove(int x, int y, int nx, int ny)
{
int next1 = num[x, y];
if (next1 == 1)
return;
int next2 = num[nx, ny];
if (next1 == 0)
{
num[x, y] = 4;
if (num[row, col] == 4)//人在平地上走
{
num[row, col] = 0;
}
if (num[row, col] == 5)//人在goal上
{
num[row, col] = 3;
}
}
else if (next1 == 3)
{
num[x, y] = 5;
if (num[row, col] == 4)//人在平地上走
{
num[row, col] = 0;
}
if (num[row, col] == 5)//人在goal上
{
num[row, col] = 3;
}
}
else if (next1 == 2 && next2 == 0)
{
num[nx, ny] = 2;
num[x, y] = 4;
if (num[row, col] == 4)//人在平地上走
num[row, col] = 0;
if (num[row, col] == 5)//人在goal上
num[row, col] = 3;
}
else if (next1 == 2 && next2 == 3)
{
num[nx, ny] = 6;
num[x, y] = 4;
if (num[row, col] == 4)//人在平地上走
num[row, col] = 0;
if (num[row, col] == 5)//人在goal上
num[row, col] = 3;
}
else if (next1 == 6 && next2 == 0)
{
num[nx, ny] = 2;
num[x, y] = 5;
if (num[row, col] == 4)//人在平地上走
num[row, col] = 0;
if (num[row, col] == 5)//人在goal上
num[row, col] = 3;
}
else if (next1 == 6 && next2 == 3)
{
num[nx, ny] = 6;
num[x, y] = 5;
if (num[row, col] == 4)//人在平地上走
num[row, col] = 0;
if (num[row, col] == 5)//人在goal上
num[row, col] = 3;
}
row = x;
col = y;
}
//判断玩家是否获胜
public void Win()
{
int count = 0;
//遍历数组,逐个判断
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (num[i, j] == 6)
count++;
}
}
//如果两个箱子均推到goal上,则判为玩家获胜
if (count == 2)
{
DialogResult result = MessageBox.Show("是否进入下一关?", "恭喜过关(*^▽^*)", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
//点击ok选项,进入下一关,更新地图
if (result == DialogResult.OK)
{
num = new int[10, 10];
leval++;
if (leval == 8)
{
DialogResult result2 = MessageBox.Show("您已全部通关!!是否重新开始", "(p≧w≦q)", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
if (result2 == DialogResult.OK)
{
num = new int[10, 10];
leval = 1;
}
}
_direction = Direction.Down;//默认小人方向
initnum();
view();
}
}
}