本文介绍了射击。与列表碰撞的镜头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我出于学习目的而制作了一个太空入侵者克隆。入侵者被枪杀后,我一直陷在这个问题上。我不确定子弹与侵略者(位置列表)的一部分发生碰撞时如何擦除它以及游戏是否继续。有人知道吗?



这里是代码:

 使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading;
使用System.Threading.Tasks;

命名空间SpaceInvader
{
类程序
{

静态void Main()
{
设置。 ScreenSettings();
ArmyOfInvaders.InitializeArmyOfInvaders();
Player.InitializePlayer();
int stepCount = 0;
int ArmyOfInvadersSpeed = 50;
而(true)
{
stepCount ++;

if(stepCount%ArmyOfInvadersSpeed == 0)
{
Draw.EraseItem(ArmyOfInvaders.armyOfInvaders);

Invader.invader.Clear();
ArmyOfInvaders.armyOfInvaders.Clear();
ArmyOfInvaders.InitializeArmyOfInvaders(Movement.moveY,Movement.moveX);

Movement.MovementArmyOfInvaders();
stepCount = 0;
}
Console.CursorVisible = false;
Draw.DrawItem(ArmyOfInvaders.armyOfInvaders);
Draw.EraseItem(Player.player);
Shoot.GenerateShot();

Movement.MovementPlayer();
Draw.DrawItem(Player.player);
Draw.DrawShoot();
Draw.EraseShoot();
//Collision.InvaderGotShot();

Thread.Sleep(Common.gameSpeed);

}

}

}
public class设置
{
static public int maxRows = 50;
static public int maxCols = 180;
public static void ScreenSettings()
{
Console.CursorVisible = false;
Console.BufferHeight = Console.WindowHeight = maxRows;
Console.BufferWidth = Console.WindowWidth = maxCols;
}
}
公共结构位置
{
public int Row {get;组; }
public int Col {get;组; }
public char Symbol {get;组; }

public Position(int row,int col,char symbol)
{
this.Row = row;
this.Col = col;
this.Symbol =符号;
}
}
公共类播放器
{
public static List< Position>玩家=新的List< Position>();
public static int playerWide = 9;
public static int playerLong = player.Count;

公共静态列表< Position> InitializePlayer(int row = 0,int col = 0)
{


int startrow = Settings.maxRows-5-playerLong; //开始位置行
int startcol =(Settings.maxCols / 2)-(playerWide / 2); //开始位置col

player.Add(new Position(startrow + row,startcol + col,'A'));
player.Add(new Position(startrow + 1 + row,startcol + col,'o'));
player.Add(new Position(startrow + 2 + row,startcol-2 + col,’|’));
player.Add(new Position(startrow + 2 + row,startcol + col,'o'));
player.Add(new Position(startrow + 2 + row,startcol + 2 + col,’|’));
player.Add(new Position(startrow + 3 + row,startcol-4 + col,‘/’));
player.Add(new Position(startrow + 3 + row,startcol-3 + col,‘\\’));
player.Add(new Position(startrow + 3 + row,startcol-2 + col,‘\\’));
player.Add(new Position(startrow + 3 + row,startcol-1 + col,‘\\’));
player.Add(new Position(startrow + 3 + row,startcol + col,'o'));
player.Add(new Position(startrow + 3 + row,startcol + 1 + col,‘/’));
player.Add(new Position(startrow + 3 + row,startcol + 2 + col,‘/’));
player.Add(new Position(startrow + 3 + row,startcol + 3 + col,‘/’));
player.Add(new Position(startrow + 3 + row,startcol + 4 + col,‘\\’));
player.Add(new Position(startrow + 4 + row,startcol-2 + col,‘<’));
player.Add(new Position(startrow + 4 + row,startcol-1 + col,‘/’));
player.Add(new Position(startrow + 4 + row,startcol-0 + col,'o'));
player.Add(new Position(startrow + 4 + row,startcol + 1 + col,‘\\’));
player.Add(new Position(startrow + 4 + row,startcol + 2 + col,’>’));
返回玩家;
}
}
公共类运动
{
public static bool isRight = true;

public static int moveX = 0;
public static int moveY = 0;
public static int left = -1;
public static int right = 1;
public static int ArmyOfInvaderJump = 5;
public static void MovementArmyOfInvaders()
{


if(isRight)
{
moveX + = ArmyOfInvaderJump;
if(ArmyOfInvaders.armyOfInvaders [ArmyOfInvaders.armyOfInvaders.Count-1] [Invader.invader.Count-1] .Col> = Settings.maxCols-20)
{
isRight = false ;
moveY + = 2;
}
}
else
{
moveX-= ArmyOfInvaderJump;
if(ArmyOfInvaders.armyOfInvaders [0] [0] .Col< = 20)
{
isRight = true;
moveY + = 2;
}
}
}

public static void MovementPlayer()
{


而(控制台。 KeyAvailable)
{
ConsoleKeyInfo键= Console.ReadKey(true);
而(Console.KeyAvailable)
{
Console.ReadKey(true);
}
if(Player.player [0] .Col-6> = 0)
{
if(key.Key == ConsoleKey.LeftArrow)
{
for(int i = 0; i< Player.player.Count; i ++)
{
Player.player [i] = new Position(Player.player [i] .Row ,Player.player [i] .Col +左,Player.player [i] .Symbol);
}
}
}
if(Player.player [0] .Col + 7< Settings.maxCols)
{
if(key。键== ConsoleKey.RightArrow)
{
for(int i = 0; i< Player.player.Count; i ++)
{
Player.player [i] = new Position(Player.player [i] .Row,Player.player [i] .Col + right,Player.player [i] .Symbol);
}
}
}
if(key.Key == ConsoleKey.Spacebar)
{
Shoot.shoot [Player.player [0]。行-1,Player.player [0] .Col] = 1;
}
}

}

}
类拍摄
{
public static int [,]拍摄= new int [Settings.maxRows,Settings.maxCols];

public static void GenerateShot()
{
for(int Row = 0; Row< Shoot.shoot.GetLength(0); Row ++)
{
for(int Col = 0; Col< Shoot.shoot.GetLength(1); Col ++)
{
if(Shoot.shoot [Row,Col] == 1& (行== 0))
{
Shoot.shoot [Row,Col] = 0;

}
如果(Shoot.shoot [Row,Col] == 1)
{
Shoot.shoot [Row,Col] = 0;
Shoot.shoot [Row-1,Col] = 1;



}
}
}


}
}
静态public class Invader
{
public static List< Position> invader = new List< Position>();
public static List< Position> InitializeInvader(int row,int col)
{


int startrow = 5; //开始位置行
int startcol = ArmyOfInvaders.startArmyOfInvadersPosition; //开始位置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,‘/’));
返程入侵者;
}




}
公共类Draw
{
public static void DrawItem(List< Position> ;入侵者)
{
;
foreach(入侵者的位置部分)
{
Console.SetCursorPosition(part.Col,part.Row);
Console.Write((char)part.Symbol);
}

}
public static void DrawItem(List< List< Position>" ArmyOfInvaders)
{
foreach(List< Position> invader in ArmyOfInvaders)
{
Draw.DrawItem(invader);
}
}
public static void EraseItem(List< List< Position>" ArmyOfInvaders)
{
foreach(List< Position> in ArmyOfInvaders中的侵略者)
{
foreach(入侵者的位置)
{
Console.SetCursorPosition(part.Col,part.Row);
Console.Write(’);
}
}
}
公共静态无效EraseItem(List< Position>入侵者)
{

foreach(入侵者的位置部分)
{
Console.SetCursorPosition(part.Col,part.Row);
Console.Write(’);
}

}
public static void DrawShoot()
{
for(int Row = 0; Row< Shoot.shoot.GetLength(0 ); Row ++)
{
for(int Col = 0; Col< Shoot.shoot.GetLength(1); Col ++)
{
if(Shoot.shoot [Row ,Col] == 1)
{
Console.SetCursorPosition(Col,Row);
Console.Write( |);

}
}
}
}
public static void EraseShoot()
{
for(int Row = 0;行< Shoot.shoot.GetLength(0);行++)
{
for(int Col = 0; Col< Shoot.shoot.GetLength(1); Col ++)
{
if(行== 0)
{
Console.SetCursorPosition(Col,Row);
Console.Write();
}
if(Shoot.shoot [Row,Col] == 1)
{
Console.SetCursorPosition(Col,Row + 1);
Console.Write();
}

}
}
}
}
公共课碰撞
{

公共static void InvaderGotShot()
{
for(int i = 0; i< ArmyOfInvaders.armyOfInvaders.Count; i ++)
{
for(int j = 0; j< ; Invader.invader.Count; j ++)
{
if(Shoot.shoot [ArmyOfInvaders.armyOfInvaders [i] [j] .Row,ArmyOfInvaders.armyOfInvaders [i] [j] .Col] == 1)
{

ArmyOfInvaders.armyOfInvaders [i] .Clear();
for(int k = 0; k< Invader.invader.Count; k ++)
{
ArmyOfInvaders.armyOfInvaders [i] .Add(new Position(ArmyOfInvaders.armyOfInvaders [i] [ k] .Row,ArmyOfInvaders.armyOfInvaders [i] [k] .Col,''));
}

}

}
}

}
}
公共类ArmyOfInvaders
{

public static int invadersColDistance = 9;
public static int invadersColsCount = 10;
public static int invadersRowDistance = 4;
public static int invadersRowsCount = 4;

public static int startArmyOfInvadersPosition =(Settings.maxCols-(ArmyOfInvaders.invadersColDistance * ArmyOfInvaders.invadersColsCount))/ 2 + 5; // 5 e shiranata na edin invader

public静态List< List< Position>> ArmyOfInvaders = new List< List< Position>>();

公共静态无效InitializeArmyOfInvaders(int moveY = 0,int moveX = 0)
{
for(int row = 0; row< invadersRowDistance * invadersRowsCount; row + = invadersRowDistance )
{
for(int col = 0; col< invadersColDistance * invadersColsCount; col + = invadersColDistance)
{
Invader.InitializeInvader(row + moveY,col + moveX) ;
}
}

ArmyOfInvaders.Add(Invader.invader);

}






}
公共类公共
{
public static int gameSpeed = 1;
}
}

谢谢!!

解决方案

我没有声称要对您的设计进行重做,而是向其中添加了死亡侵略者( ArmyOfInvaders.deadInvader )的集合。存储那些已经射击的人,并更改了 InvaderGotShot 和 DrawItem 方法。我摆脱了 Invader.invader 集合,并在 Invader.InitializeInvader 中引入了局部变量,而不是为了防止分配错误。请看一下:

  class程序
{

static void Main()
{
Settings.ScreenSettings();
ArmyOfInvaders.InitializeArmyOfInvaders();
Player.InitializePlayer();
int stepCount = 0;
int ArmyOfInvadersSpeed = 50;
而(true)
{
stepCount ++;

if(stepCount%ArmyOfInvadersSpeed == 0)
{
Draw.EraseItem(ArmyOfInvaders.armyOfInvaders);

ArmyOfInvaders.armyOfInvaders.Clear();
ArmyOfInvaders.InitializeArmyOfInvaders(Movement.moveY,Movement.moveX);

Movement.MovementArmyOfInvaders();
stepCount = 0;
}
Console.CursorVisible = false;
Draw.DrawItem(ArmyOfInvaders.armyOfInvaders);
Draw.EraseItem(Player.player);
Shoot.GenerateShot();

Movement.MovementPlayer();
Draw.DrawItem(Player.player);
Draw.DrawShoot();
Draw.EraseShoot();
Collision.InvaderGotShot();

Thread.Sleep(Common.gameSpeed);

}

}

}
public class设置
{
static public int maxRows = 50;
static public int maxCols = 180;
public static void ScreenSettings()
{
Console.CursorVisible = false;
Console.BufferHeight = Console.WindowHeight = maxRows;
Console.BufferWidth = Console.WindowWidth = maxCols;
}
}
公共结构位置
{
public int Row {get;组; }
public int Col {get;组; }
public char Symbol {get;组; }

public Position(int row,int col,char symbol)
{
this.Row = row;
this.Col = col;
this.Symbol =符号;
}
}
公共类播放器
{
public static List< Position>玩家=新的List< Position>();
public static int playerWide = 9;
public static int playerLong = player.Count;

公共静态列表< Position> InitializePlayer(int row = 0,int col = 0)
{


int startrow = Settings.maxRows-5-playerLong; //开始位置行
int startcol =(Settings.maxCols / 2)-(playerWide / 2); //开始位置col

player.Add(new Position(startrow + row,startcol + col,'A'));
player.Add(new Position(startrow + 1 + row,startcol + col,'o'));
player.Add(new Position(startrow + 2 + row,startcol-2 + col,’|’));
player.Add(new Position(startrow + 2 + row,startcol + col,'o'));
player.Add(new Position(startrow + 2 + row,startcol + 2 + col,’|’));
player.Add(new Position(startrow + 3 + row,startcol-4 + col,‘/’));
player.Add(new Position(startrow + 3 + row,startcol-3 + col,‘\\’));
player.Add(new Position(startrow + 3 + row,startcol-2 + col,‘\\’));
player.Add(new Position(startrow + 3 + row,startcol-1 + col,‘\\’));
player.Add(new Position(startrow + 3 + row,startcol + col,'o'));
player.Add(new Position(startrow + 3 + row,startcol + 1 + col,‘/’));
player.Add(new Position(startrow + 3 + row,startcol + 2 + col,‘/’));
player.Add(new Position(startrow + 3 + row,startcol + 3 + col,‘/’));
player.Add(new Position(startrow + 3 + row,startcol + 4 + col,‘\\’));
player.Add(new Position(startrow + 4 + row,startcol-2 + col,‘<’));
player.Add(new Position(startrow + 4 + row,startcol-1 + col,‘/’));
player.Add(new Position(startrow + 4 + row,startcol-0 + col,'o'));
player.Add(new Position(startrow + 4 + row,startcol + 1 + col,‘\\’));
player.Add(new Position(startrow + 4 + row,startcol + 2 + col,’>’));
返回玩家;
}
}
公共类运动
{
public static bool isRight = true;

public static int moveX = 0;
public static int moveY = 0;
public static int left = -1;
public static int right = 1;
public static int ArmyOfInvaderJump = 5;
public static void MovementArmyOfInvaders()
{


if(isRight)
{
moveX + = ArmyOfInvaderJump;
if(ArmyOfInvaders.armyOfInvaders [ArmyOfInvaders.armyOfInvaders.Count-1] [9] .Col> = Settings.maxCols-20)
{
isRight = false;
moveY + = 2;
}
}
else
{
moveX-= ArmyOfInvaderJump;
if(ArmyOfInvaders.armyOfInvaders [0] [0] .Col< = 20)
{
isRight = true;
moveY + = 2;
}
}
}

public static void MovementPlayer()
{


而(控制台。 KeyAvailable)
{
ConsoleKeyInfo键= Console.ReadKey(true);
而(Console.KeyAvailable)
{
Console.ReadKey(true);
}
if(Player.player [0] .Col-6> = 0)
{
if(key.Key == ConsoleKey.LeftArrow)
{
for(int i = 0; i< Player.player.Count; i ++)
{
Player.player [i] = new Position(Player.player [i] .Row ,Player.player [i] .Col +左,Player.player [i] .Symbol);
}
}
}
if(Player.player [0] .Col + 7< Settings.maxCols)
{
if(key。键== ConsoleKey.RightArrow)
{
for(int i = 0; i< Player.player.Count; i ++)
{
Player.player [i] = new Position(Player.player [i] .Row,Player.player [i] .Col + right,Player.player [i] .Symbol);
}
}
}
if(key.Key == ConsoleKey.Spacebar)
{
Shoot.shoot [Player.player [0]。行-1,Player.player [0] .Col] = 1;
}
}

}

}
类拍摄
{
public static int [,]拍摄= new int [Settings.maxRows,Settings.maxCols];

public static void GenerateShot()
{
for(int Row = 0; Row< Shoot.shoot.GetLength(0); Row ++)
{
for(int Col = 0; Col< Shoot.shoot.GetLength(1); Col ++)
{
if(Shoot.shoot [Row,Col] == 1& (行== 0))
{
Shoot.shoot [Row,Col] = 0;

}
如果(Shoot.shoot [Row,Col] == 1)
{
Shoot.shoot [Row,Col] = 0;
Shoot.shoot [Row-1,Col] = 1;



}
}
}


}
}
静态public class Invader
{
public static List< Position> InitializeInvader(int row,int col)
{
int startrow = 5; //开始位置行
int startcol = ArmyOfInvaders.startArmyOfInvadersPosition; //开始位置col

List< Position> invader = new List< Position>();
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,‘/’));
返程入侵者;
}




}
公共类Draw
{
public static void DrawItem(List< Position> ;入侵者)
{
;
foreach(入侵者的位置部分)
{
Console.SetCursorPosition(part.Col,part.Row);
Console.Write((char)part.Symbol);
}

}
public static void DrawItem(List< List< Position>" ArmyOfInvaders)
{
for(var i = 0; i < ArmyOfInvaders.Count; i ++)
{
if(!ArmyOfInvaders.deadInvaders.Contains(i))
{
Draw.DrawItem(armyOfInvaders [i]);
}
}
}
public static void EraseItem(List< List< Position>> ArmyOfInvaders)
{
foreach(List< Position>入侵者in ArmyOfInvaders)
{
foreach(入侵者的位置部分)
{
Console.SetCursorPosition(part.Col,part.Row);
Console.Write(’);
}
}
}
公共静态无效EraseItem(List< Position>入侵者)
{

foreach(入侵者的位置部分)
{
Console.SetCursorPosition(part.Col,part.Row);
Console.Write(’);
}

}
public static void DrawShoot()
{
for(int Row = 0; Row< Shoot.shoot.GetLength(0 ); Row ++)
{
for(int Col = 0; Col< Shoot.shoot.GetLength(1); Col ++)
{
if(Shoot.shoot [Row ,Col] == 1)
{
Console.SetCursorPosition(Col,Row);
Console.Write( |);

}
}
}
}
public static void EraseShoot()
{
for(int Row = 0;行< Shoot.shoot.GetLength(0);行++)
{
for(int Col = 0; Col< Shoot.shoot.GetLength(1); Col ++)
{
if(行== 0)
{
Console.SetCursorPosition(Col,Row);
Console.Write();
}
if(Shoot.shoot [Row,Col] == 1)
{
Console.SetCursorPosition(Col,Row + 1);
Console.Write();
}

}
}
}
}
公共课碰撞
{

公共static void InvaderGotShot()
{

for(int i = 0; i< ArmyOfInvaders.armyOfInvaders.Count; i ++)
{
for(int j = 0; j< ArmyOfInvaders.armyOfInvaders [i] .Count; j ++)
{
if(Shoot.shoot [ArmyOfInvaders.armyOfInvaders [i] [j] .Row,ArmyOfInvaders.armyOfInvaders [i] [ j] .Col] == 1)
{
if(!ArmyOfInvaders.deadInvaders.Contains(i))
{
Shoot.shoot [ArmyOfInvaders.armyOfInvaders [i] [ j] .Row,
ArmyOfInvaders.armyOfInvaders [i] [j] .Col] = 0;
ArmyOfInvaders.deadInvaders.Add(i);
的回报;
}
}

}
}

}
}
公共类ArmyOfInvaders
{

public static int invadersColDistance = 9;
public static int invadersColsCount = 10;
public static int invadersRowDistance = 4;
public static int invadersRowsCount = 4;
public static List< int> deadInvaders = new List< int>();

public static int startArmyOfInvadersPosition =(Settings.maxCols-(ArmyOfInvaders.invadersColDistance * ArmyOfInvaders.invadersColsCount))/ 2 + 5; // 5 e shiranata na edin invader

public静态List< List< Position>> ArmyOfInvaders = new List< List< Position>>();

公共静态无效InitializeArmyOfInvaders(int moveY = 0,int moveX = 0)
{
for(int row = 0; row< invadersRowDistance * invadersRowsCount; row + = invadersRowDistance )
{
for(int col = 0; col< invadersColDistance * invadersColsCount; col + = invadersColDistance)
{
var invader = Invader.InitializeInvader(row + moveY,col + moveX);
ArmyOfInvaders.Add(invader);
}
}

}






}
public class Common
{
public static int gameSpeed = 1;
}

重点是向用户隐藏死去的外星人,而不是将其排除在外采集。您的实现尝试清除被射击外星人的位置,但是在下一次迭代中,它再次初始化了所有入侵者。



还请注意,我被迫引入文字 Movement.MovementArmyOfInvaders 中的> 9 ,而不是调用 Invader.invader.Length 。

希望有帮助。


I'm making for learning purpose a space invaders clone. I'm stuck on the issue when an invader got shot. I'm not sure when there is a collision of a bullet with a part of the invader(who is a list of positions) how to erase it and the game to continue. Has anyone got an idea?

Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SpaceInvader
{
    class Program
    {

        static void Main()
        {
            Settings.ScreenSettings();
            ArmyOfInvaders.InitializeArmyOfInvaders();
            Player.InitializePlayer();
            int stepCount = 0;
            int armyOfInvadersSpeed = 50;
            while (true)
            {
                stepCount++;

                if (stepCount % armyOfInvadersSpeed == 0)
                {
                    Draw.EraseItem(ArmyOfInvaders.armyOfInvaders);

                    Invader.invader.Clear();
                    ArmyOfInvaders.armyOfInvaders.Clear();
                    ArmyOfInvaders.InitializeArmyOfInvaders(Movement.moveY, Movement.moveX);

                    Movement.MovementArmyOfInvaders();
                    stepCount = 0;
                }
                Console.CursorVisible = false;
                Draw.DrawItem(ArmyOfInvaders.armyOfInvaders);
                Draw.EraseItem(Player.player);
                Shoot.GenerateShot();

                Movement.MovementPlayer();
                Draw.DrawItem(Player.player);
                Draw.DrawShoot();
                Draw.EraseShoot();
                //Collision.InvaderGotShot();

                Thread.Sleep(Common.gameSpeed);

            }

        }

    }
    public class Settings
    {
        static public int maxRows = 50;
        static public int maxCols = 180;
        public static void ScreenSettings()
        {
            Console.CursorVisible = false;
            Console.BufferHeight = Console.WindowHeight = maxRows;
            Console.BufferWidth = Console.WindowWidth = maxCols;
        }
    }
    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;
        }
    }
    public class Player
    {
        public static List<Position> player = new List<Position>();
        public static int playerWide = 9;
        public static int playerLong = player.Count;

        public static List<Position> InitializePlayer(int row = 0, int col = 0)
        {


            int startrow = Settings.maxRows - 5 - playerLong;//start position row
            int startcol = (Settings.maxCols / 2) - (playerWide / 2);// start position col

            player.Add(new Position(startrow + row, startcol + col, 'A'));
            player.Add(new Position(startrow + 1 + row, startcol + col, 'o'));
            player.Add(new Position(startrow + 2 + row, startcol - 2 + col, '|'));
            player.Add(new Position(startrow + 2 + row, startcol + col, 'o'));
            player.Add(new Position(startrow + 2 + row, startcol + 2 + col, '|'));
            player.Add(new Position(startrow + 3 + row, startcol - 4 + col, '/'));
            player.Add(new Position(startrow + 3 + row, startcol - 3 + col, '\\'));
            player.Add(new Position(startrow + 3 + row, startcol - 2 + col, '\\'));
            player.Add(new Position(startrow + 3 + row, startcol - 1 + col, '\\'));
            player.Add(new Position(startrow + 3 + row, startcol + col, 'o'));
            player.Add(new Position(startrow + 3 + row, startcol + 1 + col, '/'));
            player.Add(new Position(startrow + 3 + row, startcol + 2 + col, '/'));
            player.Add(new Position(startrow + 3 + row, startcol + 3 + col, '/'));
            player.Add(new Position(startrow + 3 + row, startcol + 4 + col, '\\'));
            player.Add(new Position(startrow + 4 + row, startcol - 2 + col, '<'));
            player.Add(new Position(startrow + 4 + row, startcol - 1 + col, '/'));
            player.Add(new Position(startrow + 4 + row, startcol - 0 + col, 'o'));
            player.Add(new Position(startrow + 4 + row, startcol + 1 + col, '\\'));
            player.Add(new Position(startrow + 4 + row, startcol + 2 + col, '>'));
            return player;
        }
    }
    public class Movement
    {
        public static bool isRight = true;

        public static int moveX = 0;
        public static int moveY = 0;
        public static int left = -1;
        public static int right = 1;
        public static int armyOfInvaderJump = 5;
        public static void MovementArmyOfInvaders()
        {


            if (isRight)
            {
                moveX += armyOfInvaderJump;
                if (ArmyOfInvaders.armyOfInvaders[ArmyOfInvaders.armyOfInvaders.Count - 1][Invader.invader.Count - 1].Col >= Settings.maxCols - 20)
                {
                    isRight = false;
                    moveY += 2;
                }
            }
            else
            {
                moveX -= armyOfInvaderJump;
                if (ArmyOfInvaders.armyOfInvaders[0][0].Col <= 20)
                {
                    isRight = true;
                    moveY += 2;
                }
            }
        }

        public static void MovementPlayer()
        {


            while (Console.KeyAvailable)
            {
                ConsoleKeyInfo key = Console.ReadKey(true);
                while (Console.KeyAvailable)
                {
                    Console.ReadKey(true);
                }
                if (Player.player[0].Col - 6 >= 0)
                {
                    if (key.Key == ConsoleKey.LeftArrow)
                    {
                        for (int i = 0; i < Player.player.Count; i++)
                        {
                            Player.player[i] = new Position(Player.player[i].Row, Player.player[i].Col + left, Player.player[i].Symbol);
                        }
                    }
                }
                if (Player.player[0].Col + 7 < Settings.maxCols)
                {
                    if (key.Key == ConsoleKey.RightArrow)
                    {
                        for (int i = 0; i < Player.player.Count; i++)
                        {
                            Player.player[i] = new Position(Player.player[i].Row, Player.player[i].Col + right, Player.player[i].Symbol);
                        }
                    }
                }
                if (key.Key == ConsoleKey.Spacebar)
                {
                    Shoot.shoot[Player.player[0].Row - 1, Player.player[0].Col] = 1;
                }
            }

        }

    }
    class Shoot
    {
        public static int[,] shoot = new int[Settings.maxRows, Settings.maxCols];

        public static void GenerateShot()
        {
            for (int Row = 0; Row < Shoot.shoot.GetLength(0); Row++)
            {
                for (int Col = 0; Col < Shoot.shoot.GetLength(1); Col++)
                {
                    if (Shoot.shoot[Row, Col] == 1 && (Row == 0))
                    {
                        Shoot.shoot[Row, Col] = 0;

                    }
                    if (Shoot.shoot[Row, Col] == 1)
                    {
                        Shoot.shoot[Row, Col] = 0;
                        Shoot.shoot[Row - 1, Col] = 1;



                    }
                }
            }


        }
    }
    static public class Invader
    {
        public static List<Position> invader = new List<Position>();
        public static List<Position> InitializeInvader(int row, int col)
        {


            int startrow = 5;//start position row
            int startcol = ArmyOfInvaders.startArmyOfInvadersPosition;// 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;
        }




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

        }
        public static void DrawItem(List<List<Position>> armyOfInvaders)
        {
            foreach (List<Position> invader in armyOfInvaders)
            {
                Draw.DrawItem(invader);
            }
        }
        public static void EraseItem(List<List<Position>> armyOfInvaders)
        {
            foreach (List<Position> invader in armyOfInvaders)
            {
                foreach (Position part in invader)
                {
                    Console.SetCursorPosition(part.Col, part.Row);
                    Console.Write(' ');
                }
            }
        }
        public static void EraseItem(List<Position> invader)
        {

            foreach (Position part in invader)
            {
                Console.SetCursorPosition(part.Col, part.Row);
                Console.Write(' ');
            }

        }
        public static void DrawShoot()
        {
            for (int Row = 0; Row < Shoot.shoot.GetLength(0); Row++)
            {
                for (int Col = 0; Col < Shoot.shoot.GetLength(1); Col++)
                {
                    if (Shoot.shoot[Row, Col] == 1)
                    {
                        Console.SetCursorPosition(Col, Row);
                        Console.Write("|");

                    }
                }
            }
        }
        public static void EraseShoot()
        {
            for (int Row = 0; Row < Shoot.shoot.GetLength(0); Row++)
            {
                for (int Col = 0; Col < Shoot.shoot.GetLength(1); Col++)
                {
                    if (Row == 0)
                    {
                        Console.SetCursorPosition(Col, Row);
                        Console.Write(" ");
                    }
                    if (Shoot.shoot[Row, Col] == 1)
                    {
                        Console.SetCursorPosition(Col, Row + 1);
                        Console.Write(" ");
                    }

                }
            }
        }
    }
    public class Collision
    {

        public static void InvaderGotShot()
        {
            for (int i = 0; i < ArmyOfInvaders.armyOfInvaders.Count; i++)
            {
                for (int j = 0; j < Invader.invader.Count; j++)
                {
                    if (Shoot.shoot[ArmyOfInvaders.armyOfInvaders[i][j].Row, ArmyOfInvaders.armyOfInvaders[i][j].Col] == 1)
                    {

                        ArmyOfInvaders.armyOfInvaders[i].Clear();
                        for (int k = 0; k < Invader.invader.Count; k++)
                        {
                            ArmyOfInvaders.armyOfInvaders[i].Add(new Position(ArmyOfInvaders.armyOfInvaders[i][k].Row, ArmyOfInvaders.armyOfInvaders[i][k].Col, ' '));
                        }

                    }

                }
            }

        }
    }
    public class ArmyOfInvaders
    {

        public static int invadersColDistance = 9;
        public static int invadersColsCount = 10;
        public static int invadersRowDistance = 4;
        public static int invadersRowsCount = 4;

        public static int startArmyOfInvadersPosition = (Settings.maxCols - (ArmyOfInvaders.invadersColDistance * ArmyOfInvaders.invadersColsCount)) / 2 + 5;//5 e shiranata na edin invader

        public static List<List<Position>> armyOfInvaders = new List<List<Position>>();

        public static void InitializeArmyOfInvaders(int moveY = 0, int moveX = 0)
        {
            for (int row = 0; row < invadersRowDistance * invadersRowsCount; row += invadersRowDistance)
            {
                for (int col = 0; col < invadersColDistance * invadersColsCount; col += invadersColDistance)
                {
                    Invader.InitializeInvader(row + moveY, col + moveX);
                }
            }

            armyOfInvaders.Add(Invader.invader);

        }






    }
    public class Common
    {
        public static int gameSpeed = 1;
    }
}

