namespace ConwayGameofLife
{
public partial class GameController : Window
{
public int row;
public int col;
public SolidColorBrush brush;
public Rectangle[,] rectangle;
public GridCell cell;
public string[,] cellPosition;
public int cellColumn;
public int cellRow;
public GameController()
{
InitializeComponent();
brush = new SolidColorBrush(Colors.Gray);
}
private void GridSlider_Button(object sender, RoutedPropertyChangedEventArgs<double> e)
{
row = (int)gridSlider.Value;
col = (int)gridSlider.Value;
resizeGrid.DataContext = gridSlider;
}
private void BuildGrid_Button(object sender, RoutedEventArgs e)
{
gameUniformGrid.Children.Clear();
gameUniformGrid.Rows = row;
gameUniformGrid.Columns = col;
rectangle = new Rectangle[row, col];
for (int x = 0; x < row; x++)
{
for (int y = 0; y < col; y++)
{
rectangle[y, x] = new Rectangle { Stroke = Brushes.Black, Fill = brush };
gameUniformGrid.Children.Add(rectangle[y, x]);
}
}
//for (int i = 0; i < rectangle.Length; i++)
//{
// rectangle[i] = new Rectangle { Stroke = Brushes.Black, Fill = brush };
// gameUniformGrid.Children.Add(rectangle[i]);
//}
}
在ToggleGrid功能,我想cellColumn和cellRow变量设置为当点击矩形是一个统一的网格列和行的价值,并能够使用这些变量的指数值来改变矩形的颜色。 我做的方法,我可以去这一点,并不能找到任何有用的一些研究。
private void ToggleGrid(object sender, MouseButtonEventArgs e) //changes color
{
if (e.OriginalSource is Shape s)
{
cellColumn = (int)GetBoundingBox(s, gameUniformGrid).X;
cellRow = (int)GetBoundingBox(s, gameUniformGrid).Y;
rectangle[cellColumn, cellRow].Fill = new SolidColorBrush(Colors.Green);
//s.Fill = new SolidColorBrush(Colors.Green);
//cellPosition[y, x] = "Dead";
}
MessageBox.Show($"Column Clicked: {cellColumn}, Row Clicked: {cellRow}");
}
private static Rect GetBoundingBox(FrameworkElement element, FrameworkElement parent)
{
GeneralTransform transform = element.TransformToAncestor(parent);
Point topLeft = transform.Transform(new Point(0, 0));
Point bottomRight = transform.Transform(new Point(element.ActualWidth, element.ActualHeight));
return new Rect(topLeft, bottomRight);
}
}