Advertisement
Statev

FallingRocksGame

Nov 29th, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.38 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. using System.Collections;
  7.  
  8. namespace _11.FallingRocksGame
  9. {
  10.     class Rock
  11.     {
  12.         public int X, Y;
  13.         public char symbol;
  14.         public byte colorNumber;
  15.         public void setPositions(int x,int y)
  16.         {
  17.             this.X = x;
  18.             this.Y = y;
  19.         }
  20.         public void Falling()
  21.         {
  22.             this.Y++;
  23.         }
  24.         public Rock(int x, int y,char symbol, byte colorNumber)
  25.         {
  26.             this.colorNumber = colorNumber;
  27.             this.symbol = symbol;
  28.             this.X = x;
  29.             this.Y = y;
  30.         }
  31.     }
  32.     class FallingRocksGame
  33.     {
  34.         static void Main(string[] args)
  35.         {
  36.             double timer = 200;
  37.             int gameLevel = 1;
  38.             Random randomGenerator = new Random();
  39.             char[] rockForms = {'^', '@', '*', '&', '+', '%', '$', '#', '!', '.', ';', '-'};
  40.             int points = 0;
  41.  
  42.             Queue<Rock> RocksElements = new Queue<Rock>();
  43.             ArrayList StricksRocksX = new ArrayList();
  44.  
  45.             int dwarfPositionX = Console.WindowWidth / 2;
  46.             int dwarfPositionY = Console.WindowHeight - 1;
  47.  
  48.             int fallinLines = 0;
  49.             int lastLevelLines = 0;
  50.             bool endLevel = false;
  51.             while(true)
  52.             {
  53.                 points++;
  54.                 StricksRocksX.Clear();
  55.                 Console.ForegroundColor = (ConsoleColor)15;
  56.                 Console.SetCursorPosition(dwarfPositionX, dwarfPositionY);
  57.                 Console.WriteLine("(0)");
  58.                 if (fallinLines < Console.WindowHeight - 1)
  59.                 {
  60.                     for (int i = 0; i < gameLevel; i++)
  61.                     {
  62.                         RocksElements.Enqueue(new Rock(randomGenerator.Next(0, Console.WindowWidth - 1), 1,
  63.                             rockForms[randomGenerator.Next(0, rockForms.Length - 1)],(byte)randomGenerator.Next(1,15)));
  64.                     }
  65.                     fallinLines++;
  66.                 }
  67.                 foreach (Rock rock in RocksElements)
  68.                 {
  69.                     if (rock.Y >= Console.WindowHeight - 1)
  70.                     {
  71.                         rock.setPositions(randomGenerator.Next(0, Console.WindowWidth - 1), 1);
  72.                     }
  73.                     else
  74.                     {
  75.                         rock.Falling();
  76.                     }
  77.                     if (!endLevel || rock.Y >= lastLevelLines)
  78.                     {
  79.                         Console.SetCursorPosition(rock.X, rock.Y);
  80.                         Console.ForegroundColor = (ConsoleColor)rock.colorNumber;
  81.                         Console.Write(rock.symbol);
  82.                     }
  83.                     if (rock.Y == dwarfPositionY)
  84.                     {
  85.                         StricksRocksX.Add(rock.X);
  86.                     }
  87.                 }
  88.  
  89.                 int keyClick = 0;
  90.                 for (int i = 0; i < 15; i++)
  91.                 {
  92.                     if (Console.KeyAvailable)
  93.                     {
  94.                         ConsoleKeyInfo pressedKey = Console.ReadKey();
  95.                         if (pressedKey.Key == ConsoleKey.RightArrow && keyClick < 7)
  96.                         {
  97.                             dwarfPositionX++;
  98.                             keyClick++;
  99.                         }
  100.                         if (pressedKey.Key == ConsoleKey.LeftArrow && keyClick < 7)
  101.                         {
  102.                             dwarfPositionX--;
  103.                             keyClick++;
  104.                         }
  105.                         if (pressedKey.Key == ConsoleKey.Spacebar)
  106.                         {
  107.                             Console.Clear();
  108.                             Console.ForegroundColor = ConsoleColor.White;
  109.                             Console.WriteLine("Your points is {0}", points);
  110.                             return;
  111.                         }
  112.                     }
  113.                 }
  114.  
  115.                 foreach (int strick in StricksRocksX)
  116.                 {
  117.                     if (strick >= dwarfPositionX && strick <= dwarfPositionX + 2)
  118.                     {
  119.                         Console.Clear();
  120.                         Console.ForegroundColor = ConsoleColor.White;
  121.                         Console.WriteLine("Your points is {0} in level {1}", points,gameLevel);
  122.                         return;
  123.                     }
  124.                 }
  125.                 if (dwarfPositionX >= Console.WindowWidth - 2)
  126.                 {
  127.                     dwarfPositionX = 2;
  128.                 }
  129.                 if (dwarfPositionX <= 1)
  130.                 {
  131.                     dwarfPositionX = Console.WindowWidth - 3;
  132.                 }
  133.                
  134.                 Thread.Sleep((int)timer);
  135.                 Console.Clear();
  136.                 timer -= 0.5;
  137.  
  138.                 if (timer < 100)
  139.                 {
  140.                     lastLevelLines++;
  141.                     endLevel = true;
  142.                 }
  143.                 if (lastLevelLines >= Console.WindowHeight - 1)
  144.                 {
  145.                     endLevel = false;
  146.                     lastLevelLines = 0;
  147.                     RocksElements.Clear();
  148.                     fallinLines = 0;
  149.                     gameLevel++;
  150.                     timer = 200;
  151.                 }
  152.             }
  153.         }
  154.     }
  155. }
  156.  
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement