以下為代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace GameTemp
{
public partial class Form1 : Form
{
#region 變量定義
/// <summary>
/// 圖片中錯誤處的坐標數組,local[圖片組數][錯誤位置數*2]
/// </summary>
private Point[][] local;
/// <summary>
/// 找到的錯誤
/// </summary>
private bool[] check;
/// <summary>
/// 已經通關的關數
/// </summary>
private int index;
/// <summary>
/// 關數
/// </summary>
private const int COUNT = 2;
/// <summary>
/// 秒數
/// </summary>
private int second;
#endregion
public Form1()
{
InitializeComponent();
}
#region Form1_Load 初始化局部變量
/// <summary>
/// 初始化局部變量
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
check = new bool[COUNT];
local = new Point[COUNT][];
local[0] = new Point[4];
local[1] = new Point[4];
//初始化坐標
local[0][0] = new Point(245, 50);
local[0][1] = new Point(253, 61);
local[0][2] = new Point(263, 520);
local[0][3] = new Point(287, 546);
local[1][0] = new Point(190, 237);
local[1][1] = new Point(203, 249);
local[1][2] = new Point(121, 490);
local[1][3] = new Point(137, 497);
}
#endregion
#region button1_Click 開始按鈕單擊事件
/// <summary>
/// 開始按鈕單擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
index = 0;
//初始化圖片
this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
this.pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
loadPicture();
this.button1.Enabled = false;
this.timer1.Start();
}
#endregion
#region loadPicture 加載圖片
/// <summary>
/// 加載圖片
/// </summary>
private void loadPicture()
{
switch (index)
{
case 0:
{
this.pictureBox1.ImageLocation = @"boy1A.jpg";
this.pictureBox2.ImageLocation = @"boy1B.jpg"; break;
}
case 1:
{
this.pictureBox1.ImageLocation = @"boyA.jpg";
this.pictureBox2.ImageLocation = @"boyB.jpg"; break;
}
}
}
#endregion
#region pictureBox1_Click 圖片框單擊事件
/// <summary>
/// 圖片框單擊事件,如果點擊的位置的坐標在錯誤坐標范圍內,則重繪圖片框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictureBox1_Click(object sender, EventArgs e)
{
//取得當前鼠標單擊位置的坐標
Point currentLocation = ((System.Windows.Forms.MouseEventArgs)(e)).Location;
//進行坐標比較
if (locationCompare(currentLocation, local[index][0], local[index][1]))
{
check[0] = true;
this.pictureBox1.Refresh();
}
if (locationCompare(currentLocation, local[index][2], local[index][3]))
{
check[1] = true;
this.pictureBox1.Refresh();
}
//如果已經達到最大關數
if (index == COUNT)
{
this.timer1.Stop();
this.second = 0;
MessageBox.Show("恭喜你,已經通關!");
this.button1.Enabled = true;
}
}
#endregion
#region locationCompare 坐標比較
/// <summary>
/// 坐標比較,如果current的坐標在first和end的坐標范圍內則返回true
/// </summary>
/// <param name="current">當前坐標</param>
/// <param name="first">左上角坐標</param>
/// <param name="end">右下腳坐標</param>
/// <returns>true:current的坐標在first和end的坐標范圍內;false:current的坐標在first和end的坐標范圍外</returns>
private bool locationCompare(Point current, Point first, Point end)
{
bool result = false;
//只有當前坐標在坐標范圍內才有效,所以逐個比較兩個point的x和y的值
if (current.X >= first.X)
{
if (current.Y >= first.Y)
{
if (current.X <= end.X)
{
if (current.Y <= end.Y)
{
result = true;
}
}
}
}
return result;
}
#endregion
#region pictureBox1_Paint 圖片框重繪事件
/// <summary>
/// 圖片框重繪事件,如果找到錯誤位置,用紅色線畫框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics.FromHwnd(this.pictureBox1.Handle);
Pen pen = new Pen(Color.Red, 3);
if (check[0])
{
e.Graphics.DrawRectangle(pen, local[index][0].X, local[index][0].Y, local[index][1].X - local[index][0].X, local[index][1].Y - local[index][0].Y);
}
if (check[1])
{
e.Graphics.DrawRectangle(pen, local[index][2].X, local[index][2].Y, local[index][3].X - local[index][2].X, local[index][3].Y - local[index][2].Y);
}
if (check[0] && check[1])
{
Thread.Sleep(100);
index++;
check[0] = false;
check[1] = false;
loadPicture();
}
}
#endregion
#region timer1_Tick 數秒事件
/// <summary>
/// 數秒事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
this.lblTime.Text = string.Empty;
second++;
this.lblTime.Text = second.ToString() + " 秒";
}
#endregion
}
}