zoukankan      html  css  js  c++  java
  • 慢慢完善-小游戏之推箱子

     利用数组做为地图,利用数字为各种元素,用简单的语句做出推箱子的效果。

    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(" ");

                    }

    进入读取键盘循环需要预设一个跳出的条件,这里我设置的是箱子的位置等于终点位置时跳出,这个条件有缺陷,暂时只能适用于一个终点的地图,有一种方法可以解决,就是判断终点坐标是否等于箱子的值,两个终点就&&一下,这里就要注意移动的时候要替换值了,箱子坐标不需要用了。

                    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(" ");

                        }

     

                    }

    条件不符合之后,跳出循环,清一下屏,输出“闯关成功”

                    Console.Clear();

                    Console.Write("闯关成功!");

     

               }

            }

    这样,这个简单的小游戏就制作完成,中间还有很多其它的东西没有考虑到,比如换地图,多加箱子,加关卡等,以后慢慢完善。

    下面是效果图:

    成功之后,提示

     

     

    程序网盘地址,感兴趣的可以下载试玩,暂时只支持WIN7及以上版本的操作系统:

    http://pan.baidu.com/s/1nt5HJlf

     

     

     

  • 相关阅读:
    PyQt5复杂控件(树控件、选项卡控件(滚动条控件、多文档控件、停靠控件)
    PyQt5单元格操作大全
    PyQt5打印机
    PyQt5剪切板操作
    PyQt5的菜单栏、工具栏和状态栏
    PyQt5日历控件及相关操作
    PyQt5控件支持拖拽方法
    《Lua程序设计》第3章 表达式 学习笔记
    Lua中的table构造式(table constructor)
    《Lua程序设计》第2章 类型与值 学习笔记
  • 原文地址:https://www.cnblogs.com/Alvin-ftd/p/Alvin-ftd.html
Copyright © 2011-2022 走看看