Advertisement
vic_alexiev

FallingAsteroids.Game.cs

Nov 21st, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Threading;
  6.  
  7. class FallingRocks
  8. {
  9.     private static Stopwatch stopwatch = new Stopwatch();
  10.  
  11.     // keeps the number of the first line of asteroids
  12.     private static readonly int asteroidInitialSpawnSize = 10;
  13.  
  14.     // after how many iterations the next line of asteroids is spawned
  15.     private static int asteroidSpawnDelay = 5;
  16.  
  17.     // what's the maximum number of asteroids in a line
  18.     private static int asteroidSpawnUpperLimit = 8;
  19.  
  20.     // above this limit it is (almost) impossible to play due to the number of asteroids falling
  21.     private static readonly int asteroidSpawnMaxUpperLimit = 11;
  22.  
  23.  
  24.     private static readonly int sleepTimeout = 150; // milliseconds
  25.  
  26.     private static int spaceShipXCoord = 0;
  27.  
  28.     private static readonly Random numberGenerator = new Random();
  29.     private static char[] asteroidChars = new char[] { '^', '@', '*', '&', '+', '-', '%', '$', '#', '!', '.', ';' };
  30.     private static List<Asteroid> asteroids = new List<Asteroid>();
  31.  
  32.     private static void RemoveScrollbars()
  33.     {
  34.         Console.BufferHeight = Console.WindowHeight;
  35.         Console.BufferWidth = Console.WindowWidth;
  36.     }
  37.  
  38.     private static void DrawSpaceShip()
  39.     {
  40.         Console.ForegroundColor = ConsoleColor.White;
  41.         Console.SetCursorPosition(spaceShipXCoord, Console.WindowHeight - 1);
  42.         Console.Write("(O)");
  43.     }
  44.  
  45.     private static void MoveSpaceShipLeft()
  46.     {
  47.         if (spaceShipXCoord > 0)
  48.         {
  49.             spaceShipXCoord--;
  50.         }
  51.     }
  52.  
  53.     private static void MoveSpaceShipRight()
  54.     {
  55.         if (spaceShipXCoord + 2 < Console.WindowWidth - 1)
  56.         {
  57.             spaceShipXCoord++;
  58.         }
  59.     }
  60.  
  61.     private static void SetSpaceShipInitialLocation()
  62.     {
  63.         spaceShipXCoord = Console.WindowWidth / 2;
  64.     }
  65.  
  66.     private static void CreateAsteroid()
  67.     {
  68.         int x = numberGenerator.Next(Console.WindowWidth);
  69.         int charIndex = numberGenerator.Next(asteroidChars.Length);
  70.  
  71.         // the enumeration ConsoleColor contains 16 colors, of which the first (black) is not used
  72.         int color = numberGenerator.Next(1, 16);
  73.  
  74.         Asteroid asteroid = new Asteroid(new Point(x, 0), asteroidChars[charIndex], (ConsoleColor)color);
  75.         asteroids.Add(asteroid);
  76.     }
  77.  
  78.     private static void InsertAsteroids(int count)
  79.     {
  80.         for (int i = 0; i < count; i++)
  81.         {
  82.             CreateAsteroid();
  83.         }
  84.     }
  85.  
  86.     private static void ClearAsteroids()
  87.     {
  88.         asteroids.Clear();
  89.     }
  90.  
  91.     private static void DeleteAsteroidsBelowTheLine()
  92.     {
  93.         for (int i = asteroids.Count - 1; i >= 0; i--)
  94.         {
  95.             if (asteroids[i].Location.Y > Console.WindowHeight - 1)
  96.             {
  97.                 asteroids.RemoveAt(i);
  98.             }
  99.         }
  100.     }
  101.  
  102.     private static void Draw(Asteroid asteroid)
  103.     {
  104.         Console.SetCursorPosition(asteroid.Location.X, asteroid.Location.Y);
  105.         Console.ForegroundColor = asteroid.Color;
  106.         Console.Write(asteroid.Character);
  107.     }
  108.  
  109.     private static bool UpdateAsteroidsLocation()
  110.     {
  111.         foreach (Asteroid asteroid in asteroids)
  112.         {
  113.             asteroid.UpdateLocation();
  114.             if (asteroid.Location.Y == Console.WindowHeight &&
  115.                 asteroid.Location.X >= spaceShipXCoord &&
  116.                 asteroid.Location.X <= spaceShipXCoord + 2)
  117.             {
  118.                 // an asteroid has hit the space ship - game over
  119.                 return false;
  120.             }
  121.         }
  122.  
  123.         return true;
  124.     }
  125.  
  126.     private static void DrawAsteroids()
  127.     {
  128.         foreach (Asteroid asteroid in asteroids)
  129.         {
  130.             Draw(asteroid);
  131.         }
  132.     }
  133.  
  134.     private static void PrintGameOver(int secondsElapsed)
  135.     {
  136.         Console.SetCursorPosition(Console.WindowWidth / 2, Console.WindowHeight / 2);
  137.         Console.ForegroundColor = ConsoleColor.Red;
  138.         Console.WriteLine("Game Over!!! Your score: {0}", secondsElapsed * 100);
  139.     }
  140.  
  141.     static void Main()
  142.     {
  143.         RemoveScrollbars();
  144.         SetSpaceShipInitialLocation();
  145.         InsertAsteroids(asteroidInitialSpawnSize);
  146.         DrawAsteroids();
  147.  
  148.         int spawnTrigger = 0;
  149.         int spawnSize = 0;
  150.  
  151.         stopwatch.Start();
  152.  
  153.         while (true)
  154.         {
  155.             if (Console.KeyAvailable)
  156.             {
  157.                 ConsoleKeyInfo keyInfo = Console.ReadKey();
  158.                 if (keyInfo.Key == ConsoleKey.LeftArrow)
  159.                 {
  160.                     MoveSpaceShipLeft();
  161.                 }
  162.                 if (keyInfo.Key == ConsoleKey.RightArrow)
  163.                 {
  164.                     MoveSpaceShipRight();
  165.                 }
  166.             }
  167.  
  168.             spawnTrigger++;
  169.  
  170.             if (!UpdateAsteroidsLocation())
  171.             {
  172.                 stopwatch.Stop();
  173.                 PrintGameOver(stopwatch.Elapsed.Seconds);
  174.  
  175.                 // start a new game and restart the stopwatch
  176.                 Console.ReadKey();
  177.                 SetSpaceShipInitialLocation();
  178.                 ClearAsteroids();
  179.  
  180.                 // increase the level of difficulty - spawn asteroids more frequently
  181.                 // and in bigger quantities
  182.                 spawnTrigger = 0;
  183.                 if (asteroidSpawnDelay > 1)
  184.                 {
  185.                     asteroidSpawnDelay--;                    
  186.                 }
  187.  
  188.                 if (asteroidSpawnUpperLimit < asteroidSpawnMaxUpperLimit)
  189.                 {
  190.                     asteroidSpawnUpperLimit++;
  191.                 }
  192.  
  193.                 stopwatch.Restart();
  194.             }
  195.             else
  196.             {
  197.                 DeleteAsteroidsBelowTheLine();
  198.             }
  199.  
  200.             Console.Clear();
  201.             DrawSpaceShip();
  202.             DrawAsteroids();
  203.  
  204.             if (spawnTrigger == asteroidSpawnDelay)
  205.             {
  206.                 spawnTrigger = 0;
  207.                 spawnSize = numberGenerator.Next(asteroidSpawnUpperLimit);
  208.                 InsertAsteroids(spawnSize);
  209.             }
  210.  
  211.             Thread.Sleep(sleepTimeout);
  212.         }
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement