public void AddElement(GameElement e) { if (Elements == null) { Elements = new List <GameElement>(); } Elements.Add(e); }
public void DrawRectangleBoard(int x, int y, int zindex, Canvas parent) { int InitialPositionX = (int)(parent.ActualWidth / 2 - 0.5 * x * PICWIDTH); int InitialPositionY = (int)(parent.ActualHeight / 2 - 0.5 * y * PICHEIGHT); for (int ix = 0; ix < x; ix++) { for (int iy = 0; iy < y; iy++) { GameElement el = new GameElement(ix * PICWIDTH + InitialPositionX, iy * PICHEIGHT + InitialPositionY, 1, zindex, parent); el.GameButton.Click += new RoutedEventHandler(GameElementButton_Click); game.AddElement(el); } } }
public Game() { Elements = new List <GameElement>(); LastElement = null; Counter = 0; }
public void DrawBoard(int zindex, Canvas parent, int type) { const int x = 8; const int y = 7; int InitialPositionX = (int)(parent.ActualWidth / 2 - 0.5 * x * PICWIDTH); int InitialPositionY = (int)(parent.ActualHeight / 2 - 0.5 * y * PICHEIGHT); int[,] coordsHeart = new int[y, x] { { 0, 1, 1, 0, 0, 1, 1, 0 }, { 1, 0, 0, 1, 1, 0, 0, 1 }, { 1, 0, 0, 0, 0, 0, 0, 1 }, { 1, 0, 0, 1, 1, 0, 0, 1 }, { 0, 1, 0, 0, 0, 0, 1, 0 }, { 0, 0, 1, 0, 0, 1, 0, 0 }, { 0, 0, 0, 1, 1, 0, 0, 0 } }; int[,] coordsSnake = new int[y, x] { { 1, 1, 1, 1, 1, 1, 1, 1 }, { 0, 0, 0, 0, 0, 0, 0, 1 }, { 1, 1, 1, 1, 1, 1, 0, 1 }, { 1, 0, 0, 0, 0, 1, 0, 1 }, { 1, 0, 1, 1, 1, 1, 0, 1 }, { 1, 0, 0, 0, 0, 0, 0, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1 } }; int[,] coordsHouse = new int[y, x] { { 0, 0, 0, 0, 0, 1, 1, 1 }, { 0, 1, 0, 0, 0, 1, 1, 1 }, { 0, 1, 1, 1, 0, 1, 1, 1 }, { 1, 1, 1, 1, 1, 0, 1, 0 }, { 1, 0, 0, 0, 1, 0, 1, 0 }, { 1, 0, 1, 0, 1, 0, 1, 0 }, { 1, 1, 1, 1, 1, 0, 1, 0 } }; int[,] coords = new int[y, x]; if (type == 0) { coords = coordsHeart; } if (type == 1) { coords = coordsSnake; } if (type == 2) { coords = coordsHouse; } for (int ix = 0; ix < x; ix++) { for (int iy = 0; iy < y; iy++) { if (coords[iy, ix] == 1) { GameElement el = new GameElement(ix * PICWIDTH + InitialPositionX, iy * PICHEIGHT + InitialPositionY, 1, zindex, parent); el.GameButton.Click += new RoutedEventHandler(GameElementButton_Click); game.AddElement(el); } } } }
private void GameElementButton_Click(object sender, RoutedEventArgs e) { Button CurrentButton = sender as Button; foreach (GameElement el in game.Elements) // Check if button is enabled { if (Canvas.GetZIndex(CurrentButton) >= Canvas.GetZIndex(el.GameButton)) { continue; } if (Math.Abs(CurrentButton.Margin.Left - el.GameButton.Margin.Left) > CurrentButton.Width) { continue; } if (Math.Abs(CurrentButton.Margin.Top - el.GameButton.Margin.Top) < CurrentButton.Height) { return; } } var converter = new System.Windows.Media.BrushConverter(); if (game.LastElement != null) { GameElement CurrentElement = null; foreach (GameElement el in game.Elements) { if (sender as Button == el.GameButton) { CurrentElement = el; break; } } if (CurrentElement != null && CurrentElement != game.LastElement) { if (CurrentElement.Index == game.LastElement.Index) { CurrentElement.GameButton.Visibility = Visibility.Hidden; game.LastElement.GameButton.Visibility = Visibility.Hidden; game.Elements.Remove(CurrentElement); game.Elements.Remove(game.LastElement); CurrentElement.RightRectangle.Visibility = Visibility.Hidden; game.LastElement.RightRectangle.Visibility = Visibility.Hidden; CurrentElement.BottomRectangle.Visibility = Visibility.Hidden; game.LastElement.BottomRectangle.Visibility = Visibility.Hidden; } game.LastElement.GameButton.BorderBrush = (Brush)converter.ConvertFromString("#FF707070"); game.LastElement.GameButton.BorderThickness = new Thickness(1); game.LastElement = null; CurrentElement = null; if (game.Elements.Count == 0) { GameEndLabel.Content = "Congratulations!"; GameEndLabel.Visibility = Visibility.Visible; } } } else if (sender is Button) { foreach (GameElement el in game.Elements) { if (sender as Button == el.GameButton) { game.LastElement = el; game.LastElement.GameButton.BorderBrush = (Brush)converter.ConvertFromString("#FFDA35BE"); game.LastElement.GameButton.BorderThickness = new Thickness(3); break; } } } }