Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
1,274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8.  
  9. class EscapeFloor
  10. {
  11.     struct Player
  12.     {
  13.         public int X1;
  14.         public int X2;
  15.         public int Y1;
  16.         public int Y2;
  17.         public int Points;
  18.         public string[] Skin;
  19.     }
  20.     struct PowerUps
  21.     {
  22.         public string Symbol;
  23.         public int X;
  24.         public int Y;
  25.         public ConsoleColor Color;
  26.  
  27.         public PowerUps(string symbol, int x, int y, ConsoleColor color)
  28.         {
  29.             this.Symbol = symbol;
  30.             this.X = x;
  31.             this.Y = y;
  32.             this.Color = color;
  33.         }
  34.     }
  35.  
  36.     static void Main()
  37.     {
  38.         int numberOfFloors = 30;
  39.         Console.BufferHeight = Console.WindowHeight = numberOfFloors;
  40.         Console.BufferWidth = Console.WindowWidth = 50;
  41.         Random rng = new Random();
  42.         StringBuilder floor = new StringBuilder();
  43.         Player player1 = new Player();
  44.         string scoreFileDir = Directory.GetCurrentDirectory() + @"\score.txt";
  45.         string[] floors = new string[Console.WindowHeight + 1];
  46.  
  47.         int floorsLength = Console.BufferWidth - 20;
  48.         int emptyRows = 3;
  49.  
  50.         PowerUps[] powerUp = new PowerUps[3];
  51.  
  52.         powerUp[0] = new PowerUps("@", 1, 1, ConsoleColor.Green);
  53.         powerUp[1] = new PowerUps("$", 1, 1, ConsoleColor.Yellow);
  54.         powerUp[2] = new PowerUps("!", 1, 1, ConsoleColor.Red);
  55.  
  56.         int fallingCounter = 0;
  57.         bool hasFallen = false;
  58.         bool isGameOver = false;
  59.         int[] holePosition = new int[numberOfFloors];
  60.         int[,] playfield = new int[Console.BufferHeight, Console.BufferWidth - 20];
  61.         string[] difficulty = { "Easy", "Medium", "Hard" };
  62.         string[] skin = { "_O_", " | ", @"/ \" };
  63.         player1.Skin = skin;
  64.         int holeWidth = 8;
  65.         int holeX = 0;
  66.  
  67.         int floorsCounter = 0;
  68.  
  69.         int levelSpeed = 350;
  70.  
  71.         int currentPlayerRow = 0;
  72.  
  73.         /**/
  74.         player1.X1 = playfield.GetLength(1) / 2; //start at the horizontal center of the playfield
  75.         player1.X2 = player1.X1 + player1.Skin.Length - 1;
  76.         player1.Y1 = playfield.GetLength(0) - 15;
  77.         player1.Y2 = player1.Y1 + player1.Skin.Length - 1;
  78.         /**/
  79.  
  80.         for (int i = 0; i < playfield.GetLength(0); i++)
  81.         {
  82.             Console.SetCursorPosition(playfield.GetLength(1), i);
  83.             Console.Write("|");
  84.         }
  85.  
  86.         while (!isGameOver)
  87.         {
  88.  
  89.             for (int i = 1; i < player1.Skin.Length; i++)
  90.             {
  91.                 for (int j = 0; j < player1.Skin.Length; j++)
  92.                 {
  93.                     playfield[player1.Y1 + i, player1.X1 + j] = 2;
  94.                 }
  95.             }
  96.  
  97.             //row generator
  98.             if (floorsCounter % 4 == 0)
  99.             {
  100.                 holeX = rng.Next(0, playfield.GetLength(1) - 5);
  101.                 for (int i = 0; i < playfield.GetLength(1); i++)
  102.                 {
  103.                     if (i >= holeX && i < holeX + holeWidth)
  104.                     {
  105.                         playfield[playfield.GetLength(0) - 1, i] = 3;
  106.                     }
  107.                     else
  108.                     {
  109.                         playfield[playfield.GetLength(0) - 1, i] = 1;
  110.                     }
  111.                 }
  112.                 floorsCounter = 0;
  113.             }
  114.  
  115.             //rows moving
  116.             for (int Y = 0; Y < playfield.GetLength(0); Y++)
  117.             {
  118.                 for (int X = 0; X < playfield.GetLength(1); X++)
  119.                 {
  120.                     //destroying the row when it reaches the top
  121.                     if (playfield[0, X] != 0)
  122.                     {
  123.                         for (int i = 0; i < playfield.GetLength(1); i++)
  124.                         {
  125.                             playfield[0, X] = 0;
  126.                             Console.SetCursorPosition(X, 0);
  127.                             Console.Write(" ");
  128.                         }
  129.                     }
  130.                     if (playfield[Y, X] == 3)
  131.                     {
  132.                         playfield[Y - 1, X] = 3;
  133.                         playfield[Y, X] = 0;
  134.                     }
  135.                     if (playfield[Y, X] == 1)
  136.                     {
  137.                         Console.SetCursorPosition(X, Y - 1);
  138.                         Console.Write("=");
  139.                           playfield[Y - 1, X] = 1;
  140.                         Console.SetCursorPosition(X, Y);
  141.                         Console.Write(" ");
  142.                         playfield[Y, X] = 0;
  143.                         //print character
  144.                         PrintCharacter(player1);
  145.                     }
  146.                      if (playfield[Y, X] == 0)
  147.                      {
  148.                          Console.SetCursorPosition(X, Y);
  149.                          Console.Write(" ");
  150.                      }
  151.  
  152.                 }
  153.             }
  154.  
  155.             floorsCounter++;
  156.             Thread.Sleep(levelSpeed);
  157.  
  158.             //move through holes
  159.             for (int i = 0; i < player1.Skin.Length; i++)
  160.             {
  161.                 if (playfield[player1.Y2 + 1, player1.X1 + i] == 1 && player1.Y2 < 26)
  162.                 {
  163.                     for (int j = 0; j < player1.Skin[0].Length; j++)
  164.                     {
  165.                         Console.SetCursorPosition(player1.X1 + j, player1.Y1 - 1);
  166.                         Console.Write(" ");
  167.                     }
  168.                     player1.Y1--;
  169.                     break;
  170.                 }
  171.                 else if (playfield[player1.Y2 + 1, player1.X1 + i] != 1 && player1.Y2 < 26)
  172.                 {
  173.                     for (int j = 0; j < player1.Skin.Length; j++)
  174.                     {
  175.                         playfield[player1.Y1, player1.X1 + j] = 0;
  176.                     }
  177.                     player1.Y1++;
  178.                     break;
  179.                 }
  180.             }
  181.             player1.Y2 = player1.Y1 + 2;
  182.  
  183.             if (playfield[player1.Y2 + 1, player1.X1] == 3 && playfield[player1.Y2 + 1, player1.X2] == 3)
  184.             {
  185.                 player1.Points = player1.Points + 20;
  186.             }
  187.  
  188.             if (Console.KeyAvailable == true)
  189.             {
  190.                 ConsoleKeyInfo pressedKey = Console.ReadKey(true);
  191.                 while (Console.KeyAvailable) Console.ReadKey(true);
  192.  
  193.                 if (pressedKey.Key == ConsoleKey.LeftArrow)
  194.                 {
  195.                     //boundaries
  196.                     if (player1.X1 - 1 >= 0 && playfield[player1.Y2 - 1, player1.X1 - 1] == 0)
  197.                     {
  198.                         player1.X1--;
  199.                         player1.X2 = player1.X1 + player1.Skin.Length - 1;
  200.                         for (int i = 0; i < player1.Skin.Length; i++)                           //extract to clear left or right side
  201.                         {                                                                       //extract to clear left or right side
  202.                             Console.SetCursorPosition(player1.X1, player1.Y1 + i);              //extract to clear left or right side
  203.                             Console.Write(player1.Skin[i]);                                     //extract to clear left or right side
  204.                             Console.SetCursorPosition(player1.X2, player1.Y1 + i);          //extract to clear left or right side
  205.                             Console.Write(" ");                                                 //extract to clear left or right side
  206.                             playfield[player1.Y1 + i, player1.X2 + 1] = 0;
  207.                         }
  208.                     }
  209.                 }
  210.                 else if (pressedKey.Key == ConsoleKey.RightArrow)
  211.                 {
  212.                     //boundaries
  213.                     if (player1.X2 + 1 != playfield.GetLength(1) && playfield[player1.Y2 - 1, player1.X2 + 1] == 0)
  214.                     {
  215.                         player1.X1++;
  216.                         player1.X2 = player1.X1 + player1.Skin.Length - 1;
  217.                         for (int i = 0; i < player1.Skin.Length; i++)
  218.                         {
  219.                             Console.SetCursorPosition(player1.X1, player1.Y1 + i);
  220.                             Console.Write(player1.Skin[i]);
  221.                             Console.SetCursorPosition(player1.X1 , player1.Y1 + i);
  222.                             Console.Write(" ");
  223.                             playfield[player1.Y1 + i, player1.X1 - 1] = 0;
  224.                         }
  225.                     }
  226.                 }
  227.             }
  228.  
  229.             for (int i = 0; i < playfield.GetLength(0); i++)
  230.             {
  231.                 Console.SetCursorPosition(playfield.GetLength(0), i);
  232.                 Console.ForegroundColor = ConsoleColor.White;
  233.                 Console.Write("|");
  234.             }
  235.             Console.SetCursorPosition(playfield.GetLength(1) + 2, 5); //row 5
  236.             Console.Write("Score: {0}", player1.Points);
  237.             Console.SetCursorPosition(playfield.GetLength(1) + 2, 15); //row 15
  238.             Console.Write("Speed: {0}", levelSpeed);
  239.             if (player1.Points < 200)
  240.             {
  241.                 Console.SetCursorPosition(playfield.GetLength(1) + 2, 10); //row 10
  242.                 Console.ForegroundColor = ConsoleColor.Green;
  243.                 Console.Write("Difficulty: {0}     ", difficulty[0]);
  244.                 Console.ResetColor();
  245.             }
  246.             else if (player1.Points <= 400)
  247.             {
  248.                 levelSpeed = 12;
  249.                 holeWidth = 5;
  250.                 Console.SetCursorPosition(playfield.GetLength(1) + 2, 10); //row 10
  251.                 Console.ForegroundColor = ConsoleColor.Yellow;
  252.                 Console.Write("Difficulty: {0}     ", difficulty[1]);
  253.                 Console.ResetColor();
  254.             }
  255.             else if (player1.Points >= 400)
  256.             {
  257.                 levelSpeed = 100;
  258.                 holeWidth = 3;
  259.                 Console.SetCursorPosition(playfield.GetLength(1) + 2, 10); //row 10
  260.                 Console.ForegroundColor = ConsoleColor.Red;
  261.                 Console.Write("Difficulty: {0}     ", difficulty[2]);
  262.                 Console.ResetColor();
  263.             }
  264.            
  265.             if (player1.Y1 == 0)
  266.             {
  267.                 isGameOver = true;
  268.             }
  269.  
  270.  
  271.             player1.Points++;
  272.         }/**/
  273.        
  274.         Console.Clear();
  275.         Console.WriteLine("Game Over!");
  276.         Console.WriteLine("Your score is: {0}", player1.Points);
  277.         Console.Write("Your name: ");
  278.         string name = Console.ReadLine();
  279.         string score = player1.Points.ToString() + " - " + name;
  280.  
  281.         using (StreamWriter w = File.AppendText(scoreFileDir))
  282.         {
  283.                 w.WriteLine(score);
  284.         }
  285.         Thread.Sleep(2000);
  286.     }
  287.  
  288.     private static void PrintCharacter(Player player1)
  289.     {
  290.         for (int j = 0; j < player1.Skin.Length; j++)
  291.         {
  292.             Console.SetCursorPosition(player1.X1, player1.Y1 + j);
  293.             Console.Write(player1.Skin[j]);
  294.         }
  295.     }
  296. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement