分析
1,实现地图,蛇,食物的出现
2,蛇的长度以及蛇头与蛇身的区别
3,食物的随机位置
4,实现开始继续让蛇停止与继续移动
5,蛇吃到食物加分数
6,蛇碰撞到墙壁或者蛇身就会死亡
代码实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using System.Windows.Threading;
using System.Media;
using System.Data.SqlClient;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//窗体边框不显示
this.WindowStyle = WindowStyle.None;
}
DispatcherTimer moverTimer = new DispatcherTimer();//计时器
Random ra = new Random();//创建随机数,食物所需要
Border food = new Border();//创建食物
int fenshu = 0; //记录分数
Label feng = new Label(); // 分数对象
List<Border> snake = new List<Border>();
//更方便计算
double size = 20;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Background = Brushes.Transparent;
//全屏显示
this.WindowState = WindowState.Maximized;
this.Background = Brushes.Transparent;
//颜色渐变
continer.Background = new RadialGradientBrush(Colors.White,Colors.Blue);
//正常显示
continer.Width = this.Width;
continer.Height = this.Height;
feng.Width = 120;
feng.Height = 50;
Canvas.SetLeft(feng, 1060);
Canvas.SetTop(feng, 100);
feng.Background = Brushes