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.Drawing;
- namespace SnakeV2
- {
- class SnakeGame
- {
- public double MS, LineHeightRatio;
- public int Points;
- public SnakeObject Snake;
- public FoodObject Food;
- public RocksObject Rocks;
- public SnakeGame()
- {
- Snake = new SnakeObject(5);
- Food = new FoodObject();
- Rocks = new RocksObject(Food.Point);
- Snake.Dir = Direction.Right;
- MS = 6.0;
- LineHeightRatio = 1.6;
- Points = 0;
- }
- public void DirectionInput()
- {
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo userInput = Console.ReadKey();
- if (userInput.Key == ConsoleKey.LeftArrow)
- {
- if (Snake.Dir != Direction.Right && Snake.Dir != Direction.Left) { Snake.Dir = Direction.Left; MS = 6.0; };
- }
- else if (userInput.Key == ConsoleKey.RightArrow)
- {
- if (Snake.Dir != Direction.Left && Snake.Dir != Direction.Right) { Snake.Dir = Direction.Right; MS = 6.0; };
- }
- else if (userInput.Key == ConsoleKey.UpArrow)
- {
- if (Snake.Dir != Direction.Down && Snake.Dir != Direction.Up) { Snake.Dir = Direction.Up; MS = 6.0 * LineHeightRatio; };
- }
- else if (userInput.Key == ConsoleKey.DownArrow)
- {
- if (Snake.Dir != Direction.Up && Snake.Dir != Direction.Down) { Snake.Dir = Direction.Down; MS = 6.0 * LineHeightRatio; };
- }
- }
- }
- public void GameOver(string Reason)
- {
- Console.Clear();
- Console.WriteLine("Game over!");
- Console.WriteLine(Reason);
- Console.WriteLine("\nPoints: " + Points);
- Console.WriteLine("\nTap 'R' to restart...");
- ConsoleKeyInfo key = Console.ReadKey(true);
- while (key.KeyChar != 'R' && key.KeyChar != 'r')
- key = Console.ReadKey(true);
- SnakeGame Game = new SnakeGame();
- Game.Start();
- }
- public void DrawPoints()
- {
- Console.SetCursorPosition(0, 0);
- Console.WriteLine("Points: " + Points);
- }
- public void ScreenDraw()
- {
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Yellow;
- Food.Draw();
- Console.ForegroundColor = ConsoleColor.Green;
- Snake.Draw();
- /*Console.ForegroundColor = ConsoleColor.Red;
- DrawPoints();*/
- Console.ForegroundColor = ConsoleColor.Cyan;
- Rocks.Draw();
- }
- public void Start()
- {
- while (true)
- {
- DirectionInput();
- if (Snake.Dir == Direction.Left)
- Snake.Head.Y--;
- else if (Snake.Dir == Direction.Right)
- Snake.Head.Y++;
- else if (Snake.Dir == Direction.Up)
- Snake.Head.X--;
- else if (Snake.Dir == Direction.Down)
- Snake.Head.X++;
- Snake.Update();
- foreach (Point p in Snake.Body)
- if (p.X == Food.Point.X && p.Y == Food.Point.Y)
- {
- Points++;
- Food = new FoodObject();
- Rocks = new RocksObject(Food.Point);
- //Snake.Grow(); Still building this one, so please ignore, having a problem this causes the program to crash
- }
- if (Snake.Head.X < 0 || Snake.Head.Y < 0 || Snake.Head.X == Console.WindowHeight || Snake.Head.Y == Console.WindowWidth)
- GameOver("Crashed a wall.");
- foreach (Point Rock in Rocks.List)
- if (Rock.X == Snake.Head.X && Rock.Y == Snake.Head.Y)
- GameOver("Crashed a rock.");
- ScreenDraw();
- System.Threading.Thread.Sleep((int)MS * 10);
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- SnakeGame Game = new SnakeGame();
- Game.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement