出于学习目的,我想在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;
}
}
答案 0 :(得分:2)
尝试以这种方式更改Main
方法以使图片更好:
static void Main()
{
ScreenSettings();
while (true)
{
invader.Clear();
InitializeInvaders(moveY, moveX);
DrawInvaders();
Console.Clear();
Thread.Sleep(10);
moveX++;
}
}
重点是,在重画外星人之前,您必须清除以前的位置。而且您不需要在InitializeInvaders
中两次调用DrawInvaders
和Main
。
我同意杜高拱门(Dour High Arch)的观点,Invader
类会更好。还有另外两条建议:
invader
,moveX
,moveY
。希望有帮助。附言善良的外星人=)