Thank's in advance!

解决方案

Without claiming to rework your design, I added collection of dead invaders(ArmyOfInvaders.deadInvader) to stored those ones, who already shot, and changed InvaderGotShot and DrawItem methods. I got rid of Invader.invader collection and introduced local variable in Invader.InitializeInvader instead of it to prevent misassigning. Please take a look:

class Program
{

    static void Main()
    {
        Settings.ScreenSettings();
        ArmyOfInvaders.InitializeArmyOfInvaders();
        Player.InitializePlayer();
        int stepCount = 0;
        int armyOfInvadersSpeed = 50;
        while (true)
        {
            stepCount++;

            if (stepCount % armyOfInvadersSpeed == 0)
            {
                Draw.EraseItem(ArmyOfInvaders.armyOfInvaders);

                ArmyOfInvaders.armyOfInvaders.Clear();
                ArmyOfInvaders.InitializeArmyOfInvaders(Movement.moveY, Movement.moveX);

                Movement.MovementArmyOfInvaders();
                stepCount = 0;
            }
            Console.CursorVisible = false;
            Draw.DrawItem(ArmyOfInvaders.armyOfInvaders);
            Draw.EraseItem(Player.player);
            Shoot.GenerateShot();

            Movement.MovementPlayer();
            Draw.DrawItem(Player.player);
            Draw.DrawShoot();
            Draw.EraseShoot();
            Collision.InvaderGotShot();

            Thread.Sleep(Common.gameSpeed);

        }

    }

}
public class Settings
{
    static public int maxRows = 50;
    static public int maxCols = 180;
    public static void ScreenSettings()
    {
        Console.CursorVisible = false;
        Console.BufferHeight = Console.WindowHeight = maxRows;
        Console.BufferWidth = Console.WindowWidth = maxCols;
    }
}
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;
    }
}
public class Player
{
    public static List<Position> player = new List<Position>();
    public static int playerWide = 9;
    public static int playerLong = player.Count;

