6. Dots disappear.
Right now we can "eat" the dots, but they do not disappear, so we can eat them several times and get lots of points. That is not acceptable in most games, so we must mark them as "not visible", and not to draw them after that moment.
We can use a "boolean" variable to keep track of if a certain dot is visible:
bool visibleDot1 = true;
Now, before showing a dot on screen we should check if it is still visible:
if (visibleDot1) { Console.SetCursorPosition(xDot1, yDot1); Console.Write("."); }
And each time that there is a collision between our character and a dot, we must increase the score in 10 points, but also mark that dot as "not visible":
if ((x == xDot1) && (y == yDot1) && visibleDot1) { score += 10; visibleDot1 = false; }
The resulting program is simple:
/* First mini-console-game skeleton Version H: Dots disappear */ using System; public class Game01h { 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 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"); // Read keys and calculate new position 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 // TO DO // 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 // TO DO } } }
Suggested exercise: Instead of checking if the user has got 30 point for the game to end, check if no dots are visible.