WPF推箱子(三)
自定义游戏:
自定义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="自定义" Height="600" Width="600" Loaded="Window_Loaded" Icon="img/推箱子.ico">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="41*"/>
<ColumnDefinition Width="553*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="81*">
</RowDefinition>
<RowDefinition Height="490*">
</RowDefinition>
</Grid.RowDefinitions>
<Canvas Grid.Row="0" Name="bg1" RenderTransformOrigin="0.631,0.531" Grid.ColumnSpan="2" Margin="0,0,0.333,489.667" Grid.RowSpan="2">
<Image Source="img/墙.png" Width="40" Height="40" x:Name="墙" Canvas.Left="103" MouseDown="wall_MouseDown" RenderTransformOrigin="1.912,1.126" Canvas.Top="10" ></Image>
<Image Source="img/tall.png" Width="40" Height="40" x:Name="路" MouseDown="wall_MouseDown" RenderTransformOrigin="1.36,1.153" Canvas.Left="23" Canvas.Top="10" ></Image>
<Image Source="img/box.png" Width="40" Height="40" x:Name="箱子" Canvas.Left="191" MouseDown="wall_MouseDown" RenderTransformOrigin="1.912,1.126" Canvas.Top="10" ></Image>
<Image Source="img/dest.png" Width="40" Height="40" x:Name="目标" Canvas.Left="270" MouseDown="wall_MouseDown" RenderTransformOrigin="1.36,1.153" Canvas.Top="10" ></Image>
<Image Source="img/公主.png" Width="40" Height="40" x:Name="人" Canvas.Left="353" MouseDown="wall_MouseDown" Canvas.Top="10" ></Image>
<Button x:Name="btn" Canvas.Left="476" Canvas.Top="17" Height="36" Width="70" Click="btn_Click" >保存</Button>
</Canvas>
<Grid x:Name="bg" Grid.Row="1" Width="400" Height="400" Grid.Column="1" Margin="56.227,45.333,96.333,44.667" />
</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.Navigation;
using System.Windows.Shapes;
using System.IO;
namespace WPF推箱子
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
int[] index;
int size = 10;
Image[,] maplist;
int[,] map;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
for (int i = 0; i < size; i++)
{
RowDefinition row = new RowDefinition();
bg.RowDefinitions.Add(row);
ColumnDefinition col = new ColumnDefinition();
bg.ColumnDefinitions.Add(col);
}
// bg.ShowGridLines = true;
create();
}
private void create()
{
map = new int[size, size];
maplist = new Image[size, size];
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j= 0; j < map.GetLength(1); j++)
{
maplist[i, j] = new Image();
maplist[i, j].Source = new BitmapImage(new Uri("img/tall.png", UriKind.Relative));
maplist[i, j].Width = 40;
maplist[i, j].Height = 40;
maplist[i, j].Tag = new int[] { i,j};
maplist[i, j].MouseDown += MainWindow_MouseDown;
Grid.SetRow(maplist[i, j], i);
Grid.SetColumn(maplist[i, j], j);
bg.Children.Add(maplist[i, j]);
}
}
}
private void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
Image img = sender as Image;
index = img.Tag as int[];
if (Name != null)
{
maplist[index[0], index[1]].Source = new BitmapImage(new Uri( "img/"+Name, UriKind.Relative));
switch (Name)
{
case "公主.png":
map[index[0], index[1]] = 1;
break;
case "dest.png":
map[index[0], index[1]] = 5;
break;
case "墙.png":
map[index[0], index[1]] = 2;
break;
case "box.png":
map[index[0], index[1]] = 3;
break;
case "BoD.png":
map[index[0], index[1]] = 4;
break;
case "tall.png":
map[index[0], index[1]] = 0;
break;
}
}
}
string Name;
private void wall_MouseDown(object sender, MouseButtonEventArgs e)
{
Image img = sender as Image;
switch (img.Name)
{
case "墙": //2
Name = "墙.png";
break;
case "路": //0
Name = "tall.png";
break;
case "箱子": //3
Name = "box.png";
break;
case "目标"://5
Name = "dest.png";
break;
case "完成后"://4
Name = "BoD.png";
break;
case "人"://1
Name = "公主.png";
break;
}
}
string mpath = "../../path/10.txt";
private void btn_Click(object sender, RoutedEventArgs e)
{
StreamWriter write = new StreamWriter(mpath,false, Encoding.Default);
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
if (j < 9)
{
write.Write(map[i, j] + ",");
}
else
{
write.Write(map[i, j]);
}
}
write.WriteLine();
}
write.WriteLine();
write.Flush();
write.Close();
MessageBox.Show("写入成功");
bg.RowDefinitions.Clear();
bg.ColumnDefinitions.Clear();
bg.Children.Clear();
this.Close();
}
}
}
运行结果:
来源:CSDN
作者:IT-wanghanghang
链接:https://blog.csdn.net/chonbi/article/details/104171272