    public static List<Position> InitializePlayer(int row = 0, int col = 0)
    {


        int startrow = Settings.maxRows - 5 - playerLong;//start position row
        int startcol = (Settings.maxCols / 2) - (playerWide / 2);// start position col

        player.Add(new Position(startrow + row, startcol + col, 'A'));
        player.Add(new Position(startrow + 1 + row, startcol + col, 'o'));
        player.Add(new Position(startrow + 2 + row, startcol - 2 + col, '|'));
        player.Add(new Position(startrow + 2 + row, startcol + col, 'o'));
        player.Add(new Position(startrow + 2 + row, startcol + 2 + col, '|'));
        player.Add(new Position(startrow + 3 + row, startcol - 4 + col, '/'));
        player.Add(new Position(startrow + 3 + row, startcol - 3 + col, '\\'));
        player.Add(new Position(startrow + 3 + row, startcol - 2 + col, '\\'));
        player.Add(new Position(startrow + 3 + row, startcol - 1 + col, '\\'));
        player.Add(new Position(startrow + 3 + row, startcol + col, 'o'));
        player.Add(new Position(startrow + 3 + row, startcol + 1 + col, '/'));
        player.Add(new Position(startrow + 3 + row, startcol + 2 + col, '/'));
        player.Add(new Position(startrow + 3 + row, startcol + 3 + col, '/'));
        player.Add(new Position(startrow + 3 + row, startcol + 4 + col, '\\'));
        player.Add(new Position(startrow + 4 + row, startcol - 2 + col, '<'));
        player.Add(new Position(startrow + 4 + row, startcol - 1 + col, '/'));
        player.Add(new Position(startrow + 4 + row, startcol - 0 + col, 'o'));
        player.Add(new Position(startrow + 4 + row, startcol + 1 + col, '\\'));
        player.Add(new Position(startrow + 4 + row, startcol + 2 + col, '>'));
        return player;
    }
}
public class Movement
{
    public static bool isRight = true;

