Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.Threading.Tasks;
  7.  
  8. namespace SnakeGame
  9. {
  10.     class Program
  11.     {
  12.          static void colorBlock (int c) {
  13.             if(c == 1){
  14.                 Console.BackgroundColor = ConsoleColor.Green;
  15.             } else if (c == 2)
  16.             {
  17.                 Console.BackgroundColor = ConsoleColor.DarkGreen;
  18.             }
  19.             else
  20.             {
  21.                 Console.BackgroundColor = ConsoleColor.Red;
  22.             }
  23.             Console.Write("  ");
  24.             Console.BackgroundColor = ConsoleColor.Black;
  25.          }
  26.  
  27.         static void Main(string[] args)
  28.         {
  29.  
  30.             string[,] map =
  31.         {
  32.             { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
  33.             { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
  34.             { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
  35.             { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
  36.             { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
  37.             { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
  38.             { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
  39.             { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
  40.             { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
  41.             { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "}
  42.         };
  43.            
  44.             //Misc
  45.             Random Rand = new Random();
  46.             bool alive = true;
  47.             int points = 0;
  48.             int timer = 500;
  49.  
  50.            
  51.             //Mask
  52.             int maskX = 5;
  53.             int maskY = 5;
  54.             int maskSize = 1;
  55.             int maskDirX = 1;
  56.             int maskDirY = 0;
  57.  
  58.             //Apple
  59.             int appleX = Rand.Next(0,10);
  60.             int appleY = Rand.Next(0, 10);
  61.  
  62.             //Snake form
  63.             List<int> prevMaskX = new List<int>();
  64.             List<int> prevMaskY = new List<int>();
  65.  
  66.             prevMaskX.Add(maskY);
  67.             prevMaskY.Add(maskX);
  68.             prevMaskX.Add(maskY);
  69.             prevMaskY.Add(maskX);
  70.             prevMaskX.Add(maskY);
  71.             prevMaskY.Add(maskX);
  72.  
  73.  
  74.  
  75.             //Code starts
  76.             while (alive == true)
  77.             {
  78.             Console.Clear();
  79.  
  80.  
  81.                 for (int x = 0; x < map.GetLength(0); x++)
  82.                 {
  83.                     for (int y = 0; y < map.GetLength(1); y++)
  84.                     {
  85.                         if (maskX == y && maskY == x) //Huvudet mask
  86.                         {
  87.                             colorBlock(2);
  88.                             if (appleX == maskX && appleY == maskY) //Kolla om huvudet är på äpple
  89.                             {
  90.                                 appleX = Rand.Next(0, 10);
  91.                                 appleY = Rand.Next(0, 10);
  92.                                 points = points + 1;
  93.                                 maskSize++;
  94.                             }
  95.                             /* if () { //Kolla om huvudet är på orm
  96.  
  97.                              }*/
  98.                         }
  99.                         else if (appleX == y && appleY == x) //Äpplet plats
  100.                         {
  101.                             colorBlock(0);
  102.                         }
  103.                         else if (prevMaskX[prevMaskX.Count - maskSize] == x && prevMaskY[prevMaskX.Count - maskSize] == y)
  104.                         {
  105.                             for(int i = 1; i > maskSize; i++)
  106.                             {
  107.                                 if (prevMaskX[prevMaskX.Count - i] == x && prevMaskY[prevMaskX.Count - i] == y)
  108.                                 {
  109.                                     colorBlock(1);
  110.                                 }
  111.                             }
  112.                         }
  113.                         else
  114.                         {
  115.                             Console.Write(map[x, y]);//Om inget stämmer skriv ut resterande ruto
  116.                         }
  117.                    
  118.                            
  119.                     }
  120.                     Console.WriteLine();
  121.                    
  122.                 }
  123.  
  124.  
  125.                 Console.WriteLine("You currently have : " + points + " points");
  126.                 Console.WriteLine("Masksize :" + maskSize); //debug
  127.                 Console.WriteLine("MaskX = " + maskX + " " + "MaskY = " + maskY); //debug
  128.  
  129.                 Stopwatch stopWatch = new Stopwatch();
  130.                 stopWatch.Start();
  131.                
  132.                 while (stopWatch.ElapsedMilliseconds < timer)
  133.                 {
  134.                     if (Console.KeyAvailable)
  135.                     {
  136.                         var key = Console.ReadKey(true);
  137.                         if (key.Key == ConsoleKey.LeftArrow && maskDirX != 1)
  138.                         {
  139.                             maskDirX = -1;
  140.                             maskDirY = 0;
  141.                         }
  142.                         else if (key.Key == ConsoleKey.RightArrow && maskDirX != -1)
  143.                         {
  144.                             maskDirX = 1;
  145.                             maskDirY = 0;
  146.                         }
  147.                         else if (key.Key == ConsoleKey.UpArrow && maskDirY != -1)
  148.                         {
  149.                             maskDirY = 1;
  150.                             maskDirX = 0;
  151.                         }
  152.                         else if (key.Key == ConsoleKey.DownArrow && maskDirY != 1)
  153.                         {
  154.                             maskDirY = -1;
  155.                             maskDirX = 0;
  156.                         }
  157.                     }
  158.                 }
  159.  
  160.                     prevMaskX.Add(maskY);
  161.                     prevMaskY.Add(maskX);
  162.  
  163.                 if (maskDirX == 1)
  164.                 {
  165.                     maskX++;
  166.                 }
  167.                 else if(maskDirX == -1)
  168.                 {
  169.                     maskX--;
  170.                 }
  171.                 else if(maskDirY == 1)
  172.                 {
  173.                     maskY--;
  174.                 }
  175.                 else if(maskDirY == -1)
  176.                 {
  177.                     maskY++;
  178.                 }
  179.                 if (maskX <= -1 || maskY <= -1 || maskX >= 10 || maskY >= 10)
  180.                 {
  181.                     alive = false;
  182.                 }
  183.             }
  184.             Console.WriteLine("You have lost with " + points + " points");
  185.         }
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement