Here is a simple console-based implementation of the classic Snake game using C# and Object-Oriented Programming.
main.csusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SnakeGame
{
// Define the direction to where the snake is moving
public enum Direction
{
Up,
Down,
Left,
Right
}
// Define the cell in the game grid
public struct Cell
{
public int X;
public int Y;
public Cell(int x, int y)
{
X = x;
Y = y;
}
}
// Define the snake object
public class Snake
{
// Snake body is a list of cell
public List<Cell> Body { get; set; }
// Direction of the snake
public Direction Direction { get; set; }
public Snake()
{
// Initialize snake body with 3 cells at the center of the screen
Body = new List<Cell>()
{
new Cell(Console.WindowWidth/2 - 1, Console.WindowHeight/2),
new Cell(Console.WindowWidth/2, Console.WindowHeight/2),
new Cell(Console.WindowWidth/2 + 1, Console.WindowHeight/2),
};
// Initial direction is to the right
Direction = Direction.Right;
}
// Move the snake
public void Move()
{
// Determine the old head and new head of the snake based on direction
Cell oldHead = Body[0];
Cell newHead = new Cell(oldHead.X, oldHead.Y);
switch (Direction)
{
case Direction.Up:
newHead.Y--;
break;
case Direction.Down:
newHead.Y++;
break;
case Direction.Left:
newHead.X--;
break;
case Direction.Right:
newHead.X++;
break;
}
// Insert the new head to the front of the Body list
Body.Insert(0, newHead);
// Remove the last cell of the Body list
Body.RemoveAt(Body.Count - 1);
}
// Grow the snake by adding a new cell to its body
public void Grow()
{
// Determine the position of the new cell based on the direction of the last cell
Cell lastCell = Body[Body.Count - 1];
Cell newCell = new Cell(lastCell.X, lastCell.Y);
switch (Direction)
{
case Direction.Up:
newCell.Y++;
break;
case Direction.Down:
newCell.Y--;
break;
case Direction.Left:
newCell.X++;
break;
case Direction.Right:
newCell.X--;
break;
}
// Add the new cell to the end of the Body list
Body.Add(newCell);
}
// Check if the snake has collided with the wall or itself
public bool HasCollided()
{
// Check if the head of the snake is outside the game grid
Cell head = Body[0];
if (head.X < 0 || head.X >= Console.WindowWidth || head.Y < 0 || head.Y >= Console.WindowHeight)
{
return true;
}
// Check if the head of the snake has collided with its body
for (int i = 1; i < Body.Count; i++)
{
if (head.X == Body[i].X && head.Y == Body[i].Y)
{
return true;
}
}
return false;
}
}
class Program
{
static void Main(string[] args)
{
// Hide the cursor and set the console title
Console.CursorVisible = false;
Console.Title = "Snake Game";
// Create a new snake object
Snake snake = new Snake();
// Set the initial game speed and food position
int speed = 100;
Cell food = new Cell();
// Game loop
while (true)
{
// Clear the console and draw the snake and food
Console.Clear();
foreach (Cell cell in snake.Body)
{
Console.SetCursorPosition(cell.X, cell.Y);
Console.Write("*");
}
Console.SetCursorPosition(food.X, food.Y);
Console.Write("#");
// Check if the snake has collided with the wall or itself
if (snake.HasCollided())
{
Console.SetCursorPosition(Console.WindowWidth / 2 - 4, Console.WindowHeight / 2);
Console.Write("Game Over!");
break;
}
// Check if the snake head has reached the food
if (snake.Body[0].X == food.X && snake.Body[0].Y == food.Y)
{
// Grow the snake and randomly generate a new food position
snake.Grow();
Random random = new Random();
food = new Cell(random.Next(Console.WindowWidth), random.Next(Console.WindowHeight));
speed -= 5;
if (speed < 50)
{
speed = 50;
}
}
// Move the snake to the new position based on its direction
snake.Move();
// Get the direction input from the user
if (Console.KeyAvailable)
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
switch (keyInfo.Key)
{
case ConsoleKey.UpArrow:
if (snake.Direction != Direction.Down)
{
snake.Direction = Direction.Up;
}
break;
case ConsoleKey.DownArrow:
if (snake.Direction != Direction.Up)
{
snake.Direction = Direction.Down;
}
break;
case ConsoleKey.LeftArrow:
if (snake.Direction != Direction.Right)
{
snake.Direction = Direction.Left;
}
break;
case ConsoleKey.RightArrow:
if (snake.Direction != Direction.Left)
{
snake.Direction = Direction.Right;
}
break;
default:
break;
}
}
// Wait for a short amount of time to control game speed
System.Threading.Thread.Sleep(speed);
}
}
}
}
7228 chars226 lines
gistlibby LogSnag