Advertisement
PrezesSmoku

L15_BrickGame_P

Apr 21st, 2023
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace BrickGame
  8. {
  9.     class Program
  10.     {
  11.         static string[] plansza;
  12.         const string GRACZ = "^";
  13.         const string PRZESZKODA = "#";
  14.         static void Main(string[] args)
  15.         {  
  16.             int pozycjaGracza = 1;
  17.             Random generatorLosowy = new Random();
  18.             bool czyUderzony = false;
  19.             NowaPlansza(10);
  20.             UstawGracza(pozycjaGracza);
  21.             PokazPlansze();
  22.             //Pętla
  23.             while (!czyUderzony)
  24.             {
  25.                 //sterowanie
  26.                 if (Console.KeyAvailable)
  27.                 {
  28.                     ConsoleKeyInfo nacisnietyKlawisz = Console.ReadKey(true);
  29.                     if (nacisnietyKlawisz.Key == ConsoleKey.RightArrow)
  30.                     {
  31.                         if (pozycjaGracza < 2)
  32.                         {
  33.                             pozycjaGracza++;
  34.                         }
  35.                     }
  36.                     if (nacisnietyKlawisz.Key == ConsoleKey.LeftArrow)
  37.                     {
  38.                         if (pozycjaGracza > 0)
  39.                         {
  40.                             pozycjaGracza--;
  41.                         }
  42.                     }
  43.                 }
  44.  
  45.                 //sprawdzenie uderzenia
  46.                 int pozycjaNajblizszejPrzeszkody = plansza[plansza.Length - 2].IndexOf(PRZESZKODA);
  47.                 if (pozycjaGracza == pozycjaNajblizszejPrzeszkody)
  48.                 {
  49.                     czyUderzony = true;
  50.                 }
  51.  
  52.                
  53.                 //nowa przeszkoda
  54.                 int pozycjaPrzeszkody = generatorLosowy.Next(3);
  55.                 string przeszkoda = UstawPrzeszkode(pozycjaPrzeszkody);
  56.  
  57.                 //przesunięcie planszy w dół
  58.                 for (int i = plansza.Length - 2; i > 0; i--)
  59.                 {
  60.                     plansza[i] = plansza[i - 1];
  61.                 }
  62.                 plansza[0] = przeszkoda;
  63.  
  64.                 UstawGracza(pozycjaGracza);
  65.                 PokazPlansze();
  66.                 Thread.Sleep(600);
  67.                
  68.             }
  69.  
  70.  
  71.             Console.Clear();
  72.             Console.WriteLine("GAME OVER");
  73.  
  74.  
  75.             Console.ReadKey();
  76.         }
  77.  
  78.         private static void NowaPlansza(int rozmiarPlanszy)
  79.         {
  80.             plansza = new string[rozmiarPlanszy];
  81.             for (int i = 0; i < plansza.Length; i++)
  82.             {
  83.                 plansza[i] = "";
  84.             }
  85.         }
  86.  
  87.         private static string UstawPrzeszkode(int pozycja)
  88.         {
  89.             string linia = "   ";
  90.             linia = linia.Insert(pozycja, PRZESZKODA);
  91.             return linia;
  92.         }
  93.  
  94.        
  95.         private static void UstawGracza(int pozycja)
  96.         {
  97.             string linia = "   "; //w cudzysłowie 3 spacje
  98.             linia = linia.Insert(pozycja, GRACZ);
  99.             plansza[plansza.Length - 1] = linia;
  100.         }
  101.  
  102.  
  103.  
  104.         private static void PokazPlansze()
  105.         {
  106.             Console.Clear();
  107.             for (int i = 0; i < plansza.Length; i++)
  108.             {
  109.                 Console.WriteLine(plansza[i]);
  110.             }
  111.         }
  112.     }
  113. }
  114.  
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement