Untitled
unknown
plain_text
12 days ago
4.1 kB
6
Indexable
class Program { static int punteggio = 0; static string PLAYER = "X"; static string OBSTACLE = "V"; static int POSIZIONE_PLAYER = 1; static string[] griglia = new string[] { "___", "___", "___", "___", "___", "___", "___", "___", "___", "___" }; static bool ÈIlGiocoFinito() { if (POSIZIONE_PLAYER == 0 && griglia[9] == "V__") { return true; } else if (POSIZIONE_PLAYER == 1 && griglia[9] == "_V_") { return true; } else if (POSIZIONE_PLAYER == 2 && griglia[9] == "__V") { return true; } return false; } static void CadutaOstacoli() { string[] nuovaGriglia = new string[] { "___", "___", "___", "___", "___", "___", "___", "___", "___", "___" }; nuovaGriglia[1] = griglia[0]; nuovaGriglia[2] = griglia[1]; nuovaGriglia[3] = griglia[2]; nuovaGriglia[4] = griglia[3]; nuovaGriglia[5] = griglia[4]; nuovaGriglia[6] = griglia[5]; nuovaGriglia[7] = griglia[6]; nuovaGriglia[8] = griglia[7]; nuovaGriglia[9] = griglia[8]; griglia = nuovaGriglia; } static void GeneraOstacolo() { string rigaInAlto = ""; Random random = new Random(); int spawnOstacolo = random.Next(1, 11); // probabilita 60% --> 6 possibiltia su 10 if (spawnOstacolo > 6) { //Inserisco logica spawn ostacolo int posizioneOstacolo = random.Next(0, 3); // numero 0 , 1 o 2 for (int i = 0; i < 3; i++) { if (i == posizioneOstacolo) { rigaInAlto += "V"; } else { rigaInAlto += "_"; } } } else { rigaInAlto = "___"; } griglia[0] = rigaInAlto; } static void StampaGriglia() { Console.Clear(); //cancello quello che c'è sul terminale Console.WriteLine($"Punteggio: {punteggio}"); for (int i = 0; i < 10; i++) { Console.WriteLine(griglia[i]); } } static void AggiornaGriglia() { string rigaPlayer = ""; for (int i = 0; i < 3; i++) { if (i == POSIZIONE_PLAYER) { rigaPlayer += "X"; } else { rigaPlayer += "_"; } } griglia[9] = rigaPlayer; } static void Main(string[] args) { for (; ; ) { if (Console.KeyAvailable) { // Un tasto è stato premuto //ConsoleKeyInfo tasto della tastiera; ReadKey --> leggi tasto! ConsoleKeyInfo tastoPremuto = Console.ReadKey(true); // 0 --> sx, 1 --> centro, 2 --> dx if (tastoPremuto.Key == ConsoleKey.RightArrow) { if (POSIZIONE_PLAYER < 2) { POSIZIONE_PLAYER++; } else //POSIZIONE_PLAYER vale 2 e sei a destra { POSIZIONE_PLAYER = 0; } } if (tastoPremuto.Key == ConsoleKey.LeftArrow) { if (POSIZIONE_PLAYER > 0) { POSIZIONE_PLAYER--; } else { POSIZIONE_PLAYER = 2; } } } CadutaOstacoli(); GeneraOstacolo(); bool gameOver = ÈIlGiocoFinito(); if (gameOver == true) { Console.WriteLine("Hai Perso! :( "); return; } AggiornaGriglia(); StampaGriglia(); Thread.Sleep(500); punteggio = punteggio + 500; } } }
Editor is loading...
Leave a Comment