    public static int moveX = 0;
    public static int moveY = 0;
    public static int left = -1;
    public static int right = 1;
    public static int armyOfInvaderJump = 5;
    public static void MovementArmyOfInvaders()
    {


        if (isRight)
        {
            moveX += armyOfInvaderJump;
            if (ArmyOfInvaders.armyOfInvaders[ArmyOfInvaders.armyOfInvaders.Count - 1][9].Col >= Settings.maxCols - 20)
            {
                isRight = false;
                moveY += 2;
            }
        }
        else
        {
            moveX -= armyOfInvaderJump;
            if (ArmyOfInvaders.armyOfInvaders[0][0].Col <= 20)
            {
                isRight = true;
                moveY += 2;
            }
        }
    }

    public static void MovementPlayer()
    {


        while (Console.KeyAvailable)
        {
            ConsoleKeyInfo key = Console.ReadKey(true);
            while (Console.KeyAvailable)
            {
                Console.ReadKey(true);
            }
            if (Player.player[0].Col - 6 >= 0)
            {
                if (key.Key == ConsoleKey.LeftArrow)
                {
                    for (int i = 0; i < Player.player.Count; i++)
                    {
                        Player.player[i] = new Position(Player.player[i].Row, Player.player[i].Col + left, Player.player[i].Symbol);
                    }
                }
            }
            if (Player.player[0].Col + 7 < Settings.maxCols)
            {
                if (key.Key == ConsoleKey.RightArrow)
                {
                    for (int i = 0; i < Player.player.Count; i++)
                    {
                        Player.player[i] = new Position(Player.player[i].Row, Player.player[i].Col + right, Player.player[i].Symbol);
                    }
                }
            }
            if (key.Key == ConsoleKey.Spacebar)
            {
                Shoot.shoot[Player.player[0].Row - 1, Player.player[0].Col] = 1;
            }
        }

    }

}
class Shoot
{
    public static int[,] shoot = new int[Settings.maxRows, Settings.maxCols];

