10. Several enemies

If we want our game to have several enemies, we can use arrays for their "x" and "y" coordinates:

float[] xEnemy = { 15, 40, 50, 60 };
float[] yEnemy = { 5, 10, 15, 20 };

Also, they should not have the same speed (increment for X) all of them, or they would be moving in the same direction at any moment, so they speeds (increments) should be independent:

float[] incrXEnemy = { 0.4f, 0.35f, 0.6f, 0.3f };

In the "drawing" part of our game loop, we must display now several enemies, instead of one. We can use a "for" statement for that:

for(int i=0; i<amountOfEnemies; i++)
{
    Console.SetCursorPosition((int)xEnemy[i], (int)yEnemy[i]);
    Console.Write("A");
}

And we should "move" (calculate the new position for) all of them:

for(int i=0; i<amountOfEnemies; i++)
{
    xEnemy[i] += incrXEnemy[i];
    if ((xEnemy[i] < 1) || (xEnemy[i] > 78))
        incrXEnemy[i] = -incrXEnemy[i];
}


So we might get:

/*
  First mini-console-game skeleton
  Version L: Several enemies, using an array
*/
 
using System;
using System.Threading;  // For the pause with Thread.Sleep
 
public class Game01l
{
    public static void Main()
    {
        int x=40, y=12;
        int xDot1=10, yDot1=5;
        bool visibleDot1 = true;
        int xDot2=20, yDot2=15;
        bool visibleDot2 = true;
        int xDot3=60, yDot3=11;
        bool visibleDot3 = true;
 
        int amountOfEnemies = 4;
        float[] xEnemy = { 15, 40, 50, 60 };
        float[] yEnemy = { 5, 10, 15, 20 };
        float[] incrXEnemy = { 0.4f, 0.35f, 0.6f, 0.3f };
 
        int score = 0;
        ConsoleKeyInfo userKey;
 
        // Game Loop
        while( 1 == 1 )
        {
            // Draw
            Console.Clear();
            Console.Write("Score: {0}",score);
 
            if (visibleDot1)
            {
                Console.SetCursorPosition(xDot1, yDot1);
                Console.Write(".");
            }
 
            if (visibleDot2)
            {
                Console.SetCursorPosition(xDot2, yDot2);
                Console.Write(".");
            }
 
            if (visibleDot3)
            {
                Console.SetCursorPosition(xDot3, yDot3);
                Console.Write(".");
            }
 
            Console.SetCursorPosition(x, y);
            Console.Write("C");
 
            for(int i=0; i<amountOfEnemies; i++)
            {
                Console.SetCursorPosition((int)xEnemy[i], (int)yEnemy[i]);
                Console.Write("A");
            }
 
            // Read keys and calculate new position
            if (Console.KeyAvailable)
            {
                userKey = Console.ReadKey(false);
 
                if(userKey.Key == ConsoleKey.RightArrow) x++;
                if(userKey.Key == ConsoleKey.LeftArrow) x--;
                if(userKey.Key == ConsoleKey.DownArrow) y++;
                if(userKey.Key == ConsoleKey.UpArrow) y--;
            }
 
            // Move enemies and environment
            for(int i=0; i<amountOfEnemies; i++)
            {
                xEnemy[i] += incrXEnemy[i];
                if ((xEnemy[i] < 1) || (xEnemy[i] > 78))
                    incrXEnemy[i] = -incrXEnemy[i];
            }
 
            // Collisions, lose energy or lives, etc
            if ((x == xDot1) && (y == yDot1) && visibleDot1)
            {
                score += 10;
                visibleDot1 = false;
            }
 
            if ((x == xDot2) && (y == yDot2) && visibleDot2)
            {
                score += 10;
                visibleDot2 = false;
            }
 
            if ((x == xDot3) && (y == yDot3) && visibleDot3)
            {
                score += 10;
                visibleDot3 = false;
            }
 
            // Pause till next fotogram
            Thread.Sleep(40);
        }
    }
}
 

Which would look like this:

Appearance of Game01l