Advertisement
simonradev

MiniSpaceInvader

Aug 19th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.02 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace MiniSpaceInvader
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             // Setting console width and height
  11.             int width = 35;
  12.             int height = 30;
  13.             Console.SetWindowSize(width, height);
  14.  
  15.             Console.BufferHeight = Console.WindowHeight;
  16.             Console.BufferWidth = Console.WindowWidth;
  17.  
  18.             // Setting cursor visability
  19.             Console.CursorVisible = false;
  20.  
  21.             // Setting the initial values of the spaceship
  22.             char spaceShip = '^';
  23.             int rowOfSpaceShip = Console.WindowHeight - 1;
  24.             int colOfSpaceShip = 0;
  25.  
  26.             int playerPoints = 0;
  27.  
  28.             char spaceShipProjectile = '|';
  29.  
  30.             // Setting up the enemy generator
  31.             Random enemyGenerator = new Random();
  32.             int minGeneratedRow = 0;
  33.             int maxGeneratedRow = Console.WindowHeight / 2;
  34.             int minGeneratedCol = 0;
  35.             int maxGeneratedCol = Console.WindowWidth;
  36.  
  37.             // Setting up the enemies
  38.             char enemy = '*';
  39.             int rowOfEnemy = enemyGenerator.Next(minGeneratedRow, maxGeneratedRow);
  40.             int colOfEnemy = enemyGenerator.Next(minGeneratedCol, maxGeneratedCol);
  41.  
  42.             // The game starts
  43.             Console.SetCursorPosition(colOfSpaceShip, rowOfSpaceShip);
  44.             Console.Write(spaceShip);
  45.            
  46.             Console.SetCursorPosition(colOfEnemy, rowOfEnemy);
  47.             Console.Write(enemy);
  48.  
  49.             while (true)
  50.             {
  51.                 ConsoleKeyInfo currentPressedKey = Console.ReadKey();
  52.                 if (currentPressedKey.Key == ConsoleKey.LeftArrow &&
  53.                     colOfSpaceShip >= 1)
  54.                 {
  55.                     // TODO: dvija <-
  56.                     colOfSpaceShip--;
  57.                 }
  58.                 else if (currentPressedKey.Key == ConsoleKey.RightArrow &&
  59.                          colOfSpaceShip <= Console.WindowWidth - 2)
  60.                 {
  61.                     // TODO: dvija ->
  62.                     colOfSpaceShip++;
  63.                 }
  64.                 else if (currentPressedKey.Key == ConsoleKey.Spacebar)
  65.                 {
  66.                     // TODO: shoot the enemy
  67.                     int rowOfProjectile = rowOfSpaceShip - 1;
  68.                     int colOfProjectile = colOfSpaceShip;
  69.                     while (rowOfProjectile > 0)
  70.                     {
  71.                         Console.Clear();
  72.  
  73.                         Console.SetCursorPosition(colOfProjectile, rowOfProjectile);
  74.                         Console.Write(spaceShipProjectile);
  75.  
  76.                         Console.SetCursorPosition(colOfSpaceShip, rowOfSpaceShip);
  77.                         Console.Write(spaceShip);
  78.  
  79.                         Console.SetCursorPosition(colOfEnemy, rowOfEnemy);
  80.                         Console.Write(enemy);
  81.  
  82.                         Thread.Sleep(70);
  83.  
  84.                         if (rowOfProjectile == rowOfEnemy &&
  85.                             colOfProjectile == colOfEnemy)
  86.                         {
  87.                             playerPoints++;
  88.  
  89.                             rowOfEnemy = enemyGenerator.Next(minGeneratedRow, maxGeneratedRow);
  90.                             colOfEnemy = enemyGenerator.Next(minGeneratedCol, maxGeneratedCol);
  91.  
  92.                             break;
  93.                         }
  94.  
  95.                         rowOfProjectile--;
  96.                     }
  97.  
  98.                     if (rowOfProjectile == 0)
  99.                     {
  100.                         goto PrintResult;
  101.                     }
  102.                 }
  103.  
  104.                 Console.Clear();
  105.  
  106.                 Console.SetCursorPosition(colOfSpaceShip, rowOfSpaceShip);
  107.                 Console.Write(spaceShip);
  108.  
  109.                 Console.SetCursorPosition(colOfEnemy, rowOfEnemy);
  110.                 Console.Write(enemy);
  111.             }
  112.  
  113.             PrintResult:
  114.             Console.WriteLine("YOU LOSST");
  115.             Console.WriteLine(playerPoints);
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement