Reputation: 119
For learning purpose I want to make a console clone on c# of space invaders. I'm stuck on the issue of the movement of the rows of invaders. I have made them and I print them but when i try to move them in a direction they are loosing speed with the time and I can't understand why. Does anyone have an idea? Here is my code so far:
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;
}
}
}
Upvotes: 0
Views: 393
Reputation: 156
You seem to call InitializeInvaders all the time in the main loop. This function adds invaders to the invader list (without s!), but it does not delete the old ones. Because the invaders may be on the same position like the old ones, you may not see them on the screen.
[Edit: Ah I see, it gets deleted in the main loop but: ]
Also, you then add this whole list (invader) to the invaderS list, without clearing invaderS before. This lists will grow and grow...
tl;dr: You forgot to clear the invaderS list.
But I think you have something other in mind. The invaders should not be created newly every time just when they change position. You should call InitializeInvaders just once and then manipulate the items in the list directly over an index. (invader[i].posX = x...) where 'i' (by default) is ((y * x) + x) (that is: y times a line plus the x position on the last line.)
To make it clear: you add them like this (pseudocode):
for(y = 0 to maxy)
{
for(x = 0 to maxx)
{
invaders.add(new invader(x, y));
}
}
Then you have each 'i' = exactly the (start) y*x + x position. ;)
And then you have a global x, y which is added to this position, job done. You only have to change this global x, y in the loop and all the invaders move...
I hope this helps a bit.
This is my first answer on stackoverflow so please don't hate me to much.
Upvotes: 3