C # Implementation of the fault game

Source: Internet
Author: User
Tags bool comparison

This afternoon wrote a fault-finding games, there is no complete idea, but first to achieve, so not very perfect, but the main function has been. In fact, the main process is to first determine the coordinates of the differences in the two pictures, then the two pictures are displayed on the form, after the game, the mouse clicks on the picture to judge, if the current mouse coordinates in the wrong coordinates within the scope of the range will display a red box, when all errors are found to jump to the next pass.

The following is code:

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 Variable Definition


///<summary>


///An array of coordinates at the error in the picture, local[Picture Group [number of false positions *2]


///</summary>


private point[][] local;


///<summary>


///found the error


///</summary>


private bool[] check;


///<summary>


The number of customs clearance has been


///</summary>


private int index;


///<summary>


///Closing Number


///</summary>


Private Const int COUNT = 2;


///<summary>


///number of seconds


///</summary>


private int second;


#endregion


public Form1 ()


{


InitializeComponent ();


}


#region Form1_Load Initialize local variables


///<summary>


///initializes local variables


///</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];


//initialization coordinates


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 Start button click event


///<summary>


///Start button click event


///</summary>


///<param name= "Sender" ></param>


///<param name= "E" ></param>


private void Button1_Click (object sender, EventArgs e)


{


index = 0;


//Initialization picture


This.pictureBox1.SizeMode = pictureboxsizemode.zoom;


This.pictureBox2.SizeMode = Pictureboxsizemode.zoom;


LoadPicture ();


this.button1.Enabled = false;


This.timer1.Start ();


}


#endregion


#region loadpicture Loading picture


///<summary>


///Load Picture


///</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 Picture box Click event


///<summary>


///Picture Box Click event, and redraw picture frame if the coordinates of the location you clicked are in the wrong coordinate range


///</summary>


///<param name= "Sender" ></param>


///<param name= "E" ></param>


private void PictureBox1_Click (object sender, EventArgs e)


{


//Get the coordinates of the current mouse click Position


Point currentlocation = ((System.Windows.Forms.MouseEventArgs) (e)). Location;


//Coordinate comparison


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 you have reached the maximum number of closing


if (index = = COUNT)


{


This.timer1.Stop ();


This.second = 0;


MessageBox.Show ("Congratulations, you've cleared the border!") ");


this.button1.Enabled = true;


}


}


#endregion


#region Locationcompare coordinates comparison


///<summary>


///coordinates, returns true if the coordinates of current are within the coordinate range of the top and end


///</summary>


///<param name= "current" > coordinates </param>


///<param name= "I" > upper left corner coordinates </param>


///<param name= "End" > right leg coordinates </param>

The coordinates of the
///<returns>true:current are within the coordinate range of the A and, false:current coordinates are outside the coordinates of the range </returns>


private bool Locationcompare (point on, Point-A, point-end)


{


bool result = FALSE;


///Only the current coordinates are valid within the coordinate range, so compare the values of x and y of the two point by each other


if (current. X >=. X)


{


if (current. Y >=. Y)


{


if (current. X <= end. X)


{


if (current. Y <= end. Y)


{


result = true;


}


}


}


}


return result;


}


#endregion


#region Picturebox1_paint Picture frame Redraw event


///<summary>


///the picture box to redraw the event, and if you find the wrong location, use the red line to draw the frame


///</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 A few SEC events


///<summary>


///A few SEC events


///</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 () + "seconds";


}


#endregion


}


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.