欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF推箱子(一)

程序员文章站 2022-04-20 23:03:05
...

WPF推箱子(一)

设计界面:

WPF推箱子(一)
WPF推箱子(一)

开始游戏界面代码:

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.Shapes;

namespace WPF推箱子
{
    /// <summary>
    /// start.xaml 的交互逻辑
    /// </summary>
    public partial class start : Window
    {
        public start()
        {
            InitializeComponent();
        }

        private void Label_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.Left = 200;
            this.Top = 100;
            this.Hide();
            Game game = new Game();
            game.Show();
        }
    }
}

游戏界面xaml代码:

<Window x:Class="WPF推箱子.Game"
        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="推箱子" Height="600" Width="600"  Loaded="Window_Loaded" Icon="img/推箱子.ico" >
    <Grid  x:Name="ybg" >
        <Grid.RowDefinitions>
            <RowDefinition Height="29"></RowDefinition>
            <RowDefinition Height="60.8"></RowDefinition>
            <RowDefinition Height="700"></RowDefinition>
        </Grid.RowDefinitions>
        <Menu x:Name="menu" RenderTransformOrigin="0.5,0.5" Margin="0,2,2.6,2.8">
            <Menu.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform Angle="0.022"/>
                    <TranslateTransform/>
                </TransformGroup>
            </Menu.RenderTransform>
            <MenuItem Width="68" Height="24" Header="菜单">
                <MenuItem.Icon>
                    <Image Source="img/登录.png" Width="15" Height="30"></Image>
                </MenuItem.Icon>
                <MenuItem Header="重玩" Click="MenuItem_Click_13">
                    <MenuItem.Icon>
                        <Image Source="img/重新开始.png" Width="18" Height="30"></Image>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="选择关卡" Name="Next">
                    <MenuItem.Icon>
                        <Image Source="img/选择.png" Width="18" Height="30"></Image>
                    </MenuItem.Icon>
                    <MenuItem Click="MenuItem_Click_2" >
                        <MenuItem.Icon>
                            <Image Source="img/关注.png" Width="18" Height="30"></Image>
                        </MenuItem.Icon>
                        <MenuItem.Header>第一关</MenuItem.Header>
                    </MenuItem>
                    <MenuItem Click="MenuItem_Click_3" >
                        <MenuItem.Icon>
                            <Image Source="img/关注.png" Width="18" Height="30"></Image>
                        </MenuItem.Icon>
                        <MenuItem.Header>第二关</MenuItem.Header>
                    </MenuItem>
                    <MenuItem  Click="MenuItem_Click_4">
                        <MenuItem.Icon>
                            <Image Source="img/关注.png" Width="18" Height="30"></Image>
                        </MenuItem.Icon>
                        <MenuItem.Header >第三关</MenuItem.Header>
                    </MenuItem>
                    <MenuItem Click="MenuItem_Click">
                        <MenuItem.Icon>
                            <Image Source="img/关注.png" Width="18" Height="30"></Image>
                        </MenuItem.Icon>
                        <MenuItem.Header >第四关</MenuItem.Header>
                    </MenuItem>
                    <MenuItem Click="MenuItem_Click_9">
                        <MenuItem.Icon>
                            <Image Source="img/关注.png" Width="18" Height="30"></Image>
                        </MenuItem.Icon>
                        <MenuItem.Header >第五关</MenuItem.Header>
                    </MenuItem>
                    <MenuItem Click="MenuItem_Click_10">
                        <MenuItem.Icon>
                            <Image Source="img/关注.png" Width="18" Height="30"></Image>
                        </MenuItem.Icon>
                        <MenuItem.Header >第六关</MenuItem.Header>
                    </MenuItem>
                    <MenuItem Click="MenuItem_Click_11">
                        <MenuItem.Icon>
                            <Image Source="img/关注.png" Width="18" Height="30"></Image>
                        </MenuItem.Icon>
                        <MenuItem.Header >第七关</MenuItem.Header>
                    </MenuItem>
                    <MenuItem Click="MenuItem_Click_12">
                        <MenuItem.Icon>
                            <Image Source="img/关注.png" Width="18" Height="30"></Image>
                        </MenuItem.Icon>
                        <MenuItem.Header >第八关</MenuItem.Header>
                    </MenuItem>
                    <MenuItem Click="MenuItem_Click_14">
                        <MenuItem.Icon>
                            <Image Source="img/关注.png" Width="18" Height="30"></Image>
                        </MenuItem.Icon>
                        <MenuItem.Header >第九关</MenuItem.Header>
                    </MenuItem>
                    <MenuItem Click="MenuItem_Click_15">
                        <MenuItem.Icon>
                            <Image Source="img/关注.png" Width="18" Height="30"></Image>
                        </MenuItem.Icon>
                        <MenuItem.Header >第十关</MenuItem.Header>
                    </MenuItem>
                </MenuItem>
                <MenuItem Name="Close" Header="退出游戏"  Click="Close_Click">
                    <MenuItem.Icon>
                        <Image Source="img/退出.png" Width="18" Height="30"></Image>
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
            <MenuItem Width="83" Height="24" Header="设置背景" >
                <MenuItem.Icon>
                    <Image Source="img/设置.png" Width="15" Height="30"></Image>
                </MenuItem.Icon>
                <MenuItem  Header="春" Click="MenuItem_Click_7"  >
                    <MenuItem.Icon>
                        <Image Source="img/关注.png" Width="18" Height="30"></Image>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem  Header="夏" Click="MenuItem_Click_5" >
                    <MenuItem.Icon>
                        <Image Source="img/关注.png" Width="18" Height="30"></Image>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem  Header="秋" Click="MenuItem_Click_6">
                    <MenuItem.Icon>
                        <Image Source="img/关注.png" Width="18" Height="30"></Image>
                    </MenuItem.Icon>
                </MenuItem>
            </MenuItem>
        </Menu>
        <Canvas Grid.Row="1">
            <Label   Canvas.Left="406" Canvas.Top="44" FontSize="20" Foreground="DarkSeaGreen" RenderTransformOrigin="1.13,0.992">时间:</Label>
            <Label Name="tim" Foreground="Red" FontSize="25"   Canvas.Left="466" Canvas.Top="41" Height="44" Width="64"></Label>
            <Label Canvas.Left="168" Canvas.Top="41" Background="Pink"   FontWeight="Bold" FontSize="20" Height="38" Width="96" >当前关卡:</Label>
            <Label Name="gk" Canvas.Left="282" Canvas.Top="42" FontWeight="Bold" Foreground="Purple" FontStyle="Italic"    FontSize="25"></Label>
        </Canvas>
        <Grid x:Name="bg" Grid.Row="2" Margin="99,47.4,94.6,276.6"/>
    /Grid>
