C# WPF中的随机迷宫发生器
编辑: 我试过了 及 但出现错误,提示“无法设置对象“#fffffffffff”的属性,因为它处于只读状态。”C# WPF中的随机迷宫发生器,c#,wpf,random,maze,generate,C#,Wpf,Random,Maze,Generate,编辑: 我试过了 及 但出现错误,提示“无法设置对象“#fffffffffff”的属性,因为它处于只读状态。” 我想用WPF做一个随机迷宫生成器。 到目前为止,我已经制作了一个开始页面和一个“开始”按钮,当你点击开始按钮时,它会引导你进入迷宫,我希望它在那里开始自我生成 我看了一些教程,关于如何设置它,以及它背后的整个理论 现在我有点困在一个问题里。我想让红方块移动到右边或下面的区域,然后给前面的区域涂上另一种颜色,这样红方块就是生成迷宫的细胞,前面的就是迷宫本身。 但我似乎根本无法获得更改
我想用WPF做一个随机迷宫生成器。 到目前为止,我已经制作了一个开始页面和一个“开始”按钮,当你点击开始按钮时,它会引导你进入迷宫,我希望它在那里开始自我生成 我看了一些教程,关于如何设置它,以及它背后的整个理论 现在我有点困在一个问题里。我想让红方块移动到右边或下面的区域,然后给前面的区域涂上另一种颜色,这样红方块就是生成迷宫的细胞,前面的就是迷宫本身。 但我似乎根本无法获得更改编程代码中红方块位置的许可,因为它是只读的。但我需要它,因为我想使用循环等等
<Grid>
<Rectangle Fill="Black" HorizontalAlignment="Left" Height="356" Margin="10,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="772"/>
<Button x:Name="Button1" Content="Start" HorizontalAlignment="Left" Margin="357,380,0,0" VerticalAlignment="Top" Width="74" Click="Button1_Click_1"/>
<TextBlock x:Name="Title1" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" Height="70" Width="654" Margin="70,71,68,0" Foreground="White" FontSize="60" FontFamily="Impact" FrameworkElement.FlowDirection="LeftToRight" TextAlignment="Center">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="Title1" Storyboard.TargetProperty="(FrameworkElement.Height)" To="0.0" Duration="00:00:02" DecelerationRatio="0" AutoReverse="True" BeginTime="00:00:02" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers><Run Text="Random Maze Generator">
<Run.Background>
<ImageBrush/>
</Run.Background>
</Run></TextBlock>
<TextBlock x:Name="Title2" HorizontalAlignment="Left" Margin="246,254,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="54" Width="298" Foreground="White" FontSize="30" Text="Press Start to continue"/>
<Grid Name="GridLines" ShowGridLines="False" Width="772" Height="356" Margin="10,10,10,53">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Name="Cell" Grid.Row="0" Grid.Column="0" Background="Red" Opacity="0" Visibility="Visible"/>
<StackPanel Name="Stack" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left"></StackPanel>
</Grid>
</Grid>
}
红方块位于Xaml中的Border.Grid.Row=“0”和Border.Grid.Column=“0”中,但我不知道如何访问它们。
很长一段时间以来,我一直认为我必须处理的是Grid.RowProperty或Grid.ColumnProperty,但这些都是只读的
我对编程相当陌生,对WPF也非常陌生,所以这可能是一个愚蠢的问题,不想触发任何人
谢谢您的时间。我认为您最好使用画布而不是网格。然后,通过设置canvas.left和canvas.top的动画,可以轻松地将红方块从任何位置平滑地设置为动画 您可以将控件覆盖在另一个控件的顶部,以便工件“打开”的画布可以位于itemscontrol的顶部。给它一个wrappanel作为itemspanel,它将把你模板化的内容从一行包装到另一行。 将cellVM的集合(每个单元格的viewmodel)绑定到itemscontrol的itemssource。然后使用一个itemtemplate将每个项目转化为迷宫的一个单元。这可以是一个简单的矩形,一种颜色表示封闭,另一种颜色表示开放。 可以为每个单元格的轮廓设置边框,也可以使用网格图像覆盖。
您也可以使用writeablebitmap为迷宫背景构建图像,但如果您的要求很简单,itemscontrol模板制作就相对容易了。基本上,您的问题是“如何在代码隐藏中设置附加属性?”。 附加属性通常是由赋予其某些角色的控件定义的属性,但通常在其他类型的控件上设置此属性。在本例中,网格使用列和行属性来布局子元素 设置此属性相当简单。您只需执行以下所有关联的setter函数:
Grid.SetColumn(Cell, 1);
然而,正如Andy提到的,使用网格并不是最好的选择。只是一个旁注:如果你想隐藏一个元素(比如隐藏文本块),你最好设置可见性属性,比如
Title1.Visibility=Visibility.Collapsed代码>还要注意,不透明度是0..1范围内的双倍值,不是百分比。是的,对此表示抱歉。我已经编辑视频和3d建模很长时间了,所以我有点假设了这个值,但它似乎仍然有效。而不是Border.Background.SetValue(Grid.RowProperty,1)
useCell.SetValue(Grid.RowProperty,1)
ou yes!!成功了!谢谢。我基本上只是做了这个帐户,所以我不知道我是否可以给你任何分数为你的答案。如果你知道怎么做,就告诉我。
<Grid>
<Rectangle Fill="Black" HorizontalAlignment="Left" Height="356" Margin="10,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="772"/>
<Button x:Name="Button1" Content="Start" HorizontalAlignment="Left" Margin="357,380,0,0" VerticalAlignment="Top" Width="74" Click="Button1_Click_1"/>
<TextBlock x:Name="Title1" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Top" Height="70" Width="654" Margin="70,71,68,0" Foreground="White" FontSize="60" FontFamily="Impact" FrameworkElement.FlowDirection="LeftToRight" TextAlignment="Center">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="FrameworkElement.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="Title1" Storyboard.TargetProperty="(FrameworkElement.Height)" To="0.0" Duration="00:00:02" DecelerationRatio="0" AutoReverse="True" BeginTime="00:00:02" RepeatBehavior="Forever" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers><Run Text="Random Maze Generator">
<Run.Background>
<ImageBrush/>
</Run.Background>
</Run></TextBlock>
<TextBlock x:Name="Title2" HorizontalAlignment="Left" Margin="246,254,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="54" Width="298" Foreground="White" FontSize="30" Text="Press Start to continue"/>
<Grid Name="GridLines" ShowGridLines="False" Width="772" Height="356" Margin="10,10,10,53">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border Name="Cell" Grid.Row="0" Grid.Column="0" Background="Red" Opacity="0" Visibility="Visible"/>
<StackPanel Name="Stack" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left"></StackPanel>
</Grid>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
double columns = 16;
int rows = 8;
double square_width;
double square_height;
double square_area;
int current_cell;
int visited_cell;
private void Button1_Click_1(object sender, RoutedEventArgs e) //After the Start button is pressed
{
Title1.Text = String.Empty; //Hide text for Title1
Title2.Text = String.Empty; //Hide text for Title2
GridLines.ShowGridLines = true; //Changes the value for GridLines from false to true
Cell.Opacity = 100; //Changes the cell opacity from 0% to 100%
}
}
Grid.SetColumn(Cell, 1);