using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
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.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WPFSnake
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private DispatcherTimer timer;
public int bodyCount = 5;
public Direction direct = Direction.Right;
//public BodyCell[] cells;
public List<BodyCell> cells;
public Image food;
public MainWindow()
{
InitializeComponent();
}
private void panel_Loaded(object sender, RoutedEventArgs e)
{
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
cells = new List<BodyCell>();
for (int i = 0; i < bodyCount; i++)
{
BodyCell cell = new BodyCell();
double left = i * 10;
double top = panel.Height / 2;
Canvas.SetLeft(cell, left);
Canvas.SetTop(cell, top);
//cell.Margin.Top= panel.Height / 2;
cells.Add(cell);
panel.Children.Add(cell);
}
food = new Image();
food.Height = 10;
food.Width = 10;
BitmapImage pic = new BitmapImage(new Uri("Image/bg.jpg", UriKind.Relative));
food.Source = pic;
Random rY = new Random();
double ftop = rY.Next(0, 30) * 10;
Random rX = new Random();
double fleft = rX.Next(0, 30) * 10;
//food.Margin = new Thickness(fleft, ftop, 0, 0);
Canvas.SetTop(food, ftop);
Canvas.SetLeft(food, fleft);
panel.Children.Add(food);
timer = new DispatcherTimer();
timer.Tick += timer_Tick;
timer.Interval = TimeSpan.FromMilliseconds(100);
timer.Start();
}
private void timer_Tick(object sender, EventArgs args)
{
SnakeProcess();
if (GameOver())
{
MessageBox.Show("Game Over.");
timer.Stop();
}
}
private void SnakeProcess()
{
#region 移动
#region 版本1
for (int i = bodyCount - 1; i >= 0; i--)
{
if (cells[i].direct == Direction.Right)
{
//cells[i].Left += 10;
Canvas.SetLeft(cells[i], Canvas.GetLeft(cells[i]) + 10);
}
else if (cells[i].direct == Direction.Left)
{
//cells[i].Left -= 10;
Canvas.SetLeft(cells[i], Canvas.GetLeft(cells[i]) - 10);
}
else if (cells[i].direct == Direction.Up)
{
//cells[i].Top -= 10;
Canvas.SetTop(cells[i], Canvas.GetTop(cells[i]) - 10);
}
else if (cells[i].direct == Direction.Down)
{
//cells[i].Top += 10;
Canvas.SetTop(cells[i], Canvas.GetTop(cells[i]) + 10);
}
}
for (int i = 0; i < bodyCount - 1; i++)
{
cells[i].direct = cells[i + 1].direct;
}
#endregion
#region 版本2
//for (int i = bodyCount - 1; i > 0; i--)
//{
// cells[i - 1] = cells[i];
//}
//if (cells[bodyCount - 1].direct == Direction.Right)
//{
// cells[bodyCount - 1].Margin.Left += 10;
//}
//else if (cells[bodyCount - 1].direct == Direction.Left)
//{
// cells[bodyCount - 1].Left -= 10;
//}
//else if (cells[bodyCount - 1].direct == Direction.Up)
//{
// cells[bodyCount - 1].Top -= 10;
//}
//else if (cells[bodyCount - 1].direct == Direction.Down)
//{
// cells[bodyCount - 1].Top += 10;
//}
#endregion
#endregion
#region 吃食物
EatFood();
#endregion
}
private bool GameOver()
{
bool isOver = false;
//撞墙
double top = Canvas.GetTop(cells[bodyCount - 1]);
double left = Canvas.GetLeft(cells[bodyCount - 1]);
if (top == 0 && cells[bodyCount - 1].direct == Direction.Up)
{
isOver = true;
}
if (top == 300 && cells[bodyCount - 1].direct == Direction.Down)
{
isOver = true;
}
if (left == 0 && cells[bodyCount - 1].direct == Direction.Left)
{
isOver = true;
}
if (left == 300 && cells[bodyCount - 1].direct == Direction.Right)
{
isOver = true;
}
//撞自己
for (int i = bodyCount - 2; i >= 0; i--)
{
if (Canvas.GetTop(cells[i]) == Canvas.GetTop(cells[bodyCount - 1]) && Canvas.GetLeft(cells[i]) == Canvas.GetLeft(cells[bodyCount - 1]))
{
isOver = true;
break;
}
}
return isOver;
}
private void EatFood()
{
BodyCell head = cells[bodyCount - 1];
if (Canvas.GetTop(cells[bodyCount - 1]) == Canvas.GetTop(food) && Canvas.GetLeft(cells[bodyCount - 1]) == Canvas.GetLeft(food))
{
BodyCell cell = new BodyCell();
panel.Children.Add(cell);
cell.direct = head.direct;
if (head.direct == Direction.Up)
{
//cell.Top = head.Top - 10;
//cell.Left = head.Left;
Canvas.SetTop(cell, Canvas.GetTop(head) - 10);
Canvas.SetLeft(cell, Canvas.GetLeft(head));
}
else if (head.direct == Direction.Down)
{
//cell.Top = head.Top + 10;
//cell.Left = head.Left;
Canvas.SetTop(cell, Canvas.GetTop(head) + 10);
Canvas.SetLeft(cell, Canvas.GetLeft(head));
}
else if (head.direct == Direction.Left)
{
//cell.Top = head.Top;
//cell.Left = head.Left - 10;
Canvas.SetTop(cell, Canvas.GetTop(head));
Canvas.SetLeft(cell, Canvas.GetLeft(head) - 10);
}
else if (head.direct == Direction.Right)
{
//cell.Top = head.Top;
//cell.Left = head.Left + 10;
Canvas.SetTop(cell, Canvas.GetTop(head));
Canvas.SetLeft(cell, Canvas.GetLeft(head) + 10);
}
cells.Add(cell);
bodyCount++;
//lbl_Score.Text = (bodyCount - 5).ToString();
panel.Children.Remove(food);
Random rY = new Random();
//food.Top = rY.Next(0, 30) * 10;
Canvas.SetTop(food, rY.Next(0, 30) * 10);
Random rX = new Random();
//food.Left = rX.Next(0, 30) * 10;
Canvas.SetLeft(food, rX.Next(0, 30) * 10);
panel.Children.Add(food);
}
}
private void Window_KeyDown_1(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up && cells[bodyCount - 1].direct != Direction.Down)
{
//direct = Direction.Up;
cells[bodyCount - 1].direct = Direction.Up;
}
else if (e.Key == Key.Left && cells[bodyCount - 1].direct != Direction.Right)
{
//direct = Direction.Left;
cells[bodyCount - 1].direct = Direction.Left;
}
else if (e.Key == Key.Down && cells[bodyCount - 1].direct != Direction.Up)
{
//direct = Direction.Down;
cells[bodyCount - 1].direct = Direction.Down;
}
else if (e.Key == Key.Right && cells[bodyCount - 1].direct != Direction.Left)
{
//direct = Direction.Right;
cells[bodyCount - 1].direct = Direction.Right;
}
if (e.Key == Key.Space)
{
if (timer.IsEnabled)
{
timer.Stop();
}
else
{
timer.Start();
}
}
}
}
public class BodyCell : Image
{
public Direction direct
{ get; set; }
public BodyCell()
: base()
{
this.Width = 10;
this.Height = 10;
direct = Direction.Right;
BitmapImage pic = new BitmapImage(new Uri("Image/bg.jpg", UriKind.Relative));
this.Source = pic;
}
}
public enum Direction
{
Up,
Down,
Left,
Right
}
}