WPF-贪吃蛇

本文介绍如何使用WPF技术开发一款贪吃蛇游戏,包括XAML布局设计和C#后端逻辑实现,详细探讨了图形用户界面的创建与事件处理。
摘要由CSDN通过智能技术生成

 

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;
            //倒计时
            
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值