xaml
<Window x:Class="贪吃蛇wpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:贪吃蛇wpf"
mc:Ignorable="d"
Title="wpf贪吃蛇" Height="450" Width="800" Loaded="Window_Loaded" Icon="img/favicon.ico">
<Canvas x:Name="BG">
<Button Content="一级加速" Canvas.Left="1450" Canvas.Top="316" Width="76" Height="40" FontSize="18" Background="Cyan" Click="Button_Click"></Button>
<Button Content="二级加速" Canvas.Left="1450" Canvas.Top="398" Width="76" Height="40" FontSize="18" Background="Cyan" Click="Button_Click_1"></Button>
<Button Content="极速" Canvas.Left="1450" Canvas.Top="480" Width="76" Height="40" FontSize="18" Background="Cyan" Click="Button_Click_2"></Button>
</Canvas>
</Window>
cs
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;
namespace 贪吃蛇wpf
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
//去边框
this.WindowStyle = WindowStyle.None;
//允许透明
this.AllowsTransparency = true;
InitializeComponent();
}
//边框设置
Border border = new Border();
//创造画布与边框内大小一致
Canvas game = new Canvas();
//创建食物
Random r = new Random();
Border food = new Border();
Border food1 = new Border();
//积分按钮
Button jf = new Button();
Button jf1 = new Button();
//倒计时效果
int num = 4;
Label shijain = new Label();
//蛇移动timer
DispatcherTimer moveTimer = new DispatcherTimer();
DispatcherTimer moveTimer1 = new DispatcherTimer();
//倒计时
DispatcherTimer daotimer = new DispatcherTimer();
//音效
MediaPlayer music = new MediaPlayer();
MediaPlayer music1 = new MediaPlayer();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//窗口最大化
this.WindowState = WindowState.Maximized;
//透明
this.Background = Brushes.Transparent;
//渐变颜色
BG.Background = new RadialGradientBrush(Colors.White, Colors.Green);
//透明度设置
BG.Opacity = 0.6;
//蛇1移动timer
moveTimer.Interval = TimeSpan.FromMilliseconds(300);
moveTimer.Tick += MoveTimer_Tick;
//蛇②移动timer
moveTimer1.Interval = TimeSpan.FromMilliseconds(300);
moveTimer1.Tick += MoveTimer1_Tick;
//倒计时