C # getting started with zero infrastructure 03: getting started with a mouse,
I. Open VS in the source code manager.
Note that the following method is to open the solution on the TFS source code server. If SVN is used, open the solution directly on the hard disk.
Open:
Then, double-click steps 1 and 2 in step 2 to open our solution, as shown below:
Note: double-click the red box to display Solution Explorer.
In the previous section, we just created a solution for hitting the mouse, and did not write any code at all. Run the solution at this time (Remember, Debug-> Start without debug, or Ctrl + F5 in the menu. I like to use shortcut keys, and you must quickly familiarize yourself with these shortcut keys, which can save time ).
After running, it is a blank Windows form, as shown below:
Ii. Import resource files
Now we have to play the mouse in this form.
The final game of playing a mouse is as follows:
To complete this game, two image resources are required (right-click to download it ):
Bg.png
Mouse.png
Please save the two images to the root directory of our project. Please check the next video for image resource import and background settings:
Finally, the entire VS image we see should be like this:
Iii. Simple Functions
Next, let's talk about the whole coding idea of the mouse game. We can see 11 rat holes in the background:
1: first, cover 11 rat holes with the mouse image;
2: Set the image of all mice to invisible;
3: Set a mouse image for the program at random every 1 second;
Iv. Video
Non-public, please contact the most course (www.zuikc.com)
V. Code explanation
Now let's see what the code in the video means:
Private List <PictureBox> _ mouses = new List <PictureBox> ();
Private Timer _ timer = new Timer ();
Private Random _ random = new Random ();
Private PictureBox _ oldMouse;
Private int _ score = 0;
Here, we define some variables, including:
_ Mouses is used to store all mouse images;
_ Timer is a timer;
_ Random represents a random number;
_ OldMouse the mouse that is currently present here;
_ Score is used to record scores, which has not yet been used;
Let's look at the following code:
Foreach (Control item in this. Controls)
{
If (item is PictureBox)
{
_ Mouses. Add (item as PictureBox );
}
}
_ Timer. Interval = 1000;
_ Timer. Tick + = _ timer_Tick;
_ Timer. Start ();
Foreach indicates that this is a loop. We need to add all the mice to the _ mouses variable, because the variable is empty at first and there are no mice;
_ Timer. Interval = 1000; indicates that the timer time is set to 1 second;
_ Timer. Tick + = _ timer_Tick; indicates that a function is triggered when the timer time is reached. This function is called _ timer_Tick;
_ Timer. Start (); indicates that the timer starts timing;
Let's look at the timer-triggered function:
Void _ timer_Tick (object sender, EventArgs e)
{
If (_ oldMouse! = Null)
{
_ OldMouse. Visible = false;
}
Int index = _ random. Next (1, 11 );
_ OldMouse = _ mouses [index];
_ OldMouse. Visible = true;
}
The logic in this function is that if a mouse is currently displayed, it should be hidden first to make another mouse appear;
When running the program, we will find that a mouse appears randomly every one second;
Now, we continue to improve the Code to achieve: When the mouse image appears, when the mouse hits the mouse, the score is + 1 and displayed;
In fact, you only need to modify the code in the preceding foreach loop:
Foreach (Control item in this. Controls)
{
If (item is PictureBox)
{
_ Mouses. Add (item as PictureBox );
Item. MouseDoubleClick + = item_MouseDoubleClick;
}
}
That is, to add a double-click event for a mouse, the event method is:
Void item_MouseDoubleClick (object sender, MouseEventArgs e)
{
_ Score ++;
_ LabelScore. Text = _ score. ToString ();
}
This completes "scoring + 1 when the mouse hits the mouse, and it is displayed." We can see that the variable _ labelScore has not been seen above. How does it appear, watch videos.
6. Mouse hitting knowledge point so far
So far, although the mouse-hitting game we have implemented is simple, it already contains a lot of grammar knowledge, the following 11 knowledge points:
Next, we need to learn this knowledge one by one. (Note: In the detailed description below this section, I will only talk about the knowledge points that must be known at present. As the course goes deeper, I will gradually supplement the in-depth explanation of each knowledge point .)
1 and 2: The referenced namespace and namespace that defines the current class
Classify a class as a namespace.
What is the relationship between namespaces and folders? It does not matter! Of course, a common practice is to use a folder in a namespace.
If we want to reference other classes in the current class, it is convenient to introduce the namespace of other classes with using, such as 1;
In turn, we are writing the mouse-hitting class now, and we need to define a namespace for the current class, such as 2;
3 and 4: classes and Instances
In the world of C #, code is organized by class. We can see that:
Public partial class Form1: Form
Form1 is a class. The simplest form of a class is as follows:
Class Mouse
{
}
For the Form1 class declaration in the mouse games, we can ignore the partial and the content after the colon.
What is the relationship between classes and files? It does not matter! Of course, a common practice is to use a file in a class.
When the new keyword is used for a class, an instance of the class is generated, for example:
Mouse aMouse = new Mouse ();
5, 10, and 11. Variables
No static variable is modified outside of the class and is an instance variable;
Variables within the method are called local variables;
A variable is called a method parameter in the declaration of a method;
A variable (or a variable name) represents an object you want to operate on.
6 and 9. Methods
The method defines the behavior of the class, that is, what the class can do.
A special method is called constructor. It is called first when a class instance is generated.
7. Loop Structure
Foreach, for, while, all belong to the loop structure. If you look at the code, you should be able to understand the meaning accurately.
8. Condition Structure
If is a conditional structure, which is very simple and will not be repeated.
Switch and ternary operators are also in the condition structure. We will explain them when using them. We will not go into detail here.
Scan, follow the most courses, and obtain daily exercises