为了学习的目的,我想在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;
}
}
发布于 2018-08-31 02:13:03
尝试以这种方式更改您的Main
方法,以使图片更好:
static void Main()
{
ScreenSettings();
while (true)
{
invader.Clear();
InitializeInvaders(moveY, moveX);
DrawInvaders();
Console.Clear();
Thread.Sleep(10);
moveX++;
}
}
关键是,在重画外星人之前,你必须先清除先前的位置。您不需要在InitializeInvaders
和DrawInvaders
的Main
中打两次电话。
我同意Dour的观点,认为Invader
课程会更好。还有另外两条建议:
invader
、moveX
、moveY
。希望能帮上忙。P.S.不错的aliens=)
https://stackoverflow.com/questions/52086601
复制相似问题