first commit
This commit is contained in:
113
Snake.cs
Normal file
113
Snake.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
//using OpenTK.Graphics.OpenGL;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace SnakeMonoGame;
|
||||
|
||||
public class Snake
|
||||
{
|
||||
//Declarations
|
||||
public int Width, Height;
|
||||
|
||||
int Speed = 300;
|
||||
long UpdateTimer = Environment.TickCount;
|
||||
|
||||
List<string> Direction = new List<string>();
|
||||
string CurrentDir = "RightArrow";
|
||||
Point Move = new Point(1, 0);
|
||||
public List<Point> Segments = new List<Point>();
|
||||
public Point Food;
|
||||
Random Random = new Random();
|
||||
bool Grow = false;
|
||||
//
|
||||
public Snake(int Width, int Height)
|
||||
{
|
||||
this.Width = Width;
|
||||
this.Height = Height;
|
||||
|
||||
Segments.Add(new Point(this.Width / 2, this.Height / 2));
|
||||
PickFood();
|
||||
}
|
||||
public void Update()
|
||||
{
|
||||
if (Environment.TickCount > UpdateTimer + Speed)
|
||||
{
|
||||
UpdateTimer = Environment.TickCount;
|
||||
|
||||
//Set Direction
|
||||
if (Direction.Count > 0)
|
||||
{
|
||||
SetDirection(Direction[0]);
|
||||
Direction.RemoveAt(0);
|
||||
}
|
||||
|
||||
//Eat Food
|
||||
if (Segments[0] == Food) { Grow = true; PickFood(); }
|
||||
|
||||
//Move
|
||||
Point nSegment = new Point(Segments[0].X + Move.X, Segments[0].Y + Move.Y);
|
||||
Segments.Insert(0, nSegment);
|
||||
if (Grow == false) { Segments.RemoveAt(Segments.Count - 1); }
|
||||
else { Grow = false; }
|
||||
|
||||
//Death
|
||||
bool death = false;
|
||||
if (Segments[0].X < 0 || Segments[0].X >= Width || Segments[0].Y < 0 || Segments[0].Y >= Height)
|
||||
{
|
||||
death = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 1; i < Segments.Count; i++)
|
||||
{
|
||||
if (Segments[i] == Segments[0]) { death = true; break; }
|
||||
}
|
||||
}
|
||||
if (death) { Segments.Clear(); Segments.Add(new Point(this.Width / 2, this.Height / 2)); }
|
||||
}
|
||||
}
|
||||
public void AddDirection(string dir)
|
||||
{
|
||||
if (dir == "UpArrow" || dir == "DownArrow" || dir == "LeftArrow" || dir == "RightArrow")
|
||||
{
|
||||
if (Direction.Count > 0)
|
||||
{
|
||||
if (dir != Direction[Direction.Count - 1]) { Direction.Add(dir); }
|
||||
}
|
||||
else { Direction.Add(dir); }
|
||||
}
|
||||
}
|
||||
private void SetDirection(string dir)
|
||||
{
|
||||
if (CheckDirection(dir) == true)
|
||||
{
|
||||
switch (dir)
|
||||
{
|
||||
case "UpArrow": Move.X = 0; Move.Y = -1; break;
|
||||
case "DownArrow": Move.X = 0; Move.Y = 1; break;
|
||||
case "LeftArrow": Move.X = -1; Move.Y = 0; break;
|
||||
case "RightArrow": Move.X = 1; Move.Y = 0; break;
|
||||
}
|
||||
CurrentDir = dir;
|
||||
}
|
||||
}
|
||||
private bool CheckDirection(string dir)
|
||||
{
|
||||
if (dir == "LeftArrow" && CurrentDir == "RightArrow") { return false; }
|
||||
if (dir == "RightArrow" && CurrentDir == "LeftArrow") { return false; }
|
||||
if (dir == "UpArrow" && CurrentDir == "DownArrow") { return false; }
|
||||
if (dir == "DownArrow" && CurrentDir == "UpArrow") { return false; }
|
||||
return true;
|
||||
}
|
||||
private void PickFood()
|
||||
{
|
||||
bool Found = false;
|
||||
while (Found == false)
|
||||
{
|
||||
Found = true;
|
||||
Food = new Point(Random.Next(0, Width), Random.Next(0, Height));
|
||||
foreach (Point p in Segments) { if (Food == p) { Found = false; break; } }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user