using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
namespace WpfApplication1
{
using System.Windows.Threading;
public partial class Window1 : Window
{
// This list describes the Bonus Red pieces of Food on the Canvas
private List<Point> bonusPoints = new List<Point>();
// This list describes the body of the snake on the Canvas
private List<Point> snakePoints = new List<Point>();
/// <summary>
/// Size of snake and eatables things
/// </summary>
private Brush snakeColor = Brushes.Green;
private enum SIZE
{
THIN = 4,
NORMAL = 6,
THICK = 8
};
/// <summary>
/// Moving Diection of snake can be changed from here.
/// </summary>
private enum MOVINGDIRECTION
{
UPWARDS = 8,
DOWNWARDS = 2,
TOLEFT = 4,
TORIGHT = 6
};
/// <summary>
/// Speed of Snake can be changed from here
/// </summary>
private TimeSpan FAST = new TimeSpan(10000); // Fast Speed
private TimeSpan MODERATE = new TimeSpan(30000); // moderate speed of snake can be handled here.
private TimeSpan SLOW = new TimeSpan(90000); // SLow Speed
private Point startingPoint = new Point(100, 100);
private Point currentPosition = new Point();
// Movement direction initialisation
private int direction = 0;
/* Placeholder for the previous movement direction
* The snake needs this to avoid its own body. */
private int previousDirection = 0;
/* Here user can change the size of the snake. and eatable things
* Possible sizes are THIN, NORMAL and THICK */
private int headSize = (int)SIZE.THICK;
private int length = 50; // length of snake is handled here.
private int score = 0; // Initiatl score.
private Random rnd = new Random(); // Random number which will add red balls for snake.
private DispatcherTimer timer;
public Window1()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Tick += new EventHandler(timer_Tick);
levellbl.Content = "1"; // level Label Number Setting
speedlbl.Content = "Speed : Slow"; // speed Setting here
/* Here user can change the speed of the snake.
* Possible speeds are FAST, MODERATE, SLOW and DAMNSLOW */
timer.Interval = SLOW; // Speed is Slow
timer.Start();
this.KeyDown += new KeyEventHandler(OnButtonKeyDown); // On any key down on keyboard device it will be triggered.
paintSnake(startingPoint); //Starting point of snake.
currentPosition = startingPoint;
// Instantiate Food Objects
int num = 0;
for (int n = 0; n < 1; n++)
{
paintBonus(num);
}
}
private void paintSnake(Point currentposition)
{
/* This method is used to paint a frame of the snake´s body
* each time it is called. */
Ellipse newEllipse = new Ellipse();
newEllipse.Fill = snakeColor;
newEllipse.Width = headSize;
newEllipse.Height = headSize;
Canvas.SetTop(newEllipse, currentposition.Y);
Canvas.SetLeft(newEllipse, currentposition.X);
int count = paintCanvas.Children.Count;
paintCanvas.Children.Add(newEllipse);
snakePoints.Add(currentposition);
// Restrict the tail of the snake
if (count > length)
{
paintCanvas.Children.RemoveAt(count - length);
snakePoints.RemoveAt(count - length);
}
}
private void paintBonus(int index)
{
Point bonusPoint = new Point(rnd.Next(5, 620), rnd.Next(5, 315)); // New points are generated which will add red balls.
//// Eatable red balls start////
Ellipse newEllipse = new Ellipse();
newEllipse.Fill = Brushes.Red;
newEllipse.Width = headSize;
newEllipse.Height = headSize;
Canvas.SetTop(newEllipse, bonusPoint.Y);
Canvas.SetLeft(newEllipse, bonusPoint.X);
paintCanvas.Children.Insert(index, newEllipse);
bonusPoints.Insert(index, bonusPoint);
//// Eatable red balls End////
}
private void timer_Tick(object sender, EventArgs e)
{
// Expand the body of the snake to the direction of movement
switch (direction)
{
case (int)MOVINGDIRECTION.DOWNWARDS:
currentPosition.Y += 1;
paintSnake(currentPosition);
break;
case (int)MOVINGDIRECTION.UPWARDS:
currentPosition.Y -= 1;
paintSnake(currentPosition);
break;
case (int)MOVINGDIRECTION.TOLEFT:
currentPosition.X -= 1;
paintSnake(currentPosition);
break;
case (int)MOVINGDIRECTION.TORIGHT:
currentPosition.X += 1;
paintSnake(currentPosition);
break;
}
// Restrict to boundaries of the Canvas
if ((currentPosition.X < 5) || (currentPosition.X > 620) ||
(currentPosition.Y < 5) || (currentPosition.Y > 315))
GameOver();
// Hitting a bonus Point causes the lengthen-Snake Effect
int n = 0;
foreach (Point point in bonusPoints)
{
if ((Math.Abs(point.X - currentPosition.X) < headSize) &&
(Math.Abs(point.Y - currentPosition.Y) < headSize))
{
length += 10;
score += 10;
scorelbl.Content = score.ToString();
if (score == 50) {
// MessageBox.Show("Level 1 Clear");
timer.Interval = MODERATE;
levellbl.Content = "2";
speedlbl.Content = "Speed : Moderate"; // speed Setting here
}
if (score == 100)
{
//MessageBox.Show("Level 2 Clear");
timer.Interval = FAST;
levellbl.Content = "3";
speedlbl.Content = "Speed : Fast"; // speed Setting here
}
// In the case of food consumption, erase the food object
// from the list of bonuses as well as from the canvas
bonusPoints.RemoveAt(n);
paintCanvas.Children.RemoveAt(n);
paintBonus(n);
break;
}
n++;
}
// Restrict hits to body of Snake
for (int q = 0; q < (snakePoints.Count - headSize*2); q++)
{
Point point = new Point(snakePoints[q].X, snakePoints[q].Y);
if ((Math.Abs(point.X - currentPosition.X) < (headSize)) &&
(Math.Abs(point.Y - currentPosition.Y) < (headSize)) )
{
GameOver();
break;
}
}
}
private void OnButtonKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Down:
if (previousDirection != (int)MOVINGDIRECTION.UPWARDS)
direction = (int)MOVINGDIRECTION.DOWNWARDS;
break;
case Key.Up:
if (previousDirection != (int)MOVINGDIRECTION.DOWNWARDS)
direction = (int)MOVINGDIRECTION.UPWARDS;
break;
case Key.Left:
if (previousDirection != (int)MOVINGDIRECTION.TORIGHT)
direction = (int)MOVINGDIRECTION.TOLEFT;
break;
case Key.Right:
if (previousDirection != (int)MOVINGDIRECTION.TOLEFT)
direction = (int)MOVINGDIRECTION.TORIGHT;
break;
}
previousDirection = direction;
}
private void GameOver()
{
StreamWriter writeData = new StreamWriter("Highscore.txt", true);
writeData.WriteLine(score);
writeData.Close();
MessageBox.Show("You Lose! Your score is "+ score.ToString(), "Game Over", MessageBoxButton.OK, MessageBoxImage.Hand);
this.Close();
}
}
}