首页 > 代码库 > 慢慢完善-小游戏之推箱子
慢慢完善-小游戏之推箱子
利用数组做为地图,利用数字为各种元素,用简单的语句做出推箱子的效果。
static void Main(string[] args)
{
Console.Write("第一关,请按空格键开始");//程序开始,在屏幕上显示这个提醒,然后下面就开始读取键盘输入的按键:
ConsoleKeyInfo cf = Console.ReadKey();
string s = cf.Key.ToString().ToLower(); //读取键盘,注意s,cf等变量不要和下面的重复
//如果读到键盘输入的为空格键时,开始游戏程序,这里用了一个if语句来判断
if (s == "spacebar")
{
//这里是定义一下地图,还有地图上一些重要元素的标识,0代表路,1代表小人,2代表箱子,3代表墙壁,4代表终点,
int x = 6, y = 1, a = 6, b = 3; //x,y为人的坐标,a,b为箱子的坐标
int[,] map = new int[10, 10] {
{3,3,3,3,3,3,3,3,3,3},
{3,0,0,0,0,3,3,0,0,3},
{3,0,3,0,0,3,3,0,0,3},
{3,0,3,3,0,3,3,0,0,3},
{3,0,3,0,0,0,0,0,0,3},
{3,0,0,0,0,3,0,0,0,3},
{3,1,0,2,0,3,3,0,0,3},
{3,0,0,0,0,3,0,0,3,3},
{3,0,0,0,0,3,0,0,4,3},
{3,3,3,3,3,3,3,3,3,3}
}; //10,10的二维数组定义了一个地图
//之后是显示出图形化的界面,分别给地图上的物体赋予形状,这里用for语句循环查找地图上的每个点,判断符合条件就打印出来
Console.Clear();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (map[i, j] == 3)
{
Console.Write("■");
}
else if (map[i, j] == 0)
{
Console.Write(" ");
}
else if (map[i, j] == 1) //□♀-
{
Console.Write("♀");
}
else if (map[i, j] == 2)
{
Console.Write("□");
}
else if (map[i, j] == 4)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("※");
Console.ForegroundColor = ConsoleColor.White;
}
}
Console.Write("\n");
}
进入读取键盘循环需要预设一个跳出的条件,这里我设置的是箱子的位置等于终点位置时跳出,这个条件有缺陷,暂时只能适用于一个终点的地图,有一种方法可以解决,就是判断终点坐标是否等于箱子的值,两个终点就&&一下,这里就要注意移动的时候要替换值了,箱子坐标不需要用了。
while (map[a, b] != map[8, 8])
{
ConsoleKeyInfo kf = Console.ReadKey();
string k = kf.Key.ToString().ToLower(); //读取键盘
//以下为读取上键时的程序
if (k == "uparrow")
{
if (x > 1)
{
if (map[x - 1, y] == 0)
{
int t = map[x, y];
map[x, y] = map[x - 1, y];
map[x - 1, y] = t;
x--;
} //如果上面是路,上移
else if (map[x - 1, y] == 2 && map[x - 2, y] == 0)
{
int m = map[x - 1, y];
map[x - 1, y] = map[x - 2, y];
map[x - 2, y] = m;
int n = map[x, y];
map[x, y] = map[x - 1, y];
map[x - 1, y] = n;
a = x - 2; b = y;
x--; //如果上面是箱子,箱子上面是路,都上移,否则错误
}
else if (map[x - 1, y] == 2 && map[x - 2, y] == 4)
{
map[x - 2, y] = map[x - 1, y];
map[x - 1, y] = map[x, y];
map[x, y] = 0;
a = x - 2; b = y;
x--;
}
else
{
Console.Write("\a");
} //如果上面是箱子,箱子上面是终点,替换
}
else
{
Console.Write("\a");
}
}
//以下为按下键时的程序
else if (k == "downarrow")
{
if (x < 8)
{
if (map[x + 1, y] == 0)
{
int t = map[x, y];
map[x, y] = map[x + 1, y];
map[x + 1, y] = t;
x++;
} //如果下面是路,下移
else if (map[x + 1, y] == 2 && map[x + 2, y] == 0)
{
int m = map[x + 1, y];
map[x + 1, y] = map[x + 2, y];
map[x + 2, y] = m;
int n = map[x, y];
map[x, y] = map[x + 1, y];
map[x + 1, y] = n;
a = x + 2; b = y;
x++; //如果下面是箱子,箱子下面是路,都下移,否则错误
}
else if (map[x + 1, y] == 2 && map[x + 2, y] == 4)
{
map[x + 2, y] = map[x + 1, y];
map[x + 1, y] = map[x, y];
map[x, y] = 0;
a = x + 2; b = y;
x++;
}
else
{
Console.Write("\a");
}
} //如果下面是箱子,箱子下面是终点,替换
else
{
Console.Write("\a");
}
}
//以下为按左键时的程序
else if (k == "leftarrow")
{
if (y > 1)
{
if (map[x, y - 1] == 0)
{
int t = map[x, y];
map[x, y] = map[x, y - 1];
map[x, y - 1] = t;
y--;
} //如果左面是路,左移
else if (map[x, y - 1] == 2 && map[x, y - 2] == 0)
{
int m = map[x, y - 1];
map[x, y - 1] = map[x, y - 2];
map[x, y - 2] = m;
int n = map[x, y];
map[x, y] = map[x, y - 1];
map[x, y - 1] = n;
a = x; b = y - 2;
y--; //如果左面是箱子,箱子左面是路,都左移,否则错误
}
else if (map[x , y-1] == 2 && map[x, y-2] == 4)
{
map[x , y-2] = map[x, y-1];
map[x, y-1] = map[x, y];
map[x, y] = 0;
a = x; b = y - 2;
y--;
}
else
{
Console.Write("\a");
} //如果左面是箱子,箱子左面是终点,替换
}
else
{
Console.Write("\a");
}
}
//以下为输入右键时的程序
else if (k == "rightarrow")
{
if (y < 8)
{
if (map[x, y + 1] == 0)
{
int t = map[x, y];
map[x, y] = map[x, y + 1];
map[x, y + 1] = t;
y++;
} //如果右面是路,右移
else if (map[x, y + 1] == 2 && map[x, y + 2] == 0)
{
int m = map[x, y + 1];
map[x, y + 1] = map[x, y + 2];
map[x, y + 2] = m;
int n = map[x, y];
map[x, y] = map[x, y + 1];
map[x, y + 1] = n;
a = x; b = y + 2;
y++; //如果右面是箱子,箱子右面是路,都右移
}
else if (map[x, y + 1] == 2 && map[x, y + 2] == 4)
{
map[x, y + 2] = map[x, y + 1];
map[x, y + 1] = map[x, y];
map[x, y] = 0;
a = x; b = y + 2;
y++;
}
else
{
Console.Write("\a");
}
} //如果右面是箱子,箱子右面是终点,都右移,替换
else
{
Console.Write("\a");
}
}
//将移动后的地图显示一遍
Console.Clear();
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (map[i, j] == 3)
{
Console.Write("■");
}
else if (map[i, j] == 0)
{
Console.Write(" ");
}
else if (map[i, j] == 1) //□♀-
{
Console.Write("♀");
}
else if (map[i, j] == 2)
{
Console.Write("□");
}
else if (map[i, j] == 4)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("※");
Console.ForegroundColor = ConsoleColor.White;
}
}
Console.Write("\n");
}
}
条件不符合之后,跳出循环,清一下屏,输出“闯关成功”
Console.Clear();
Console.Write("闯关成功!");
}
}
这样,这个简单的小游戏就制作完成,中间还有很多其它的东西没有考虑到,比如换地图,多加箱子,加关卡等,以后慢慢完善。
慢慢完善-小游戏之推箱子