游戏的显示界面
this.WindowState = WindowState.Maximized;//最大化
//this.Opacity = 0.4;
//this.Background = Brushes.Transparent;//透明
this.Background = new RadialGradientBrush(Colors.Blue, Colors.White); //线性渐变
//熊猫蛇移动
//DispatcherTimer sd = new DispatcherTimer();
sd.Interval = TimeSpan.FromMilliseconds(100);
sd.Tick += Sd_Tick;
//黄蜂蛇移动
ds.Interval = TimeSpan.FromMilliseconds(100);
ds.Tick += Ds_Tick;
//键盘事件
KeyDown += MainWindow_KeyDown;
//判断框
BG.Width = this.Width;//1548 游戏界面等于主界面
BG.Height = this.Height;//845 游戏界面等于主界面
dd.Width = size * cols-20;//框的宽
dd.Height = size * rows;//框的高
dd.BorderThickness = new Thickness(20);//边框的厚度
dd.BorderBrush = new LinearGradientBrush(Colors.White, Colors.Black, 0);
dd.CornerRadius = new CornerRadius(10);
left = Canvas.GetLeft(dd) + 20;
top = Canvas.GetTop(dd) + 20;
Canvas.SetLeft(dd,40);
Canvas.SetTop(dd,(BG.Height-dd.Height)/2);
BG.Children.Add(dd);
//开始游戏
//Button ks = new Button();
ks.Width = 200;
ks.Height = 130;
ks.FontSize = 30;
ks.Content = "开始游戏";
Canvas.SetLeft(ks,BG.Width/2-ks.Width/2);
Canvas.SetTop(ks,400 );
ks.Background = new LinearGradientBrush(Colors.White, Colors.Black, 90);
ks.Click += Ks_Click;
BG.Children.Add(ks);
//记录分数
//小熊猫
//Label xxm = new Label();
xxm.Width =150;
xxm.Height = 100;
xxm.Content = "小熊猫的分数:";
xxm.FontSize = 10;
xxm.Background = Brushes.Transparent;
Canvas.SetLeft(xxm,110);
Canvas.SetTop(xxm,60);
BG.Children.Add(xxm);
//小黄蜂
//Label xhf = new Label();
xhf.Width = 150;
xhf.Height = 100;
xhf.Content = "小黄蜂的分数:";
xhf.FontSize = 10;
xhf.Background = Brushes.Transparent;
Canvas.SetLeft(xhf,1200);
Canvas.SetTop(xhf,60);
BG.Children.Add(xhf);
yy.Open(new Uri(@"../../music/BG音乐.wav", UriKind.Relative));
yy.Play();
yy.MediaEnded += Yy_MediaEnded;
游戏界面之黑白蛇的移动和吃食物生成
for (int i = 0; i < snake.Count; i++)
{
if (snake[i].Tag.ToString() == "Left")
{
Canvas.SetLeft(snake[i], Canvas.GetLeft(snake[i]) - size);
}
else if (snake[i].Tag.ToString() == "Right")
{
Canvas.SetLeft(snake[i], Canvas.GetLeft(snake[i]) + size);
}
else if (snake[i].Tag.ToString() == "Up")
{
Canvas.SetTop(snake[i],Canvas.GetTop(snake[i])-size);
}
else if (snake[i].Tag.ToString()=="Down")
{
Canvas.SetTop(snake[i],Canvas.GetTop(snake[i])+ size);
}
}
for (int i = snake.Count - 1; i > 0; i--)
{
snake[i].Tag = snake[i - 1].Tag;
}
//小熊猫碰瓷
for (int i = 1; i < snake.Count-1 ; i++)
{
for (int j = 1; j < snakes.Count-1; j++)
{
if (/*Canvas.GetLeft(snake[0]) == Canvas.GetLeft(snake[i]) && Canvas.GetTop(snake[0]) == Canvas.GetTop(snake[i]) ||*/ Canvas.GetLeft(snake[0]) == Canvas.GetLeft(snakes[j]) && Canvas.GetTop(snake[0]) == Canvas.GetTop(snakes[j]) || Canvas.GetLeft(snake[0]) == Canvas.GetLeft(snakes[0]) && Canvas.GetTop(snake[0]) == Canvas.GetTop(snakes[0]))
{
sd.Stop();
for (int k = 0; k < snake.Count; k++)
{
BG.Children.Remove(snake[k]);
}
ds.Stop();
MessageBox.Show("小熊猫碰瓷了");
MessageBox.Show("小黄蜂获得胜利"+"小黄蜂获得" + y + "分");
MessageBoxResult ck = MessageBox.Show("请重新开始游戏", "游戏结束", MessageBoxButton.YesNo);
if (ck == MessageBoxResult.Yes)
{
BG.Children.Clear();//清除所有
snake.Clear();//释放小熊猫
snakes.Clear();//释放小黄蜂
y = 0;//释放小黄蜂的计分板
xhf.Content = "小黄蜂的分数:" + y + "分";
x = 0;//释放小熊猫的计分板
xxm.Content = "小熊猫的分数:" + x + "分";
BG.Children.Add(dd);//加载框
BG.Children.Add(xxm);//加载小熊猫的计分板
BG.Children.Add(xhf);//加载小黄蜂的计分板
BG.Children.Add(ks);//加载开始事件
return;
}
else
{
this.Close();
}
}
}
}
//小熊猫撞墙
if (Canvas.GetLeft(snake[0])>=1400||Canvas.GetTop(snake[0])>=dd.Height||Canvas.GetLeft(snake[0])<=40|| Canvas.GetTop(snake[0])<=40)
{
sd.Stop();
for (int i = 0; i < snake.Count; i++)
{
BG.Children.Remove(snake[i]);
}
//MessageBox.Show("请重新开始游戏");
ds.Stop();
MessageBox.Show("小黄蜂获得胜利"+"小黄蜂获得" + y + "分");
MessageBoxResult ck = MessageBox.Show("请重新开始游戏", "游戏结束", MessageBoxButton.YesNo);
if (ck == MessageBoxResult.Yes)
{
BG.Children.Clear();//清除所有
snake.Clear();//释放小熊猫
snakes.Clear();//释放小黄蜂
y = 0;//释放小黄蜂的计分板
xhf.Content = "小黄蜂的分数:" + y + "分";
x = 0;//释放小熊猫的计分板
xxm.Content = "小熊猫的分数:" + x + "分";
BG.Children.Add(dd);//加载框
BG.Children.Add(xxm);//加载小熊猫的计分板
BG.Children.Add(xhf);//加载小黄蜂的计分板
BG.Children.Add(ks);//加载开始事件
return;
}
else
{
this.Close();
}
}
//小熊猫吃豆豆长身体
if (Canvas.GetLeft(snake[0])==Canvas.GetLeft(food)&&Canvas.GetTop(snake[0])==Canvas.GetTop(food))
{
BG.Children.Remove(food);
CreatDoDo();
Border b = new Border();
b.Width = b.Height = size;
b.CornerRadius = new CornerRadius(20);
z++;
if (z % 2 == 0)
{
b.Background = Brushes.White;
}
else
{
b.Background = Brushes.Black;
}
//b.Background = new SolidColorBrush(Color.FromRgb(Convert.ToByte(r.Next(255)), Convert.ToByte(r.Next(255)), Convert.ToByte(r.Next(255))));
b.Tag = " ";
x++;
xxm.Content = "小熊猫的分数:" + x + "分";
if (x>5)
{
sd.Interval = TimeSpan.FromMilliseconds(50);
}
if (x > 20)
{
sd.Interval = TimeSpan.FromMilliseconds(10);
}
Canvas.SetLeft(b,Canvas.GetLeft(snake[snake.Count-1]));
Canvas.SetTop(b, Canvas.GetTop(snake[snake.Count - 1]));
snake.Add(b);
BG.Children.Add(b);
}