    public static void GenerateShot()
    {
        for (int Row = 0; Row < Shoot.shoot.GetLength(0); Row++)
        {
            for (int Col = 0; Col < Shoot.shoot.GetLength(1); Col++)
            {
                if (Shoot.shoot[Row, Col] == 1 && (Row == 0))
                {
                    Shoot.shoot[Row, Col] = 0;

                }
                if (Shoot.shoot[Row, Col] == 1)
                {
                    Shoot.shoot[Row, Col] = 0;
                    Shoot.shoot[Row - 1, Col] = 1;



                }
            }
        }


    }
}
static public class Invader
{
    public static List<Position> InitializeInvader(int row, int col)
    {
        int startrow = 5;//start position row
        int startcol = ArmyOfInvaders.startArmyOfInvadersPosition;// start position col

        List<Position> invader = new List<Position>();
        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;
    }




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

    }
    public static void DrawItem(List<List<Position>> armyOfInvaders)
    {
        for (var i = 0; i < armyOfInvaders.Count; i++)
        {
            if (!ArmyOfInvaders.deadInvaders.Contains(i))
            {
                Draw.DrawItem(armyOfInvaders[i]);
            }
        }
    }
    public static void EraseItem(List<List<Position>> armyOfInvaders)
    {
        foreach (List<Position> invader in armyOfInvaders)
        {
            foreach (Position part in invader)
            {
                Console.SetCursorPosition(part.Col, part.Row);
                Console.Write(' ');
            }
        }
    }
    public static void EraseItem(List<Position> invader)
    {

        foreach (Position part in invader)
        {
            Console.SetCursorPosition(part.Col, part.Row);
            Console.Write(' ');
        }

    }
    public static void DrawShoot()
    {
        for (int Row = 0; Row < Shoot.shoot.GetLength(0); Row++)
        {
            for (int Col = 0; Col < Shoot.shoot.GetLength(1); Col++)
            {
                if (Shoot.shoot[Row, Col] == 1)
                {
                    Console.SetCursorPosition(Col, Row);
                    Console.Write("|");

                }
            }
        }
    }
    public static void EraseShoot()
    {
        for (int Row = 0; Row < Shoot.shoot.GetLength(0); Row++)
        {
            for (int Col = 0; Col < Shoot.shoot.GetLength(1); Col++)
            {
                if (Row == 0)
                {
                    Console.SetCursorPosition(Col, Row);
                    Console.Write(" ");
                }
                if (Shoot.shoot[Row, Col] == 1)
                {
                    Console.SetCursorPosition(Col, Row + 1);
                    Console.Write(" ");
                }

            }
        }
    }
}
public class Collision
{

