Advertisement
Spaskich

Untitled

Oct 2nd, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 21.50 KB | None | 0 0
  1. namespace JustSnake
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.IO;
  6.     using System.Linq;
  7.     using System.Threading;
  8.  
  9.     internal struct Position
  10.     {
  11.         public int X;
  12.  
  13.         public int Y;
  14.  
  15.         public Position(int x, int y)
  16.         {
  17.             this.X = x;
  18.             this.Y = y;
  19.         }
  20.     }
  21.  
  22.     public class MainGameCode
  23.     {
  24.         private static int windowHeight = 30;
  25.  
  26.         private static int windowWidth = 70;
  27.  
  28.         private static int level = 1;
  29.  
  30.         private static int direction;   // 0 right 1 left 2 down 3 up
  31.  
  32.         private static Random randomGenerator = new Random();
  33.  
  34.         private static Queue<Position> snakeElements = new Queue<Position>();
  35.  
  36.         private static List<string> leaderboard = new List<string>();   // Leaderboard List
  37.        
  38.         private static string filePath = "../../file.md";
  39.  
  40.         private static string[] options = { "New Game", "HighScores", "Choose Difficulty", "Quit" };
  41.  
  42.         private static string[] difficultyOptions = { "Level 1", "Level 2", "Level 3", "Level 4", "Return" };
  43.  
  44.         private static int upperMenuBorder = 1;
  45.  
  46.         private static int lowerMenuBorder = 28;
  47.  
  48.         private static Position[] directions = new Position[]
  49.         {
  50.             new Position(1, 0),     //right
  51.             new Position(-1, 0),    // left
  52.             new Position(0, 1),     //down
  53.             new Position(0, -1)     //up
  54.         };
  55.  
  56.         public static void Main()
  57.         {
  58.             Console.OutputEncoding = System.Text.Encoding.Unicode;
  59.             Console.BufferHeight = Console.WindowHeight = windowHeight;
  60.             Console.BufferWidth = Console.WindowWidth = windowWidth;
  61.  
  62.             LoadFile();
  63.             Menu();
  64.         }
  65.  
  66.         private static void Menu()
  67.         {
  68.             Console.CursorVisible = false;
  69.             Console.Clear();
  70.             int currentSelection = 0;
  71.  
  72.             while (true)
  73.             {
  74.                 PrintData(0, lowerMenuBorder, new string('-', windowWidth), ConsoleColor.Magenta);
  75.                 PrintData(0, upperMenuBorder, new string('-', windowWidth), ConsoleColor.DarkMagenta);
  76.  
  77.                 PrintOptionsMenu(currentSelection);
  78.  
  79.                 currentSelection = GetCurrentMenuSelection(currentSelection);
  80.             }
  81.         }
  82.  
  83.         private static int GetCurrentMenuSelection(int currentSelection)
  84.         {
  85.             ConsoleKeyInfo keyPressed = Console.ReadKey();
  86.  
  87.             if (keyPressed.Key == ConsoleKey.DownArrow)
  88.             {
  89.                 PrintData(15, 10 + currentSelection, " ", ConsoleColor.White);
  90.                 currentSelection++;
  91.  
  92.                 if (currentSelection > options.Length - 1)
  93.                 {
  94.                     currentSelection = 0;
  95.                 }
  96.             }
  97.             else if (keyPressed.Key == ConsoleKey.UpArrow)
  98.             {
  99.                 PrintData(15, 10 + currentSelection, " ", ConsoleColor.White);
  100.                 currentSelection--;
  101.  
  102.                 if (currentSelection < 0)
  103.                 {
  104.                     currentSelection = 3;
  105.                 }
  106.             }
  107.             else if (keyPressed.Key == ConsoleKey.Enter)
  108.             {
  109.                 StartFromChosenMenuOption(currentSelection);
  110.             }
  111.  
  112.             return currentSelection;
  113.         }
  114.  
  115.         private static void StartFromChosenMenuOption(int currentSelection)
  116.         {
  117.             if (currentSelection == 0)
  118.             {
  119.                 StartGame();
  120.             }
  121.             else if (currentSelection == 1)
  122.             {
  123.                 Leaderboard();
  124.             }
  125.             else if (currentSelection == 2)
  126.             {
  127.                 Console.Clear();
  128.                 LevelsMenu();
  129.             }
  130.             else if (currentSelection == 3)
  131.             {
  132.                 Console.Clear();
  133.                 SaveFile();
  134.                 Environment.Exit(0);
  135.             }
  136.         }
  137.  
  138.         private static void PrintOptionsMenu(int currentSelection)
  139.         {
  140.             int printPosition = 10;
  141.  
  142.             PrintData(15, printPosition + currentSelection, "> ", ConsoleColor.White);
  143.  
  144.             foreach (var i in options)
  145.             {
  146.                 PrintData(17, printPosition, i, ConsoleColor.White);
  147.                 printPosition++;
  148.             }
  149.         }
  150.  
  151.         private static void LevelsMenu()
  152.         {
  153.             Console.CursorVisible = false;
  154.             int currentSelection = 0;
  155.  
  156.             while (true)
  157.             {
  158.                 PrintData(0, lowerMenuBorder, new string('-', windowWidth), ConsoleColor.Magenta);
  159.                 PrintData(16, 8, string.Format("{0}: {1}", "Level", level));
  160.                 PrintData(0, upperMenuBorder, new string('-', windowWidth), ConsoleColor.DarkMagenta);
  161.  
  162.                 int printPosition = 10;
  163.  
  164.                 PrintDifficultyMenu(printPosition, currentSelection);
  165.                 currentSelection = MenuKeyPressing(currentSelection);
  166.             }
  167.         }
  168.  
  169.         private static void PrintDifficultyMenu(int printPosition, int currentSelection)
  170.         {
  171.             PrintData(15, printPosition + currentSelection, "> ", ConsoleColor.White);
  172.  
  173.             foreach (var i in difficultyOptions)
  174.             {
  175.                 PrintData(17, printPosition, i, ConsoleColor.White);
  176.                 printPosition++;
  177.             }
  178.         }
  179.  
  180.         private static int MenuKeyPressing(int currentSelection)
  181.         {
  182.             ConsoleKeyInfo keyPressed = Console.ReadKey();
  183.  
  184.             if (keyPressed.Key == ConsoleKey.DownArrow)
  185.             {
  186.                 PrintData(15, 10 + currentSelection, " ", ConsoleColor.White);
  187.                 currentSelection++;
  188.  
  189.                 if (currentSelection > difficultyOptions.Length - 1)
  190.                 {
  191.                     currentSelection = 0;
  192.                 }
  193.             }
  194.             else if (keyPressed.Key == ConsoleKey.UpArrow)
  195.             {
  196.                 PrintData(15, 10 + currentSelection, " ", ConsoleColor.White);
  197.                 currentSelection--;
  198.  
  199.                 if (currentSelection < 0)
  200.                 {
  201.                     currentSelection = 4;
  202.                 }
  203.             }
  204.             else if (keyPressed.Key == ConsoleKey.Enter)
  205.             {
  206.                 RunMenuOption(currentSelection);
  207.             }
  208.  
  209.             return currentSelection;
  210.         }
  211.  
  212.         private static void RunMenuOption(int currentSelection)
  213.         {
  214.             if (currentSelection == 0)
  215.             {
  216.                 level = 1;
  217.             }
  218.             else if (currentSelection == 1)
  219.             {
  220.                 level = 2;
  221.             }
  222.             else if (currentSelection == 2)
  223.             {
  224.                 level = 3;
  225.             }
  226.             else if (currentSelection == 3)
  227.             {
  228.                 level = 4;
  229.             }
  230.             else if (currentSelection == 4)
  231.             {
  232.                 Console.Clear();
  233.                 Menu();
  234.             }
  235.         }
  236.  
  237.         private static void Leaderboard()
  238.         {
  239.             Console.Clear();
  240.  
  241.             PrintData(0, lowerMenuBorder, new string('-', windowWidth), ConsoleColor.Magenta);
  242.             PrintData(0, 3, string.Format("{0}{1}", new string(' ', windowWidth / 2 - 6), "Leaderboard"), ConsoleColor.White);
  243.             PrintData(0, upperMenuBorder, new string('-', windowWidth), ConsoleColor.DarkMagenta);
  244.  
  245.             int i = 0;
  246.  
  247.             for (; i < leaderboard.Count; i++)
  248.             {
  249.                 PrintData(0, i + 6, string.Format("[{0}] {1}", i + 1, leaderboard[i]), ConsoleColor.Yellow);
  250.             }
  251.  
  252.             PrintData(0, i + 8, "Press any key to continue", ConsoleColor.White);
  253.             Console.ReadKey();
  254.  
  255.             Menu();
  256.         }
  257.  
  258.         private static void StartGame()
  259.         {
  260.             direction = 0; // 0 right 1 left 2 down 3 up
  261.             StartSnakeElements();
  262.  
  263.             while (true)
  264.             {
  265.                 BackgroundBeep.Play();
  266.                 if (Console.KeyAvailable)
  267.                 {
  268.                     ConsoleKeyInfo command = Console.ReadKey();
  269.  
  270.                     direction = ChangeDirection(command, direction);
  271.                 }
  272.  
  273.                 Position snakeHead = snakeElements.Last();
  274.                 Position snakeNewHead = new Position(snakeHead.X + directions[direction].X, snakeHead.Y + directions[direction].Y);
  275.  
  276.                 if (snakeNewHead.X > Console.WindowWidth - 1 || snakeNewHead.X < 0 ||
  277.                     snakeNewHead.Y < 5 || snakeNewHead.Y > Console.WindowHeight - 1 || snakeElements.Contains(snakeNewHead))
  278.                 {
  279.                     PrintData(29, 15, "Game Over!", ConsoleColor.Yellow);
  280.                     Console.WriteLine();
  281.                    
  282.                     WriteName();
  283.                     BackgroundBeep.Stop();
  284.                     Menu();
  285.                 }
  286.  
  287.                 snakeElements.Enqueue(snakeNewHead);
  288.                 snakeElements.Dequeue();
  289.  
  290.                 MoveSnake();
  291.  
  292.                 Thread.Sleep(150);
  293.             }
  294.         }
  295.  
  296.         private static void WriteName()
  297.         {
  298.             //Console.Clear();
  299.  
  300.             PrintData(0, 6, "Write your name: ");
  301.             string name = Console.ReadLine();
  302.  
  303.             leaderboard.Add(name);
  304.         }
  305.  
  306.         private static void MoveSnake()
  307.         {
  308.             SnakePlayingMenu();
  309.  
  310.             foreach (Position position in snakeElements)
  311.             {
  312.                 PrintSnake(position.X, position.Y, 'o');
  313.             }
  314.         }
  315.  
  316.         private static void SnakePlayingMenu()
  317.         {
  318.             Console.Clear();
  319.  
  320.             PrintData(0, 0, new string('-', windowWidth));
  321.             PrintData(0, 2, string.Format("{0}{1}", new string(' ', windowWidth / 2 - 5),
  322.                 string.Format("{0}", "JUST SNAKE")), ConsoleColor.Red);
  323.             PrintData(4, 2, string.Format("{0}", level.ToString()), ConsoleColor.Yellow);
  324.             PrintData(0, 4, new string('-', windowWidth));
  325.         }
  326.  
  327.         private static int ChangeDirection(ConsoleKeyInfo command, int direction)
  328.         {
  329.             if (command.Key == ConsoleKey.RightArrow)
  330.             {
  331.                 if (direction != 1)
  332.                 {
  333.                     direction = 0;
  334.                 }
  335.             }
  336.             if (command.Key == ConsoleKey.LeftArrow)
  337.             {
  338.                 if (direction != 0)
  339.                 {
  340.                     direction = 1;
  341.                 }
  342.             }
  343.             if (command.Key == ConsoleKey.DownArrow)
  344.             {
  345.                 if (direction != 3)
  346.                 {
  347.                     direction = 2;
  348.                 }
  349.             }
  350.             if (command.Key == ConsoleKey.UpArrow)
  351.             {
  352.                 if (direction != 2)
  353.                 {
  354.                     direction = 3;
  355.                 }
  356.             }
  357.             if (command.Key == ConsoleKey.P)
  358.             {
  359.                 PauseGame();
  360.             }
  361.  
  362.             return direction;
  363.         }
  364.  
  365.         private static void PauseGame()
  366.         {
  367.             while (true)
  368.             {
  369.                 ConsoleKeyInfo unpause = Console.ReadKey();
  370.                 if (unpause.Key == ConsoleKey.P)
  371.                 {
  372.                     return;
  373.                 }
  374.             }          
  375.         }
  376.  
  377.         private static void StartSnakeElements()
  378.         {
  379.             snakeElements.Clear();
  380.  
  381.             for (int i = 0; i <= 5; i++)
  382.             {
  383.                 snakeElements.Enqueue(new Position(i, 5));
  384.             }
  385.  
  386.             foreach (Position position in snakeElements)
  387.             {
  388.                 PrintSnake(position.X, position.Y, 'o');
  389.             }
  390.         }
  391.  
  392.         private static void PrintData(int x, int y, string str, ConsoleColor color = ConsoleColor.Green)
  393.         {
  394.             Console.SetCursorPosition(x, y);
  395.             Console.ForegroundColor = color;
  396.             Console.Write(str);
  397.         }
  398.  
  399.         private static void PrintSnake(int x, int y, char snakeBody)
  400.         {
  401.             Console.SetCursorPosition(x, y);
  402.             Console.ForegroundColor = ConsoleColor.White;
  403.             Console.Write(snakeBody);
  404.         }
  405.  
  406.         /// <summary>
  407.         /// Load file leaderboard
  408.         /// </summary>
  409.         private static void LoadFile()
  410.         {
  411.             if (!File.Exists(filePath))
  412.             {
  413.                 FileStream fileStream = File.Create(filePath);
  414.                 fileStream.Close();
  415.             }
  416.  
  417.             using (StreamReader reader = new StreamReader(filePath))
  418.             {
  419.                 string line = reader.ReadLine();
  420.  
  421.                 while (line != null)
  422.                 {
  423.                     leaderboard.Add(line);
  424.  
  425.                     line = reader.ReadLine();
  426.                 }
  427.             }
  428.         }
  429.  
  430.         /// <summary>
  431.         /// Saving file leaderboard
  432.         /// </summary>
  433.         private static void SaveFile()
  434.         {
  435.             using (StreamWriter writer = new StreamWriter(filePath))
  436.             {
  437.                 for (int i = 0; i < leaderboard.Count; i++)
  438.                 {
  439.                     writer.Write(leaderboard[i]);
  440.                     writer.WriteLine();
  441.                 }
  442.             }
  443.         }
  444.     }
  445.  
  446.     class BackgroundBeep
  447.     {
  448.         static Thread beepThread;
  449.         static AutoResetEvent signalBeep;
  450.        
  451.  
  452.         public static void Play()
  453.         {
  454.             if(beepThread.IsAlive == false)
  455.             {
  456.  
  457.                 beepThread.Interrupt();
  458.             }
  459.         }
  460.  
  461.         public static void Stop()
  462.         {
  463.             while (!beepThread.IsAlive);
  464.             Thread.Sleep(Timeout.Infinite);
  465.             beepThread.Join();
  466.  
  467.         }
  468.  
  469.         static BackgroundBeep()
  470.         {
  471.  
  472.             signalBeep = new AutoResetEvent(false);
  473.             beepThread = new Thread(() =>
  474.             {
  475.                 for (;;)
  476.                 {
  477.                     Console.Beep(659, 125);
  478.                     Console.Beep(659, 125);
  479.                     Thread.Sleep(125);
  480.                     Console.Beep(659, 125);
  481.                     Thread.Sleep(167);
  482.                     Console.Beep(523, 125);
  483.                     Console.Beep(659, 125);
  484.                     Thread.Sleep(125);
  485.                     Console.Beep(784, 125);
  486.                     Thread.Sleep(375);
  487.                     Console.Beep(392, 125);
  488.                     Thread.Sleep(375);
  489.                     Console.Beep(523, 125);
  490.                     Thread.Sleep(250);
  491.                     Console.Beep(392, 125);
  492.                     Thread.Sleep(250);
  493.                     Console.Beep(330, 125);
  494.                     Thread.Sleep(250);
  495.                     Console.Beep(440, 125);
  496.                     Thread.Sleep(125);
  497.                     Console.Beep(494, 125);
  498.                     Thread.Sleep(125);
  499.                     Console.Beep(466, 125);
  500.                     Thread.Sleep(42);
  501.                     Console.Beep(440, 125);
  502.                     Thread.Sleep(125);
  503.                     Console.Beep(392, 125);
  504.                     Thread.Sleep(125);
  505.                     Console.Beep(659, 125);
  506.                     Thread.Sleep(125);
  507.                     Console.Beep(784, 125);
  508.                     Thread.Sleep(125);
  509.                     Console.Beep(880, 125);
  510.                     Thread.Sleep(125);
  511.                     Console.Beep(698, 125);
  512.                     Console.Beep(784, 125);
  513.                     Thread.Sleep(125);
  514.                     Console.Beep(659, 125);
  515.                     Thread.Sleep(125);
  516.                     Console.Beep(523, 125);
  517.                     Thread.Sleep(125);
  518.                     Console.Beep(587, 125);
  519.                     Console.Beep(494, 125);
  520.                     Thread.Sleep(125);
  521.                     Console.Beep(523, 125);
  522.                     Thread.Sleep(250);
  523.                     Console.Beep(392, 125);
  524.                     Thread.Sleep(250);
  525.                     Console.Beep(330, 125);
  526.                     Thread.Sleep(250);
  527.                     Console.Beep(440, 125);
  528.                     Thread.Sleep(125);
  529.                     Console.Beep(494, 125);
  530.                     Thread.Sleep(125);
  531.                     Console.Beep(466, 125);
  532.                     Thread.Sleep(42);
  533.                     Console.Beep(440, 125);
  534.                     Thread.Sleep(125);
  535.                     Console.Beep(392, 125);
  536.                     Thread.Sleep(125);
  537.                     Console.Beep(659, 125);
  538.                     Thread.Sleep(125);
  539.                     Console.Beep(784, 125);
  540.                     Thread.Sleep(125);
  541.                     Console.Beep(880, 125);
  542.                     Thread.Sleep(125);
  543.                     Console.Beep(698, 125);
  544.                     Console.Beep(784, 125);
  545.                     Thread.Sleep(125);
  546.                     Console.Beep(659, 125);
  547.                     Thread.Sleep(125);
  548.                     Console.Beep(523, 125);
  549.                     Thread.Sleep(125);
  550.                     Console.Beep(587, 125);
  551.                     Console.Beep(494, 125);
  552.                     Thread.Sleep(375);
  553.                     Console.Beep(784, 125);
  554.                     Console.Beep(740, 125);
  555.                     Console.Beep(698, 125);
  556.                     Thread.Sleep(42);
  557.                     Console.Beep(622, 125);
  558.                     Thread.Sleep(125);
  559.                     Console.Beep(659, 125);
  560.                     Thread.Sleep(167);
  561.                     Console.Beep(415, 125);
  562.                     Console.Beep(440, 125);
  563.                     Console.Beep(523, 125);
  564.                     Thread.Sleep(125);
  565.                     Console.Beep(440, 125);
  566.                     Console.Beep(523, 125);
  567.                     Console.Beep(587, 125);
  568.                     Thread.Sleep(250);
  569.                     Console.Beep(784, 125);
  570.                     Console.Beep(740, 125);
  571.                     Console.Beep(698, 125);
  572.                     Thread.Sleep(42);
  573.                     Console.Beep(622, 125);
  574.                     Thread.Sleep(125);
  575.                     Console.Beep(659, 125);
  576.                     Thread.Sleep(167);
  577.                     Console.Beep(698, 125);
  578.                     Thread.Sleep(125);
  579.                     Console.Beep(698, 125);
  580.                     Console.Beep(698, 125);
  581.                     Thread.Sleep(625);
  582.                     Console.Beep(784, 125);
  583.                     Console.Beep(740, 125);
  584.                     Console.Beep(698, 125);
  585.                     Thread.Sleep(42);
  586.                     Console.Beep(622, 125);
  587.                     Thread.Sleep(125);
  588.                     Console.Beep(659, 125);
  589.                     Thread.Sleep(167);
  590.                     Console.Beep(415, 125);
  591.                     Console.Beep(440, 125);
  592.                     Console.Beep(523, 125);
  593.                     Thread.Sleep(125);
  594.                     Console.Beep(440, 125);
  595.                     Console.Beep(523, 125);
  596.                     Console.Beep(587, 125);
  597.                     Thread.Sleep(250);
  598.                     Console.Beep(622, 125);
  599.                     Thread.Sleep(250);
  600.                     Console.Beep(587, 125);
  601.                     Thread.Sleep(250);
  602.                     Console.Beep(523, 125);
  603.                     Thread.Sleep(1125);
  604.                     Console.Beep(784, 125);
  605.                     Console.Beep(740, 125);
  606.                     Console.Beep(698, 125);
  607.                     Thread.Sleep(42);
  608.                     Console.Beep(622, 125);
  609.                     Thread.Sleep(125);
  610.                     Console.Beep(659, 125);
  611.                     Thread.Sleep(167);
  612.                     Console.Beep(415, 125);
  613.                     Console.Beep(440, 125);
  614.                     Console.Beep(523, 125);
  615.                     Thread.Sleep(125);
  616.                     Console.Beep(440, 125);
  617.                     Console.Beep(523, 125);
  618.                     Console.Beep(587, 125);
  619.                     Thread.Sleep(250);
  620.                     Console.Beep(784, 125);
  621.                     Console.Beep(740, 125);
  622.                     Console.Beep(698, 125);
  623.                     Thread.Sleep(42);
  624.                     Console.Beep(622, 125);
  625.                     Thread.Sleep(125);
  626.                     Console.Beep(659, 125);
  627.                     Thread.Sleep(167);
  628.                     Console.Beep(698, 125);
  629.                     Thread.Sleep(125);
  630.                     Console.Beep(698, 125);
  631.                     Console.Beep(698, 125);
  632.                     Thread.Sleep(625);
  633.                     Console.Beep(784, 125);
  634.                     Console.Beep(740, 125);
  635.                     Console.Beep(698, 125);
  636.                     Thread.Sleep(42);
  637.                     Console.Beep(622, 125);
  638.                     Thread.Sleep(125);
  639.                     Console.Beep(659, 125);
  640.                     Thread.Sleep(167);
  641.                     Console.Beep(415, 125);
  642.                     Console.Beep(440, 125);
  643.                     Console.Beep(523, 125);
  644.                     Thread.Sleep(125);
  645.                     Console.Beep(440, 125);
  646.                     Console.Beep(523, 125);
  647.                     Console.Beep(587, 125);
  648.                     Thread.Sleep(250);
  649.                     Console.Beep(622, 125);
  650.                     Thread.Sleep(250);
  651.                     Console.Beep(587, 125);
  652.                     Thread.Sleep(250);
  653.                     Console.Beep(523, 125);
  654.                     Thread.Sleep(625);
  655.                 }
  656.             }, 1);
  657.             beepThread.IsBackground = true;
  658.             beepThread.Start();
  659.         }
  660.     }
  661.  
  662. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement