Tetris development log

Source: Internet
Author: User

 

The official transfer to Tetris was developed yesterday. Previously, it was learning MFC and lasted about three weeks.
It took a lot of time on the drawing. I used to think about dividing multiple views on the main window, moving the blocks, the next squares, and the scores. The button information is divided into various views, it took a day to study the split window and found that there was no need to do so. Finally, I chose to display all the content above in a Single Window. Then we encountered a flickering problem, because the invalidate () function will invalidate the entire window and re-paint it. The solution is to use invalidaterect (& rect) this method only updates a region and does not need to be re-painted. My program Class division is:
1. elementsquare class, the minimum unit of the square, stores the bitmap information of the square, provides operations such as painting, wiping the previous position, moving to (bottom, left and right.
2. Shape class. This class is an abstract class. The block deformation function is a saved virtual function and is left in its derived class (Specific block shape, abstract Some common operations, such as moving to the falling, left and right. The three functions must determine whether to move or not based on the array of dropped blocks.
3, shape0 ~ Shape5 inherits from the shape class and covers the base class deformation function. This class sets the initial coordinates of each square to provide different shapes. The base class inherited by public, private, and protected is equivalent to the public, private, and protected members of this class. Initialize the coordinates of each unit in this class.
For the function of deformation, I tried it for a long time and finally found a problem that can solve the problem of residual images in the deformation process. My approach is deformation (changing coordinates) previously, the function erasecurrent, which calls elementsquare, is invalid for the current position. In this way, after the coordinates are changed and the erase method update window of the Shape class is executed, the residual image of the previous position is gone, then the functions in the process of dropping and moving left and right should also be able to do so. After modifying the code, I found that the process of moving left and right and dropping can be implemented in the same way, which makes it unnecessary to maintain prex and prey in the elementsquare class.
4. Execute class. This class is responsible for the overall scheduling and provides some methods for the View class to use, so that only a few external methods can be called to complete all operations on the square.
5. For a two-font image, there are only two States.
After discussion with Alibaba, it is found that when the block is deformed, it only calculates whether the next image location is occupied. If it is occupied, it cannot be deformed. Otherwise, the deformation code can be modified again.
Simple Test
After writing a graphic, I did a test and encountered several problems, but they all solved them one by one, one of the most exciting ones is that I used the invalidaterect (& rect) function to mark the four small rectangle areas I drew in the previous step as invalid, and then directly drew the shape of the next action, this leads to a problem: the shape shown in the next step is always less overlapped with the previous step. After checking the information on the Internet, we found that the invalidaterect function sends a low-priority wm_paint message, if there are other actions, it will take a while to refresh them. This will cause the first trace to be refreshed after I have finished drawing the second part. The solution is to call the updatewindow () function after updating each small square. This function is used by the operating system to immediately respond to the wm_paint message and refresh the window immediately.

Circular reference of header files
When two header files contain each other, a circular reference error occurs, indicating that a header file cannot find the type definition in another header file, the solution is to declare another unfound type in the header file.
Cancel and redraw
After the code is modified, I add a pointer flag to each line. After a round of scanning, I will determine which line can be eliminated and then delete it, rearranging. Then we re-draw the image. At first, we were a little stupid to redraw the image in the container class. Then we suddenly remembered that we should re-paint the image in the ondraw function, therefore, you can add the re-painting function call to the ondraw function to solve the problem of non-method re-painting. The object of the current small square is always saved in the container array.
Space drop
Capture the char (32) Space event in onkeydown and call the falldown function of shape to know that the function returns false.
Show the next graph: In the execute class, I created a copy of the shape to save the next graph. For this reason, I modified the shape code and added a method to modify the coordinate of the graph, in this way, I can set the position of the image to be displayed in this copy, and then set the display area of the current image to invalid and delete the copy each time, then, when the next square falls for the first time, call the shape erase method to send the updatewindow message and re-draw the next graph to be displayed.

STL used in the MFC project
Use STL in the MFC project. The code containing the header file and namespace should be placed in
# Ifdef _ debug
# Define new debug_new
Otherwise, an error occurs. Why is it unclear?

 

: Http://download.csdn.net/source/1750299

 

As follows:

 

 

 

 

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.