Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- using System.Threading.Tasks;
- namespace SnakeGame
- {
- class Program
- {
- static void colorBlock (int c) {
- if(c == 1){
- Console.BackgroundColor = ConsoleColor.Green;
- } else if (c == 2)
- {
- Console.BackgroundColor = ConsoleColor.DarkGreen;
- }
- else
- {
- Console.BackgroundColor = ConsoleColor.Red;
- }
- Console.Write(" ");
- Console.BackgroundColor = ConsoleColor.Black;
- }
- static void Main(string[] args)
- {
- string[,] map =
- {
- { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
- { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
- { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
- { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
- { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
- { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
- { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
- { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
- { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
- { ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "}
- };
- //Misc
- Random Rand = new Random();
- bool alive = true;
- int points = 0;
- int timer = 500;
- //Mask
- int maskX = 5;
- int maskY = 5;
- int maskSize = 1;
- int maskDirX = 1;
- int maskDirY = 0;
- //Apple
- int appleX = Rand.Next(0,10);
- int appleY = Rand.Next(0, 10);
- //Snake form
- List<int> prevMaskX = new List<int>();
- List<int> prevMaskY = new List<int>();
- prevMaskX.Add(maskY);
- prevMaskY.Add(maskX);
- prevMaskX.Add(maskY);
- prevMaskY.Add(maskX);
- prevMaskX.Add(maskY);
- prevMaskY.Add(maskX);
- //Code starts
- while (alive == true)
- {
- Console.Clear();
- for (int x = 0; x < map.GetLength(0); x++)
- {
- for (int y = 0; y < map.GetLength(1); y++)
- {
- if (maskX == y && maskY == x) //Huvudet mask
- {
- colorBlock(2);
- if (appleX == maskX && appleY == maskY) //Kolla om huvudet är på äpple
- {
- appleX = Rand.Next(0, 10);
- appleY = Rand.Next(0, 10);
- points = points + 1;
- maskSize++;
- }
- /* if () { //Kolla om huvudet är på orm
- }*/
- }
- else if (appleX == y && appleY == x) //Äpplet plats
- {
- colorBlock(0);
- }
- else if (prevMaskX[prevMaskX.Count - maskSize] == x && prevMaskY[prevMaskX.Count - maskSize] == y)
- {
- for(int i = 1; i > maskSize; i++)
- {
- if (prevMaskX[prevMaskX.Count - i] == x && prevMaskY[prevMaskX.Count - i] == y)
- {
- colorBlock(1);
- }
- }
- }
- else
- {
- Console.Write(map[x, y]);//Om inget stämmer skriv ut resterande ruto
- }
- }
- Console.WriteLine();
- }
- Console.WriteLine("You currently have : " + points + " points");
- Console.WriteLine("Masksize :" + maskSize); //debug
- Console.WriteLine("MaskX = " + maskX + " " + "MaskY = " + maskY); //debug
- Stopwatch stopWatch = new Stopwatch();
- stopWatch.Start();
- while (stopWatch.ElapsedMilliseconds < timer)
- {
- if (Console.KeyAvailable)
- {
- var key = Console.ReadKey(true);
- if (key.Key == ConsoleKey.LeftArrow && maskDirX != 1)
- {
- maskDirX = -1;
- maskDirY = 0;
- }
- else if (key.Key == ConsoleKey.RightArrow && maskDirX != -1)
- {
- maskDirX = 1;
- maskDirY = 0;
- }
- else if (key.Key == ConsoleKey.UpArrow && maskDirY != -1)
- {
- maskDirY = 1;
- maskDirX = 0;
- }
- else if (key.Key == ConsoleKey.DownArrow && maskDirY != 1)
- {
- maskDirY = -1;
- maskDirX = 0;
- }
- }
- }
- prevMaskX.Add(maskY);
- prevMaskY.Add(maskX);
- if (maskDirX == 1)
- {
- maskX++;
- }
- else if(maskDirX == -1)
- {
- maskX--;
- }
- else if(maskDirY == 1)
- {
- maskY--;
- }
- else if(maskDirY == -1)
- {
- maskY++;
- }
- if (maskX <= -1 || maskY <= -1 || maskX >= 10 || maskY >= 10)
- {
- alive = false;
- }
- }
- Console.WriteLine("You have lost with " + points + " points");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement