首页 > 技术文章 > 2d命令行小游戏源码

mercuialC 2017-02-17 11:32 原文

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace _2d
{
    class Dit
    {
        public int x, y;
        
    }
    class Map
    {
       public  void mp()
        {
             const int buy = 20;
            for (int i = 0; i < buy; i++)
            {
                for (int j = 0; j < buy; j++)
                {
                    if (i == 0 || i == buy - 1 || j == 0 || j == buy - 1)
                        Console.Write("█");
                    else
                        Console.Write(" ");
                }
                Console.WriteLine();
            }
       }
        const char b='@';
        const char d = ' ';
        const char e = '*';
        public int fenshu=0;
             public void set(int x, int y, char b)
       {
           int buy = 20;
           Console.SetCursorPosition(2 * y, x);
           Console.Write(b);
           Console.SetCursorPosition(0, buy);
       }
             
            public void put()
            {
                int speed = 160;
                int buy=20;
                Random where = new Random();
                Random _where=new Random();               
                Dit renwu = new Dit();
                renwu.x = _where.Next(1, buy - 2);
                renwu.y = _where.Next(1, buy - 2);               
                set(renwu.x, renwu.y, b);
                bool food = false;
                Dit _food=new Dit();         
                _food.x=where.Next(1,buy-2);
                _food.y=where.Next(1,buy-2);
                set(_food.y,_food.x,e);
               
                char c;
                for (; ; )
                {
                    c = Console.ReadKey(true).KeyChar;


                    switch (c)
                    {

                        case 'w': set(renwu.x, renwu.y, d); set(renwu.x - 1, renwu.y, b); renwu.x--; Thread.Sleep(speed); break;
                        case 'a': set(renwu.x, renwu.y, d); set(renwu.x, renwu.y - 1, b); renwu.y--; Thread.Sleep(speed); break;
                        case 's': set(renwu.x, renwu.y, d); set(renwu.x + 1, renwu.y, b); renwu.x++; Thread.Sleep(speed); break;
                        case 'd': set(renwu.x, renwu.y, d); set(renwu.x, renwu.y + 1, b); renwu.y++; Thread.Sleep(speed); break;
                    }
                    if (renwu.x == 0 || renwu.y == 0 || renwu.x == buy - 1 || renwu.y == buy - 1)
                    {
                        Console.Clear();
                        Console.WriteLine("GameOver!\n您的得分是{0}",fenshu);
                    }
                    if(renwu.x==_food.y&&renwu.y==_food.x)
                    {
                        food=true;
                        _food.x = where.Next(1, buy - 2);
                        _food.y = where.Next(1, buy - 2);
                        set(_food.y, _food.x, e);
                        fenshu++;
                    }
                }
               
            }
       
        }

        class Menu
        {
            public void paly()
            {
                Console.WriteLine("2d命令行小游戏");
                Console.WriteLine("输入1开始游戏\n请将输入法调至英文状态");
            }
            public void start()
            {
                int a = Convert.ToInt32(Console.ReadLine());
                if (a == 1)
                {
                    Console.Clear();
                    Map _map = new Map();
                    _map.mp();
                    _map.put();
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Menu _menu = new Menu();
                Console.CursorVisible = false;
                _menu.paly();
                _menu.start();
                Console.ReadLine();
            }
        }
    }


推荐阅读