Venciity

[Jumpstart C#] - Kamikadze game

Jan 27th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace kamikadze
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.CursorVisible = true;
  14.            
  15.             int target1 = 5;
  16.             int target2 = 17;
  17.             int target3 = 12;
  18.  
  19.             Console.SetWindowSize(20, 25);
  20.  
  21.             int tries = 2;
  22.             int score = 0;
  23.  
  24.             while (true)
  25.             {
  26.                 Console.Clear();
  27.  
  28.                 PrintTarget(target1);
  29.                 PrintTarget(target2);
  30.                 PrintTarget(target3);
  31.  
  32.                 int x = Console.WindowWidth / 2;
  33.                 int y = Console.WindowHeight - 1;
  34.  
  35.                 PrintPlayer(x, y);
  36.  
  37.                 string title = String.Format("Tries remaining: {0}, score: {1}", tries, score);
  38.  
  39.                 Console.Title = title;
  40.                
  41.                 var key = Console.ReadKey();
  42.  
  43.                 if (key.Key != ConsoleKey.UpArrow)
  44.                 {
  45.                     Console.CursorLeft--;
  46.                     continue;
  47.                 }
  48.  
  49.                 while (true)
  50.                 {
  51.                     System.Threading.Thread.Sleep(80);
  52.  
  53.                     if (Console.KeyAvailable)
  54.                     {
  55.                         key = Console.ReadKey();
  56.                         if (key.Key == ConsoleKey.LeftArrow)
  57.                         {
  58.                             if (x > 0)
  59.                             {
  60.                                 x--;
  61.                                 Console.Write(" ");
  62.                             }
  63.                         }
  64.                         else if (key.Key == ConsoleKey.RightArrow)
  65.                         {
  66.                             if (x < Console.WindowWidth - 1)
  67.                             {
  68.                                 x++;
  69.                                 Console.Write(" ");
  70.                             }
  71.                         }
  72.                     }
  73.  
  74.                     Console.SetCursorPosition(x, y);
  75.                     Console.Write(" ");
  76.                     y--;
  77.  
  78.                     if (y < 0)
  79.                         break;
  80.                     else if (y == 0)
  81.                     {
  82.                         if (x == target1)
  83.                         {
  84.                             target1 = -1;
  85.                             score++;
  86.                         }
  87.                         else if (x == target2)
  88.                         {
  89.                             target2 = -1;
  90.                             score++;
  91.                         }
  92.                         else if (x == target3)
  93.                         {
  94.                             target3 = -1;
  95.                             score++;
  96.                         }
  97.                         else
  98.                         {
  99.                             tries--;
  100.                             if( tries == 0)
  101.                             {
  102.                                 PrintResult("You lost!");
  103.                                 return;
  104.                             }
  105.                         }
  106.  
  107.                         if (score == 3)
  108.                         {
  109.                             PrintResult("You won!");
  110.                             return;
  111.                         }
  112.                     }
  113.  
  114.                     PrintPlayer(x, y);
  115.                 }
  116.             }
  117.         }
  118.  
  119.         private static void PrintResult(string result)
  120.         {
  121.             Console.Clear();
  122.             Console.SetCursorPosition(
  123.                 Console.WindowWidth / 2 - result.Length / 2,
  124.             Console.WindowHeight / 2);
  125.             Console.WriteLine(result);
  126.             Console.CursorVisible = false;
  127.             Console.ReadKey();
  128.         }
  129.  
  130.         private static void PrintTarget(int x)
  131.         {
  132.             if (x > -1 &&  x < Console.WindowWidth)
  133.             {
  134.                 Console.SetCursorPosition(x, 0);
  135.                 Console.Write("*");
  136.             }
  137.         }
  138.  
  139.         private static void PrintPlayer(int x, int y)
  140.         {
  141.             Console.SetCursorPosition(x, y);
  142.             Console.Write("+");
  143.             Console.CursorLeft--;
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment