C# 太空入侵者控制台游戏克隆
出于学习目的,我想在《太空入侵者》的c#上制作一个控制台克隆。我被一排排侵略者的移动问题所困扰。我制作了它们,我打印了它们,但当我试图将它们向某个方向移动时,它们的速度会随着时间的推移而降低,我不明白为什么。有人有主意吗?以下是我目前的代码:C# 太空入侵者控制台游戏克隆,c#,console-application,C#,Console Application,出于学习目的,我想在《太空入侵者》的c#上制作一个控制台克隆。我被一排排侵略者的移动问题所困扰。我制作了它们,我打印了它们,但当我试图将它们向某个方向移动时,它们的速度会随着时间的推移而降低,我不明白为什么。有人有主意吗?以下是我目前的代码: using System; using System.Collections.Generic; using System.Threading; namespace SpaceInvader { public struct Position
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();
while (true)
{
invader.Clear();
InitializeInvaders(moveY, moveX);
DrawInvaders();
Console.Clear();
Thread.Sleep(10);
moveX++;
}
}
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 EraseInvaders()
{
foreach (List<Position> invader in invaders)
{
foreach (Position part in invader)
{
Console.SetCursorPosition(part.Col, part.Row);
Console.Write(' ');
}
}
}
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统线程;
命名空间空间入侵者
{
公共结构位置
{
公共int行{get;set;}
公共整数列{get;set;}
公共字符符号{get;set;}
公共位置(整数行、整数列、字符符号)
{
this.Row=行;
this.Col=Col;
这个符号=符号;
}
}
班级计划
{
静态公共int maxRows=50;
静态公共int maxCols=180;
公共静态列表入侵者=新列表();
公共静态列表入侵者=新列表();
公共静态int-moveX=0;
公共静态int-moveY=0;
静态void Main()
{
屏幕设置();
while(true)
{
入侵者;
初始化导入器(moveY、moveX);
吸引入侵者();
Console.Clear();
睡眠(10);
moveX++;
}
}
私有静态无效屏幕设置()
{
Console.CursorVisible=false;
Console.BufferHeight=Console.WindowHeight=maxRows;
Console.BufferWidth=Console.WindowWidth=maxCols;
}
私有静态无效入侵者()
{
foreach(在入侵者中列出入侵者)
{
入侵者(入侵者);
}
}
专用静态无效橡皮擦入侵者()
{
foreach(在入侵者中列出入侵者)
{
foreach(入侵者中的位置部件)
{
Console.SetCursorPosition(part.Col,part.Row);
控制台。写入(“”);
}
}
}
私有静态void初始化INVADER(int-moveY=0,int-moveX=0)
{
对于(int行=0;行<16;行+=4)
{
用于(整数列=0;列<99;列+=9)
{
初始化EINVADER(行+移动,列+移动);
}
}
入侵者。添加(入侵者);
}
私有静态无效入侵者(列表入侵者)
{
;
foreach(入侵者中的位置部件)
{
Console.SetCursorPosition(part.Col,part.Row);
Console.Write((char)part.Symbol);
}
}
公共静态列表初始化INVADER(int行,int列)
{
int startrow=5;//起始位置行
int startcol=40;//起始位置列
添加(新位置(startrow+row,startcol+col,“/”);
添加(新位置(startrow+row,startcol+1+col,'{');
添加(新位置(startrow+row,startcol+2+col,'O');
添加(新位置(startrow+row,startcol+3+col,'}');
添加(新位置(startrow+row,startcol+4+col,“\\”);
添加(新位置(startrow+1+行,startcol+col,“\\”);
添加(新位置(startrow+1+行,startcol+1+列,“~”);
添加(新位置(startrow+1+行,startcol+2+列,“$”);
添加(新位置(startrow+1+行,startcol+3+列,“~”);
添加(新位置(startrow+1+行,startcol+4+列,“/”);
还击入侵者;
}
}
}
您似乎一直在主循环中调用InitializeInvaders。此函数将入侵者添加到入侵者列表中(不带),但不会删除旧入侵者。因为入侵者可能与旧入侵者处于相同的位置,所以您可能无法在屏幕上看到他们
[编辑:啊,我明白了,它在主循环中被删除了,但是:]
此外,您还可以将整个列表(入侵者)添加到入侵者列表中,而无需清除入侵者。这个列表将不断增长
tl;你忘了清除入侵者名单
但我想你还有别的想法。入侵者不应该在每次改变位置时都被重新创造。您应该只调用InitializeInvaders一次,然后直接在索引上操作列表中的项。(入侵者[i].posX=x…),其中“i”(默认情况下)是((y*x)+x)(即:y乘以一行加上最后一行上的x位置。)
为了清楚起见:您可以这样添加它们(伪代码):
for(y=0到最大值)
{
对于(x=0到maxx)
{
添加(新入侵者(x,y));
}
}
然后,每个“i”正好位于(开始)y*x+x位置
然后你有一个全局x,y,它被添加到这个位置,任务完成了。
你只需要改变这个循环中的全局x,y,所有入侵者就会移动
我希望这有点帮助
这是我关于stackoverflow的第一个答案,所以请不要太恨我。您似乎一直在主循环中调用InitializeInvaders。此函数将入侵者添加到入侵者列表中(不带),但不会删除旧入侵者。因为入侵者可能与旧入侵者处于相同的位置,所以您可能无法在屏幕上看到他们 [编辑:啊,我明白了,它在主循环中被删除了,但是:] 此外,您还可以将整个列表(入侵者)添加到入侵者列表中,而无需清除入侵者。这个列表将不断增长 tl;你忘了清除入侵者名单 但我想你还有别的想法。入侵者不应该在每次改变位置时都被重新创造。你应该叫initialize