Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace MiniSpaceInvader
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Setting console width and height
- int width = 35;
- int height = 30;
- Console.SetWindowSize(width, height);
- Console.BufferHeight = Console.WindowHeight;
- Console.BufferWidth = Console.WindowWidth;
- // Setting cursor visability
- Console.CursorVisible = false;
- // Setting the initial values of the spaceship
- char spaceShip = '^';
- int rowOfSpaceShip = Console.WindowHeight - 1;
- int colOfSpaceShip = 0;
- int playerPoints = 0;
- char spaceShipProjectile = '|';
- // Setting up the enemy generator
- Random enemyGenerator = new Random();
- int minGeneratedRow = 0;
- int maxGeneratedRow = Console.WindowHeight / 2;
- int minGeneratedCol = 0;
- int maxGeneratedCol = Console.WindowWidth;
- // Setting up the enemies
- char enemy = '*';
- int rowOfEnemy = enemyGenerator.Next(minGeneratedRow, maxGeneratedRow);
- int colOfEnemy = enemyGenerator.Next(minGeneratedCol, maxGeneratedCol);
- // The game starts
- Console.SetCursorPosition(colOfSpaceShip, rowOfSpaceShip);
- Console.Write(spaceShip);
- Console.SetCursorPosition(colOfEnemy, rowOfEnemy);
- Console.Write(enemy);
- while (true)
- {
- ConsoleKeyInfo currentPressedKey = Console.ReadKey();
- if (currentPressedKey.Key == ConsoleKey.LeftArrow &&
- colOfSpaceShip >= 1)
- {
- // TODO: dvija <-
- colOfSpaceShip--;
- }
- else if (currentPressedKey.Key == ConsoleKey.RightArrow &&
- colOfSpaceShip <= Console.WindowWidth - 2)
- {
- // TODO: dvija ->
- colOfSpaceShip++;
- }
- else if (currentPressedKey.Key == ConsoleKey.Spacebar)
- {
- // TODO: shoot the enemy
- int rowOfProjectile = rowOfSpaceShip - 1;
- int colOfProjectile = colOfSpaceShip;
- while (rowOfProjectile > 0)
- {
- Console.Clear();
- Console.SetCursorPosition(colOfProjectile, rowOfProjectile);
- Console.Write(spaceShipProjectile);
- Console.SetCursorPosition(colOfSpaceShip, rowOfSpaceShip);
- Console.Write(spaceShip);
- Console.SetCursorPosition(colOfEnemy, rowOfEnemy);
- Console.Write(enemy);
- Thread.Sleep(70);
- if (rowOfProjectile == rowOfEnemy &&
- colOfProjectile == colOfEnemy)
- {
- playerPoints++;
- rowOfEnemy = enemyGenerator.Next(minGeneratedRow, maxGeneratedRow);
- colOfEnemy = enemyGenerator.Next(minGeneratedCol, maxGeneratedCol);
- break;
- }
- rowOfProjectile--;
- }
- if (rowOfProjectile == 0)
- {
- goto PrintResult;
- }
- }
- Console.Clear();
- Console.SetCursorPosition(colOfSpaceShip, rowOfSpaceShip);
- Console.Write(spaceShip);
- Console.SetCursorPosition(colOfEnemy, rowOfEnemy);
- Console.Write(enemy);
- }
- PrintResult:
- Console.WriteLine("YOU LOSST");
- Console.WriteLine(playerPoints);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement