Julian Docev
Julian Docev

Reputation: 119

Space invaders aliens rows

For learning purpose I want to make a console clone on c# of space invaders. I'm stuck on the issue of how to make the rows of invaders. There must 4 rows with 6 invaders for example. I have managed to make one invader as a list of structure where I put the x and y coordinate and the character. My question is: How can I make 4 rows with 6 invaders of that type so they can be printed on the console every one with different coordinates. this is an example of my invader:

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;

    }
}

Upvotes: 1

Views: 349

Answers (1)

KozhevnikovDmitry
KozhevnikovDmitry

Reputation: 1720

Try to change your Main method in this way to make picture better:

static void Main()
{
    ScreenSettings();

    while (true)
    {
        invader.Clear();
        InitializeInvaders(moveY, moveX);
        DrawInvaders();
        Console.Clear();
        Thread.Sleep(10);
        moveX++;
    }
}

The point is, that you have to clear the previous positions before redrawing aliens. And you don't need call InitializeInvaders and DrawInvaders twice in Main.

I agree with Dour High Arch, that it would be better with Invader class. And two another pieces of advice:

  1. Don't name you local variables just like global statics: invader, moveX, moveY.
  2. Try to go away from global static variables. Encapsulate this state within classes, hide it in private fields, make those class be responsible for modification of their state in order to make code more structured and comprehensible.

Hope it helps. P.S. Nice aliens=)

Upvotes: 2

Related Questions