Advertisement
SamuilPetrow

FallingRocks

Mar 17th, 2014
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.12 KB | None | 0 0
  1. namespace FallingRocks
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Threading;
  6.     public class FallingRocks
  7.     {
  8.         static ulong score = 0;
  9.         static int lives = 3;
  10.         static int[,] field = new int[40, 40];
  11.         static char[] lastLine = new char[field.GetLength(1)];
  12.         static int curPos = 20;
  13.         public static void Draw(int[,] field, char[] lastLine)
  14.         {
  15.             for (int i = 1; i < field.GetLength(0); i++)
  16.             {
  17.                 for (int j = 0; j < field.GetLength(1); j++)
  18.                 {
  19.                     switch (field[i,j])
  20.                     {
  21.                         case 0: Console.ForegroundColor = ConsoleColor.White;Console.Write(" "); break;
  22.                         case 1: Console.ForegroundColor = ConsoleColor.White; Console.Write("("); break;
  23.                         case 2: Console.ForegroundColor = ConsoleColor.White; Console.Write("0"); break;
  24.                         case 3: Console.ForegroundColor = ConsoleColor.White; Console.Write(")"); break;
  25.                         case 4: Console.ForegroundColor = ConsoleColor.Blue; Console.Write("^"); break;
  26.                         case 5: Console.ForegroundColor = ConsoleColor.DarkYellow; Console.Write("@"); break;
  27.                         case 6: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("*"); break;
  28.                         case 7: Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("&"); break;
  29.                         case 8: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("+"); break;
  30.                         case 9: Console.ForegroundColor = ConsoleColor.DarkYellow; Console.Write("%"); break;
  31.                         case 10: Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("$"); break;
  32.                         case 11: Console.ForegroundColor = ConsoleColor.DarkYellow; Console.Write("#"); break;
  33.                         case 12: Console.ForegroundColor = ConsoleColor.Red; Console.Write("!"); break;
  34.                         case 13: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("."); break;
  35.                         case 14: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write(";"); break;
  36.                         case 15: Console.ForegroundColor = ConsoleColor.Red; Console.Write("-"); break;
  37.                     }
  38.                 }
  39.                 Console.WriteLine();
  40.             }
  41.             for (int i = 0; i < lastLine.Length; i++)
  42.             {
  43.                 Console.ForegroundColor = ConsoleColor.White;
  44.                 Console.Write(lastLine[i]);
  45.             }
  46.  
  47.             Console.WriteLine();
  48.             Console.WriteLine();
  49.             Console.ForegroundColor = ConsoleColor.White;
  50.             if(score>40) Console.WriteLine("                 SCORE: {0}", score-40);
  51.             else
  52.             {
  53.                 Console.WriteLine("                 SCORE: {0}", 0);
  54.             }
  55.             Console.ForegroundColor = ConsoleColor.Red;
  56.             Console.WriteLine("                 LIVES: {0}", lives);
  57.         }
  58.         public static void Move(ConsoleKeyInfo? info, char[] lastLine, ref int curPos)
  59.         {
  60.             if (info!=null)
  61.             {
  62.              
  63.             switch (info.Value.Key)
  64.             {
  65.                 case ConsoleKey.LeftArrow:
  66.                 if (curPos-2>=0)
  67.                 {
  68.                   lastLine[curPos - 1] = ' ';
  69.                   lastLine[curPos] = ' ';
  70.                   if(curPos+1<field.GetLength(1)) lastLine[curPos + 1] = ' ';
  71.                   lastLine[curPos - 2] = '(';
  72.                   lastLine[curPos - 1] = '0';
  73.                   lastLine[curPos] = ')';
  74.                   curPos--;
  75.                 }
  76.                 break;
  77.                 case ConsoleKey.RightArrow:
  78.                 if (curPos+2<lastLine.Length)
  79.                 {
  80.                     if(curPos-1>=0) lastLine[curPos - 1] = ' ';
  81.                     if(curPos-2>=0) lastLine[curPos-2] = ' ';
  82.                     lastLine[curPos] = ' ';
  83.                     lastLine[curPos] = '(';
  84.                     lastLine[curPos + 1] = '0';
  85.                     lastLine[curPos + 2] = ')';
  86.                     curPos++;
  87.                        
  88.                 }
  89.                 break;
  90.                 default: break;
  91.             }  
  92.             }
  93.         }
  94.         public static void GenerateLine(int[,] field)
  95.         {
  96.             Random r = new Random();
  97.             int numberOfRocks = r.Next(3, 6);
  98.             for (int i = 1; i <= numberOfRocks; i++)
  99.             {
  100.                 int numberofRocks = r.Next(1, 3);
  101.                 int symbol = r.Next(4, 14);
  102.                 if(numberOfRocks==1) field[0, r.Next(0,field.GetLength(1)-1)] = symbol;
  103.                 else if (numberofRocks==2)
  104.                 {
  105.                     int pos = r.Next(0, field.GetLength(1) - 1);
  106.                     field[0, pos] = symbol;
  107.                     field[0, pos + 1] = symbol;
  108.                 }
  109.             }
  110.         }
  111.         public static void FallDown(int[,] field, ref int curPos)
  112.         {
  113.             int[,] copy = new int[field.GetLength(0), field.GetLength(1)];
  114.  
  115.             for (int i = 1; i < field.GetLength(0); i++)
  116.             {
  117.                 for (int j = 0; j < field.GetLength(1); j++)
  118.                 {
  119.                     copy[i, j] = field[i - 1, j];
  120.                 }
  121.             }
  122.             GenerateLine(copy);
  123.             for (int i = 0; i < field.GetLength(0); i++)
  124.             {
  125.                 for (int j = 0; j < field.GetLength(1); j++)
  126.                 {
  127.                    
  128.                     field[i, j] = 0;
  129.                 }
  130.             }
  131.  
  132.             for (int i = 0; i < field.GetLength(0); i++)
  133.             {
  134.                 for (int j = 0; j < field.GetLength(1); j++)
  135.                 {
  136.                     field[i, j] = copy[i, j];
  137.                 }
  138.             }
  139.         }
  140.         public void TimerProc(object state)
  141.         {
  142.             Timer t = (Timer)state;
  143.             t.Dispose();
  144.             ConsoleKeyInfo? info = Console.ReadKey();
  145.             if (info != null)
  146.             {
  147.                 Move(info, lastLine, ref curPos);
  148.             }
  149.         }
  150.         public void StartTimer(int dueTime)
  151.         {
  152.             Timer t = new Timer(new TimerCallback(TimerProc));
  153.             t.Change(dueTime, 0);
  154.         }
  155.         public static void CheckCollision(int[] lastLineOfMatrix, int curPos)
  156.         {
  157.             bool flag = false;
  158.             for (int i = 0; i < lastLineOfMatrix.Length; i++)
  159.             {
  160.                 if (lastLineOfMatrix[i]!=0&&(i==curPos-1||i==curPos||i==curPos+1))
  161.                 {
  162.                     flag = true; break;
  163.                 }
  164.             }
  165.             if (flag)
  166.             {
  167.                 if (lives>0)
  168.                 {
  169.                     lives--;                    
  170.                 }
  171.             }
  172.             else
  173.             {
  174.                 score++;
  175.             }
  176.         }
  177.         public static void Main()
  178.         {
  179.             FallingRocks inst = new FallingRocks();
  180.             Console.SetWindowSize(45, 45);
  181.             Console.SetBufferSize(45, 45);
  182.             lastLine[curPos - 2] = '(';
  183.             lastLine[curPos - 1] = '0';
  184.             lastLine[curPos] = ')';
  185.             for (int i = 0; i < field.GetLength(0); i++)
  186.             {
  187.                 for (int j = 0; j < field.GetLength(1); j++)
  188.                 {
  189.                     field[i, j] = 0;
  190.                 }
  191.             }
  192.            
  193.             while (lives>0)
  194.             {
  195.                 Console.Clear();
  196.                 int[] lastFalling = new int[field.GetLength(1)];
  197.                 for (int i = 0; i < field.GetLength(1); i++)
  198.                 {
  199.                     lastFalling[i] = field[field.GetLength(0) - 1, i];
  200.                 }
  201.                 CheckCollision(lastFalling,curPos);
  202.                 FallDown(field, ref curPos);
  203.                 Draw(field, lastLine);
  204.                 inst.StartTimer(50);
  205.                 Thread.Sleep(150);
  206.             }
  207.             Console.WriteLine("                 GAME OVER!");
  208.             Thread.Sleep(10000);
  209.         }
  210.     }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement