/// <summary> /// move up /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnUp_Click(object sender, EventArgs e) { bool bttnUp = true; movesCounter++; labelShowMoves.Text = movesCounter.ToString(); if (tile.TileType == "box") //check the type { //make the movement until stopped by another tile do { TilePlay adj = pictureBoxMatrix[tile.Col, tile.Row - 1]; //check the seclected tile click if (tile.TileCategory == adj.TileCategory && adj.TileType == "door") {//remove the box bttnUp = false; TileConfigration(); //this.Controls.Remove(tile); boxCounter--; if (boxCounter == 0) { MessageBox.Show("Game Finished"); RemoveGrid(); } } if (adj.TileCategory == "none") { adj.Image = tile.Image; adj.TileCategory = tile.TileCategory; adj.TileType = tile.TileType; pictureBoxMatrix[tile.Col, tile.Row].Image = images[0].Image; pictureBoxMatrix[tile.Col, tile.Row].TileCategory = images[0].Category; pictureBoxMatrix[tile.Col, tile.Row].TileType = images[0].TileType; tile = adj; } else { bttnUp = false; } } while (bttnUp); } else { MessageBox.Show("Select box first"); } }
/// <summary> /// Move Dawn /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDown_Click(object sender, EventArgs e) { movesCounter++; labelShowMoves.Text = movesCounter.ToString(); bool btnDwn = true; if (tile.TileType == "box") { do { TilePlay adj = pictureBoxMatrix[tile.Col, tile.Row + 1]; if (tile.TileCategory == adj.TileCategory && adj.TileType == "door") { btnDwn = false; TileConfigration(); //this.Controls.Remove(tile); boxCounter--; if (boxCounter == 0) { MessageBox.Show("Game Finished"); RemoveGrid(); } } if (adj.TileCategory == "none") { adj.Image = tile.Image; adj.TileCategory = tile.TileCategory; adj.TileType = tile.TileType; pictureBoxMatrix[tile.Col, tile.Row].Image = images[0].Image; pictureBoxMatrix[tile.Col, tile.Row].TileCategory = images[0].Category; pictureBoxMatrix[tile.Col, tile.Row].TileType = images[0].TileType; tile = adj; } else { btnDwn = false; } } while (btnDwn); } else { MessageBox.Show("select box first"); } }
/// <summary> /// function to create the level load the images /// </summary> public void GetImage() { //declaring local variables x = 50; y = 50; VGAP = 100; //the dictionary of images //assign type &catogery to the tile images[0] = new TileConfig() { Image = null , TileType = "none" , Category = "none" }; images[1] = new TileConfig() { Image = Properties.Resources.wall2 , TileType = "wall" , Category = "wall" }; images[2] = new TileConfig() { Image = Properties.Resources.doorBlue , TileType = "door" , Category = "blue" }; images[3] = new TileConfig() { Image = Properties.Resources.doorYellow , TileType = "door" , Category = "yellow" }; images[4] = new TileConfig() { Image = Properties.Resources.doorRed , TileType = "door" , Category = "red" }; images[5] = new TileConfig() { Image = Properties.Resources.doorGreen , TileType = "door" , Category = "green" }; images[6] = new TileConfig() { Image = Properties.Resources.boxesGreen , TileType = "box" , Category = "green" }; images[7] = new TileConfig() { Image = Properties.Resources.boxesYellow , TileType = "box" , Category = "yellow" }; images[8] = new TileConfig() { Image = Properties.Resources.boxesBlue , TileType = "box" , Category = "blue" }; images[9] = new TileConfig() { Image = Properties.Resources.boxesRed , TileType = "box" , Category = "red" }; try {//create the grid load the image pictureBoxMatrix = new TilePlay[colNumber, rowsNumber]; try { for (int i = 0; i < colNumber; i++) { for (int j = 0; j < rowsNumber; j++) { var config = images[int.Parse(lines[counter])]; TilePlay myTile = new TilePlay(); myTile.Location = new Point(i * x + VGAP, j * x + y); myTile.Image = config.Image; myTile.TileType = config.TileType; myTile.TileCategory = config.Category; myTile.Row = j; myTile.Col = i; myTile.Click += PictureBox_Click; //check the number of boxes if (myTile.TileType == "box") { boxCounter++; } pictureBoxMatrix[i, j] = myTile; this.Controls.Add(pictureBoxMatrix[i, j]); counter++; } } MessageBox.Show("The level created successfully"); //disable load menu loadGameToolStripMenuItem.Enabled = false; //enable the navigation buttons this.btnUp.Enabled = true; this.btnRight.Enabled = true; this.btnleft.Enabled = true; this.btnDown.Enabled = true; } catch (Exception ex) { MessageBox.Show("Error in creating the grid " + ex.Message); } } catch (FormatException ex) { //when input is not number MessageBox.Show("Invalid format: " + ex.Message); } catch (Exception ex) { MessageBox.Show("Invalid Entries " + ex.Message); } }
/// <summary> /// make the tile click /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void PictureBox_Click(object sender, EventArgs e) { tile = (TilePlay)sender; }