C # getting started with zero infrastructure 04: enumeration, refactoring, and event processor for mouse hitting,

Source: Internet
Author: User

C # getting started with zero infrastructure 04: enumeration, refactoring, and event processor for mouse hitting,

1. Add "start", "pause", and "stop" to the interface"

After the interaction process in the previous course, our program increases user friendliness and records more detailed results. But we still find that there are many things to be improved (maybe you have found bugs? Don't worry .), First of all, it seems that the interface should not appear, and the game should start. We need to add the "Start", "pause", and "stop" buttons for the program. Moreover, the game is too single-functional, and we should add a level of difficulty. That's right. For the time being, it can be divided into "simple", "medium", and "difficult.

Now, we will add these features to view videos.

 

Ii. Video

Non-public part. Please contact the most course (www.zuikc.com)

 

Iii. increase difficulty levels for games

After completing the UI, we need to add the game logic for so many event processors. For more information about function implementation, see the video.

 

Iv. Video

Non-public part. Please contact the most course (www.zuikc.com)

 

V. Video Summary

1: First of all, we learned how to use the following shortcut keys. You must know that skilled use of the shortcut keys will increase your confidence in writing code:

Tab: Let VS help us quickly complete the Code;

Ctrl + C: not just copying, but copying without selecting any content;

Ctrl + D: by default, the entire row in the clipboard is pasted on the current top;

Ctrl + Shift + Arrow: menu word;

Alt + right-click header: Smart sensing;

Ctrl + K + D: automatically alignment the code;

2: replace if with switch

When there are too many if operations, you can try to replace if with a switch. Of course, if there are more than three if conditions, you should be careful. Maybe we should modify the code, because too many if conditions make the code so non-object-oriented. Don't worry, we will soon learn object-oriented.

3: Add a file

In this example, we learned how to add a file to the project;

4: if there are repeated code, you need to refine the method. This method to continuously improve the code quality has a very fashionable name: refactoring

In the next lesson, we will learn Object-Oriented Programming (OOP), and we will perform a lot of refactoring. We should look forward to OOP, because this will let us write code like writing poems.

5: Enumeration

Enumeration is an important concept at the level of class in C. We have already mentioned in our previous courses that classes have attributes and behaviors. For enumeration, there are only one group of meanings, for example, Monday to Sunday, you can define an enumeration like this:

Enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri };

In our game, we define the difficulty as an enumeration, as shown below:

Enum Level
{
Basic,
Middle,
High
}

6: Event Processor

What is an event processor is a method. If a method is assigned an event variable, this method is an event processor. In principle, this concept will be discussed in the next section, including the delegated processor. But if you need to understand the Button event now, we should know this concept. So what is assigning values to event variables? In fact, we have seen such code:

In our game, our current code is:

It is an event processor. The statement assigned to the Button's Click Event variable is automatically generated by VS in Form1.Designer. cs, as follows:

This. buttonStart. Click + = new System. EventHandler (this. buttonStart_Click );

In today's coding, we didn't write the above statement because VS is doing a good job and helped us write such a statement. However, if we have to go deeper, why should I Click it, it will automatically execute the code for starting the game? OK. Now let's take a look at Form1.Designer. cs.

Scan, follow the most courses (www.zuikc.com), get more articles from me, and get the daily practice of software development.

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.