Advertisement
PrezesSmoku

L15_BrickGame_R

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