Tetris source code parsing (with download) [2]

Source: Internet
Author: User

Before talking about the source code, let's talk about the "basic principle" of this Russian square.

1 first, the square is a combination of four grids, right?

There are two situations where two blocks are stuck. One is the actual condition, and the other is that the blocks stacked with other blocks generate "boundary contact ". Right

The third block will automatically fall, right? If it gets stuck, a new block will appear on the top.

4. It can be rotated during the fall process (if space permits)

5. If there is "one line of Connection" in the stacked blocks, the line disappears and moves down the line above.

6 if it gets stuck in the air, it meansCodeProblematic machine hardware or character, right?

 

What should we use to achieve this?ProgramThe logic is tight, and I haven't seen a piece of code yet.

First, we should understand the game zone as a matrix of 15x10. This can be expressed using a two-dimensional array.

Blocks are interpreted as pixels in The Matrix. In the process of dropping, we only need to "erase pixels" and "Draw pixels ".

The write pixel method is to rewrite the value of the corresponding two-dimensional array to true or false.

We only need to implement the game logic on this basis, even if there is no front-end display, the game logic is still running.

Some shape code:

 

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace Tetris {// the shape of the square. Four Kinds of triangle strips and square corners appear randomly (if you do not want to comment out the corresponding one and change the random parameter) public class shape {public bool [,] transform; // The square public shape () // The initialization operation returns either of the four shapes {transform = new bool [gamearea. height, gamearea. height]; gamearea. initarea (ref Transform); int I = new random (). next (12); Switch (I) {Case 0: // staggered {transform [0, 4] = true; transform [0, 5] = true; transform [1, 4] = true; transform [1, 3] = true; break;} Case 1: // square {transform [0, 4] = true; transform [0, 5] = true; transform [1, 4] = true; transform [1, 5] = true; break ;}}}}}

 

 

The gamearea class initializes the canvas (game area) and stores stacked blocks.

Some code of gamearea:

 

Using system; using system. collections. generic; using system. LINQ; using system. text; namespace Tetris {// The Game region rectangle is used to store the square public class gamearea {// The container width defaults to 10 grids. If you want to change the gameplay rules, adjust this parameter. // don't forget. adjust Panel1 in form1 to the width; public const int width = 10; Public const int Height = 15; // container height public bool [,] gameareaarray; Public gamearea () // initialize the game area {gameareaarray = new bool [height, width]; for (INT I = 0; I  

 

 

What will happen later?

See Chapter 3

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.