太空入侵者外星人行

时间:2018-08-29 21:54:52

标签: c# console-application

出于学习目的,我想在c#空间入侵者上创建一个控制台克隆。我一直陷在如何制造侵略者行的问题上。例如,必须有4行带有6个入侵者。我设法使一个入侵者成为结构的列表,在其中我放置了x和y坐标以及该字符。 我的问题是:我该如何用6种这种类型的入侵者进行4行打印,以便它们可以在每个具有不同坐标的控制台上打印出来。 这是我的入侵者的一个例子:

using System;
using System.Collections.Generic;
using System.Threading;


namespace SpaceInvader
{
    public struct Position
    {
        public int Row { get; set; }
        public int Col { get; set; }
        public char Symbol { get; set; }

        public Position(int row, int col, char symbol)
        {
            this.Row = row;
            this.Col = col;
            this.Symbol = symbol;
        }
    }
    class Program
    {
        static public int maxRows = 50;
        static public int maxCols = 180;

        public static List<Position> invader = new List<Position>();
        public static List<List<Position>> invaders = new List<List<Position>>();
        public static  int moveX = 0;
        public static int moveY =0;


        static void Main()
        {
            ScreenSettings();
            InitializeInvaders();
            DrawInvaders();

            while (true)
            {

                moveX++;    
                InitializeInvaders(moveY,moveX);
                DrawInvaders();
                Console.Clear();
                Thread.Sleep(300);

            }

        }


        private static void ScreenSettings()
        {
            Console.CursorVisible = false;
            Console.BufferHeight = Console.WindowHeight = maxRows;
            Console.BufferWidth = Console.WindowWidth = maxCols;
        }

        private static void DrawInvaders()
        {
            foreach (List<Position> invader in invaders)
            {
                DrawInvader(invader);
            }
        }

        private static void InitializeInvaders(int moveY = 0, int moveX = 0)
        {
            for (int row = 0 ; row < 16; row += 4)
            {
                for (int col = 0 ; col < 99 ; col += 9)
                {
                    InitializeInvader(row+moveY, col+moveX);
                }
            }

            invaders.Add(invader);

        }


        private static void DrawInvader(List<Position> invader)
        {
            ;
            foreach (Position part in invader)
            {
                Console.SetCursorPosition(part.Col, part.Row);
                Console.Write((char)part.Symbol);
            }

        }

        public static List<Position> InitializeInvader(int row, int col)
        {


            int startrow = 5;//start position row
            int startcol = 40;// start position col

            invader.Add(new Position(startrow + row, startcol + col, '/'));
            invader.Add(new Position(startrow + row, startcol + 1 + col, '{'));
            invader.Add(new Position(startrow + row, startcol + 2 + col, 'O'));
            invader.Add(new Position(startrow + row, startcol + 3 + col, '}'));
            invader.Add(new Position(startrow + row, startcol + 4 + col, '\\'));
            invader.Add(new Position(startrow + 1 + row, startcol + col, '\\'));
            invader.Add(new Position(startrow + 1 + row, startcol + 1 + col, '~'));
            invader.Add(new Position(startrow + 1 + row, startcol + 2 + col, '$'));
            invader.Add(new Position(startrow + 1 + row, startcol + 3 + col, '~'));
            invader.Add(new Position(startrow + 1 + row, startcol + 4 + col, '/'));
            return invader;

    }
}

1 个答案:

答案 0 :(得分:2)

尝试以这种方式更改Main方法以使图片更好:

static void Main()
{
    ScreenSettings();

    while (true)
    {
        invader.Clear();
        InitializeInvaders(moveY, moveX);
        DrawInvaders();
        Console.Clear();
        Thread.Sleep(10);
        moveX++;
    }
}

重点是,在重画外星人之前,您必须清除以前的位置。而且您不需要在InitializeInvaders中两次调用DrawInvadersMain

我同意杜高拱门(Dour High Arch)的观点,Invader类会更好。还有另外两条建议:

  1. 不要像全局静态变量一样为您命名局部变量:invadermoveXmoveY
  2. 尝试摆脱全局静态变量。将这种状态封装在类中,将其隐藏在私有字段中,使那些类负责修改其状态,以使代码更结构化和易于理解。

希望有帮助。附言善良的外星人=)

相关问题