</Window>

XAML.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.Shapes;
using System.Windows.Threading;
using System.IO;
using System.Media;

namespace WPF推箱子
{
    /// <summary>
    /// Game.xaml 的交互逻辑
    /// </summary>
    public partial class Game : Window
    {
        public Game()
        {
            InitializeComponent();
        }
        //背景音乐
        MediaPlayer music = new MediaPlayer();
        SoundPlayer sound = new SoundPlayer("../../music/yes.wav");
        SoundPlayer sound1 = new SoundPlayer("../../music/游戏胜利.wav");
        //装图片
        Image[,] maplist=new  Image[10,10];
        int size = 10;
        //创建人
        Person per = new Person();
        Image pimg;
        //关卡
        int index = 1;
        //地图的数组
        int[,] map;
        //行列
        int p, d;
        //自定义记录人的坐标
        int px; int py;
        //计时器
        DispatcherTimer timer = new DispatcherTimer();
        //通关时间
        int time = 0;
        //清空地图
        void Clear()
        {
            bg.RowDefinitions.Clear();
            bg.ColumnDefinitions.Clear();
            bg.Children.Clear();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Left = 200;
            this.Top = 100;
            music.Open(new Uri(@"../../music/mic1.wav", UriKind.Relative));
            music.MediaEnded += Music_MediaEnded;
            music.Play();
            index = 1;
            gk.Content = "第" + index + "关";
            BGimage("../../img/bg3.jpg");
            Createmap();
            person(5, 5);
            timer.Interval = TimeSpan.FromMilliseconds(1000);
            timer.Tick += Timer_Tick;
            timer.Start();
            this.KeyDown += Game_KeyDown;
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            Time();
        }
        void Time() 
        {
            time++;
            tim.Content = time + "s";
        }
        //循环背景音乐
        private void Music_MediaEnded(object sender, EventArgs e)
        {
            music.Position = new TimeSpan(0);
        }
       // string dir = " ";
        //控制移动
        private void Game_KeyDown(object sender, KeyEventArgs e)
        {

            //人的坐标
            int row, col;
            row = Grid.GetRow(pimg);
            col = Grid.GetColumn(pimg);
         /// pimg.Source=new BitmapImage(new Uri("img/"+dir+".png",UriKind.Relative));
            switch (e.Key)
            {
                case Key.Left:
                   // dir = "left";
                    panduan(row , col - 1, row, col - 2);
                    break;
                case Key.Right:
                    //dir = "right";
                    panduan(row , col + 1, row, col + 2);
                    break;
                case Key.Up:
                    panduan(row - 1, col , row - 2, col);
                   // dir = "up";
                    break;
                case Key.Down:
                    panduan(row + 1, col , row + 2, col );
                   // dir = "down";
                    break;
            }
        }
        // 0为路  1为人 2为墙 3为箱子 4为完成后 5为目标
        //记录位置
        int rows, cols;
        bool isbox = false;
        //记录通关时间
        string tpath = "../../End/time.txt";
        /// <summary>
        /// 更新位置
        /// </summary>
        /// <param name="row">纵坐标</param>
        /// <param name="col">横坐标</param>
        /// <param name="nextrow">下一个纵坐标</param>
        /// <param name="nextcol">下一个横坐标</param>
        private void panduan(int row ,int col,int nextrow,int nextcol)
        {
            switch (map[row, col])
            {
                    //路 
                    case 0: 
                    Grid.SetRow(pimg, row);
                    Grid.SetColumn(pimg, col);
                    isbox = false;
                    break;
                    //墙 
                    case 2:
                    break;
                    //箱子
                    case 3:
                    //如果箱子的位置为路
                    switch (map[nextrow,nextcol])
                    {
                        //箱子的下一个位置是路
                        case 0:
                            isbox = false;
                            map[row, col] = 0;
                            map[nextrow, nextcol] = 3;
                            maplist[row, col].Source = new BitmapImage(new Uri("img/tall.png", UriKind.Relative));
                            maplist[nextrow, nextcol].Source = new BitmapImage(new Uri("img/box.png", UriKind.Relative));
                            Grid.SetRow(pimg, row);
                            Grid.SetColumn(pimg, col);
                            break;
                            //箱子的下一个位置是箱子的话 
                        case 3:
                            break;

                        default:
                            //记录箱子的位置
                            rows = row;
                            cols = col;
                            //判断下一个位置
                            isbox = true;
                            panduan(nextrow, nextcol, nextrow, nextcol);
                            break;
                    }
                    break;
                //判断是完成后的时候
                case 4:
                    // 完成后下个位置是目标的话
                    if (map[nextrow, nextcol] == 5)
                    {
                        map[nextrow, nextcol] = 4;
                        maplist[nextrow, nextcol].Source = new BitmapImage(new Uri("img/BoD.png", UriKind.Relative));
                        map[row, col] = 5;
                        maplist[row, col].Source = new BitmapImage(new Uri("img/dest.png", UriKind.Relative));
                        Grid.SetRow(pimg, row);
                        Grid.SetColumn(pimg, col);
                    }
                    //如果完成下个位置是路的话
                    else if(map[nextrow, nextcol] == 0)
                    {
                        map[nextrow, nextcol] = 3;
                        maplist[nextrow, nextcol].Source = new BitmapImage(new Uri("img/box.png", UriKind.Relative));
                        map[row, col] = 5;
                        maplist[row, col].Source = new BitmapImage(new Uri("img/dest.png", UriKind.Relative));
                        Grid.SetRow(pimg, row);
                        Grid.SetColumn(pimg, col);
                    }
                    else if (map[nextrow, nextcol] == 2)
                    {
                        return;
                    }
                    break;
                    //下个为目标的话
                case 5:
                    if (isbox)
                    {
                        map[rows, cols] = 0;
                        maplist[rows, cols].Source= new BitmapImage(new Uri("img/tall.png", UriKind.Relative));
                        map[nextrow, nextcol] = 4;
                        maplist[nextrow, nextcol].Source = new BitmapImage(new Uri("img/BoD.png", UriKind.Relative));
                        Grid.SetRow(pimg, rows);
                        Grid.SetColumn(pimg, cols);
                        sound.Play();
                        isbox = false;
                        Pass();
                    }
                    else
                    {
                        Grid.SetRow(pimg, row);
                        Grid.SetColumn(pimg, col);
                    }
                    break;
            }
        }
            //背景
        private void MenuItem_Click_5(object sender, RoutedEventArgs e)
        {
            BGimage("../../img/bg1.jpg");
        }
        private void MenuItem_Click_6(object sender, RoutedEventArgs e)
        {
            BGimage("../../img/bg3.jpg");
        }
        private void MenuItem_Click_7(object sender, RoutedEventArgs e)
        {
            BGimage("../../img/bg2.jpg");
        }
          //退出
        private void Close_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Application.Current.Shutdown();
        }
       //背景图
        private void BGimage(string BGpath)
        {
            Image image1 = new Image();
            image1.Source = new BitmapImage(new Uri(BGpath, UriKind.RelativeOrAbsolute));
            ImageBrush brush1 = new ImageBrush();
            brush1.ImageSource = image1.Source;
            ybg.Background = brush1;
        }
相关标签: WPF推箱子