    public static void InvaderGotShot()
    {

        for (int i = 0; i < ArmyOfInvaders.armyOfInvaders.Count; i++)
        {
            for (int j = 0; j < ArmyOfInvaders.armyOfInvaders[i].Count; j++)
            {
                if (Shoot.shoot[ArmyOfInvaders.armyOfInvaders[i][j].Row, ArmyOfInvaders.armyOfInvaders[i][j].Col] == 1)
                {
                    if (!ArmyOfInvaders.deadInvaders.Contains(i))
                    {
                        Shoot.shoot[ArmyOfInvaders.armyOfInvaders[i][j].Row,
                            ArmyOfInvaders.armyOfInvaders[i][j].Col] = 0;
                        ArmyOfInvaders.deadInvaders.Add(i);
                        return;
                    }
                }

            }
        }

    }
}
public class ArmyOfInvaders
{

    public static int invadersColDistance = 9;
    public static int invadersColsCount = 10;
    public static int invadersRowDistance = 4;
    public static int invadersRowsCount = 4;
    public static List<int> deadInvaders = new List<int>();

    public static int startArmyOfInvadersPosition = (Settings.maxCols - (ArmyOfInvaders.invadersColDistance * ArmyOfInvaders.invadersColsCount)) / 2 + 5;//5 e shiranata na edin invader

    public static List<List<Position>> armyOfInvaders = new List<List<Position>>();

    public static void InitializeArmyOfInvaders(int moveY = 0, int moveX = 0)
    {
        for (int row = 0; row < invadersRowDistance * invadersRowsCount; row += invadersRowDistance)
        {
            for (int col = 0; col < invadersColDistance * invadersColsCount; col += invadersColDistance)
            {
                var invader = Invader.InitializeInvader(row + moveY, col + moveX);
                armyOfInvaders.Add(invader);
            }
        }

    }






}
public class Common
{
    public static int gameSpeed = 1;
}

The point was to hide dead aliens from user, but not to exclude them from collection. Your implementatiion tried to clear positions of shot alien, but on the next iteration it initialized all invaders again.

Also notice that I was forced to introduce literal 9 in Movement.MovementArmyOfInvaders instead of call Invader.invader.Length.

Hope it helps.

这篇关于射击。与列表碰撞的镜头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:42