Advertisement
Guest User

Main File

a guest
Mar 5th, 2012
1,156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Drawing;
  7.  
  8. namespace SnakeV2
  9. {
  10.     class SnakeGame
  11.     {
  12.         public double MS, LineHeightRatio;
  13.         public int Points;
  14.         public SnakeObject Snake;
  15.         public FoodObject Food;
  16.         public RocksObject Rocks;
  17.  
  18.         public SnakeGame()
  19.         {
  20.             Snake = new SnakeObject(5);
  21.             Food = new FoodObject();
  22.             Rocks = new RocksObject(Food.Point);
  23.             Snake.Dir = Direction.Right;
  24.             MS = 6.0;
  25.             LineHeightRatio = 1.6;
  26.             Points = 0;
  27.         }
  28.  
  29.         public void DirectionInput()
  30.         {
  31.             if (Console.KeyAvailable)
  32.             {
  33.                 ConsoleKeyInfo userInput = Console.ReadKey();
  34.                 if (userInput.Key == ConsoleKey.LeftArrow)
  35.                 {
  36.                     if (Snake.Dir != Direction.Right && Snake.Dir != Direction.Left) { Snake.Dir = Direction.Left; MS = 6.0; };
  37.                 }
  38.                 else if (userInput.Key == ConsoleKey.RightArrow)
  39.                 {
  40.                     if (Snake.Dir != Direction.Left && Snake.Dir != Direction.Right) { Snake.Dir = Direction.Right; MS = 6.0; };
  41.                 }
  42.                 else if (userInput.Key == ConsoleKey.UpArrow)
  43.                 {
  44.                     if (Snake.Dir != Direction.Down && Snake.Dir != Direction.Up) { Snake.Dir = Direction.Up; MS = 6.0 * LineHeightRatio; };
  45.                 }
  46.                 else if (userInput.Key == ConsoleKey.DownArrow)
  47.                 {
  48.                     if (Snake.Dir != Direction.Up && Snake.Dir != Direction.Down) { Snake.Dir = Direction.Down; MS = 6.0 * LineHeightRatio; };
  49.                 }
  50.             }
  51.         }
  52.  
  53.         public void GameOver(string Reason)
  54.         {
  55.             Console.Clear();
  56.  
  57.             Console.WriteLine("Game over!");
  58.             Console.WriteLine(Reason);
  59.             Console.WriteLine("\nPoints: " + Points);
  60.             Console.WriteLine("\nTap 'R' to restart...");
  61.  
  62.             ConsoleKeyInfo key = Console.ReadKey(true);
  63.             while (key.KeyChar != 'R' && key.KeyChar != 'r')
  64.                 key = Console.ReadKey(true);
  65.             SnakeGame Game = new SnakeGame();
  66.             Game.Start();
  67.         }
  68.  
  69.         public void DrawPoints()
  70.         {
  71.             Console.SetCursorPosition(0, 0);
  72.             Console.WriteLine("Points: " + Points);
  73.         }
  74.  
  75.         public void ScreenDraw()
  76.         {
  77.             Console.Clear();
  78.             Console.ForegroundColor = ConsoleColor.Yellow;
  79.             Food.Draw();
  80.             Console.ForegroundColor = ConsoleColor.Green;
  81.             Snake.Draw();
  82.             /*Console.ForegroundColor = ConsoleColor.Red;
  83.             DrawPoints();*/
  84.             Console.ForegroundColor = ConsoleColor.Cyan;
  85.             Rocks.Draw();
  86.         }
  87.  
  88.         public void Start()
  89.         {
  90.             while (true)
  91.             {
  92.                 DirectionInput();
  93.  
  94.                 if (Snake.Dir == Direction.Left)
  95.                     Snake.Head.Y--;
  96.                 else if (Snake.Dir == Direction.Right)
  97.                     Snake.Head.Y++;
  98.                 else if (Snake.Dir == Direction.Up)
  99.                     Snake.Head.X--;
  100.                 else if (Snake.Dir == Direction.Down)
  101.                     Snake.Head.X++;
  102.  
  103.                 Snake.Update();
  104.  
  105.                 foreach (Point p in Snake.Body)
  106.                     if (p.X == Food.Point.X && p.Y == Food.Point.Y)
  107.                     {
  108.                         Points++;
  109.                         Food = new FoodObject();
  110.                         Rocks = new RocksObject(Food.Point);
  111.                         //Snake.Grow(); Still building this one, so please ignore, having a problem this causes the program to crash
  112.                     }
  113.  
  114.                 if (Snake.Head.X < 0 || Snake.Head.Y < 0 || Snake.Head.X == Console.WindowHeight || Snake.Head.Y == Console.WindowWidth)
  115.                     GameOver("Crashed a wall.");
  116.                 foreach (Point Rock in Rocks.List)
  117.                     if (Rock.X == Snake.Head.X && Rock.Y == Snake.Head.Y)
  118.                         GameOver("Crashed a rock.");
  119.  
  120.                 ScreenDraw();
  121.  
  122.                 System.Threading.Thread.Sleep((int)MS * 10);
  123.             }
  124.         }
  125.     }
  126.     class Program
  127.     {
  128.         static void Main(string[] args)
  129.         {
  130.             SnakeGame Game = new SnakeGame();
  131.  
  132.             Game.Start();
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement