命令行小游戏Beta1.0

来源:互联网 发布:楼层网络布局图 编辑:程序博客网 时间:2024/05/11 04:46

遇到的问题:

1.Console.ReadKey() 返回的是一个 ConsoleKeyInfo 对象,表示按键的状态,包括按下的键位、以及 Shift、Alt 和 Ctrl 的状态等等

2.Console.ReadKey() 可以传递一个 Boolean 参数,表示是否隐藏回显,默认是 false。如果传递 true 的话,用户的输入就不会回显在控制台上。

    ConsoleKeyInfo kinfo = Console.ReadKey(true);    var op= Console.ReadKey(true);        switch (op.KeyChar)

3.用Console.clear() 屏幕闪烁严重。参考了其他人的做法 改成用Console.SetCursorPosition(x,y);只重绘了前后两个位置。



using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Beta1._0{    class Game    {        private int MapHight ;        private int MapWidth;        private int PlayerX;        private int PlayerY;        public  Game ()        {            MapHight = 20;            MapWidth = 40;            PlayerX = 5;            PlayerY = 5;        }        public void Move ()        {            var  op= Console.ReadKey(true);            switch (op.KeyChar)            {                case 'w':                case 'W':                    if (PlayerY > 1)                    {                        Console.SetCursorPosition(PlayerX, PlayerY);                        Console.Write(" ");                        PlayerY--;                        Console.SetCursorPosition(PlayerX, PlayerY);                        Console.Write("♜");                    }                    break;                case 's':                case 'S':                    if (PlayerY <MapHight-1)                    {                        Console.SetCursorPosition(PlayerX, PlayerY);                        Console.Write(" ");                        PlayerY++;                        Console.SetCursorPosition(PlayerX, PlayerY);                        Console.Write("♜");                    }                    break;                case 'a':                case 'A':                    if (PlayerX > 1)                    {                        Console.SetCursorPosition(PlayerX, PlayerY);                        Console.Write(" ");                        PlayerX--;                        Console.SetCursorPosition(PlayerX, PlayerY);                        Console.Write("♜");                    }                    break;                case 'd':                case 'D':                    if (PlayerX < MapWidth - 1)                    {                        Console.SetCursorPosition(PlayerX, PlayerY);                        Console.Write(" ");                        PlayerX++;                        Console.SetCursorPosition(PlayerX, PlayerY);                        Console.Write("♜");                    }                    break;            }        }        public void show()        {            int i, j;            for (i = 0; i <= MapHight; i++)            {                for (j = 0; j <= MapWidth; j++)                {                    if (i == 0 || i == MapHight  || j == 0 || j == MapWidth)                        Console.Write("#");                    else if (i == PlayerY && j == PlayerX)                        Console.Write("♜");                    else Console.Write(" ");                }                Console.WriteLine();            }        }    }    class Program    {        static void Main(string[] args)        {            Game g=new Game  ();            g.show();            while(true)            {                g.Move();            }           Console.ReadKey();        }    }}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 魅蓝note5卡顿怎么办 魅蓝note5卡了怎么办 魅蓝note5很卡怎么办 魅蓝note5锁了怎么办 荣耀9开不开机怎么办 4s更新后用不了怎么办 魅蓝3开不了机怎么办 手机不支持联通4g网络怎么办 华为手机出现emui界面怎么办 华为畅玩4x内存不足怎么办 手机电源键掉了怎么办 手机电源键坏了怎么办 小米5s听筒声音小怎么办 荣耀8电源键失灵怎么办 华为荣耀3c卡怎么办 大王卡是2g网络怎么办 联通停用2g副卡怎么办 华为荣耀8忘记解锁密码怎么办 华为手机内存满了怎么办 华为手机无限重启怎么办 华为3c重启怎么办 荣耀6 无限重启怎么办 手机进水无法开机了怎么办 华为手机不停重启怎么办 华为手机反复重启怎么办 酷派电池不耐用怎么办 美图手机充电慢怎么办 酷派b770太卡怎么办 酷派手机出现无命令怎么办 华为荣耀4x卡怎么办 华为手机图案解锁忘了怎么办 xp电脑读不起u盘怎么办 在外国玩王者卡怎么办 华为p7忘记解锁密码怎么办 华为荣耀4x存储空间不足怎么办 红米4a内存不够怎么办 华为h60开不了机怎么办 华为荣耀4c内存不足怎么办 华为4c运行内存不足怎么办 华为手机总是显示内存不足怎么办 华为荣耀4x畅玩版内存不足怎么办