private void DrawSquare(int square_horizontal, int square_vertical) { Rectangle sub_picture_dimensions = new Rectangle(); switch (square_states[square_horizontal, square_vertical]) { case 0: sub_picture_dimensions = new Rectangle(0, 0, 128, 128); break; case 1: sub_picture_dimensions = number_images[square_values[square_horizontal, square_vertical]]; break; case 2: sub_picture_dimensions = new Rectangle(128, 0, 128, 128); break; } Bitmap temp = new Bitmap(this.canvas_width, this.canvas_height); using (Graphics g = Graphics.FromImage(temp)) { g.DrawImage(canvas, Point.Empty); g.DrawImage( Properties.Resources.minesweeper_tiles, new Rectangle(square_horizontal * square_width + board_offset_horizontal, square_vertical * square_height + board_offset_vertical, square_width, square_height), sub_picture_dimensions, GraphicsUnit.Pixel ); } canvas = temp; Canvas_Panel.Invalidate(); }
private void DrawGrid(int num_tiles_horizontal, int num_tiles_vertical) { this.canvas_width = board_offset_horizontal * 2 + square_width * num_tiles_horizontal; this.canvas_height = board_offset_vertical * 2 + square_height * num_tiles_vertical; this.ClientSize = new Size(canvas_width, canvas_height + menuStrip1.Height + toolStrip1.Height + statusStrip1.Height); Bitmap temp = new Bitmap(canvas_width, canvas_height); using (Graphics g = Graphics.FromImage(temp)) { for (int i = 0; i < num_tiles_horizontal; i++) { for (int j = 0; j < num_tiles_vertical; j++) { g.DrawImage( Properties.Resources.minesweeper_tiles, new Rectangle(i * square_width + board_offset_horizontal, j * square_height + board_offset_vertical, square_width, square_height), new Rectangle(0, 0, 128, 128), GraphicsUnit.Pixel ); } } } canvas = temp; Canvas_Panel.Invalidate(); }
private void DrawEntireBoard() { Bitmap temp = new Bitmap(this.canvas_width, this.canvas_height); using (Graphics g = Graphics.FromImage(temp)) { g.DrawImage(canvas, Point.Empty); for (int i = 0; i < number_squares_horizontal; i++) { for (int j = 0; j < number_squares_vertical; j++) { g.DrawImage( Properties.Resources.minesweeper_tiles, new Rectangle(i * square_width + board_offset_horizontal, j * square_height + board_offset_vertical, square_width, square_height), number_images[square_values[i, j]], GraphicsUnit.Pixel ); } } } canvas = temp; Canvas_Panel.Invalidate(); }
private void Canvas_Panel_MouseClick(object sender, MouseEventArgs e) { int click_x = e.X; int click_y = e.Y; int square_x, square_y; if (click_x > board_offset_horizontal && click_x < this.ClientSize.Width - board_offset_horizontal && click_y > board_offset_vertical && click_y < this.ClientSize.Height - board_offset_vertical) { // Translate mouse click co-ordinates into board co-ordinates click_x -= board_offset_horizontal; click_y -= board_offset_vertical; square_x = click_x / square_width; square_y = click_y / square_height; switch (e.Button) { case MouseButtons.Left: if (square_states[square_x, square_y] != 1) { square_states[square_x, square_y] = 1; DrawSquare(square_x, square_y); num_squares_left--; Update_Status_Bar(); if (square_values[square_x, square_y] == 0) { Bitmap temp = new Bitmap(canvas_width, canvas_height); using (Graphics g = Graphics.FromImage(temp)) { g.DrawImage(canvas, Point.Empty); showAdjacentSquares(square_x, square_y, g); } canvas = temp; Canvas_Panel.Invalidate(); } } break; case MouseButtons.Right: switch (square_states[square_x, square_y]) { case 0: square_states[square_x, square_y] = 2; DrawSquare(square_x, square_y); num_mines_left--; Update_Status_Bar(); break; case 2: square_states[square_x, square_y] = 0; DrawSquare(square_x, square_y); num_mines_left++; Update_Status_Bar(); break; } break; } if (square_states[square_x, square_y] == 1 && square_values[square_x, square_y] == 9) { Game_Loss(); } if (num_squares_left == num_mines) { Game_Win(); } } }