首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >太空侵略者外星人行

太空侵略者外星人行
EN

Stack Overflow用户
提问于 2018-08-29 21:54:51
回答 1查看 287关注 0票数 1

为了学习的目的,我想在c#上做一个太空入侵者的控制台克隆。我被困在如何制造一排排入侵者的问题上。例如,必须有4行和6个入侵者。我设法使一个入侵者作为一个结构列表,在其中我放置x和y坐标和字符。我的问题是:如何用6种类型的入侵者做4行,这样就可以将它们打印在控制台上,每一行都有不同的坐标。这是我的入侵者的一个例子:

代码语言:javascript
运行
复制
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;

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-31 02:13:03

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

代码语言:javascript
运行
复制
static void Main()
{
    ScreenSettings();

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

关键是,在重画外星人之前,你必须先清除先前的位置。您不需要在InitializeInvadersDrawInvadersMain中打两次电话。

我同意Dour的观点,认为Invader课程会更好。还有另外两条建议:

  1. 不要像全局静力学那样命名局部变量:invadermoveXmoveY
  2. 试着远离全局静态变量。将此状态封装在类中,将其隐藏在私有字段中,使这些类负责修改它们的状态,从而使代码更具有结构化和可理解性。

希望能帮上忙。P.S.不错的aliens=)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52086601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档