How to Implement the Tetris game using javascript

Source: Internet
Author: User

How to Implement the Tetris game using javascript

How to Implement the Tetris game using javascript

As for Tetris, there are many difficulties. If JavaScript code is used to write data, we need to consider collision, boundary, whereabouts, and other issues. Most of these problems have been taken into account in this article, here we will provide you with some ideas and suggestions. If you need them, you can refer to them.

Observe the beauty of programming: "Although the program is difficult to write, it is wonderful. To write a program well, you need to write basic knowledge, including programming languages, data structures, and algorithms. The program is well written and requires meticulous logic thinking and a good foundation for organizing the program, and is familiar with the programming environment and programming tools ."

After years of computer learning, do you fall in love with programming. In other words, I did not try to write a game on my own, so I do not love programming.

The sensation and economic value caused by Tetris can be said to be a major event in the game history. It seems simple but changeable and addictive. I believe that most of the students have been obsessed with it.

Game Rules

1. A plane Virtual Site for placing a small square. Its standard size is 10 rows in width and 20 columns in height, in the unit of each small square.

2. A group of Rule images composed of four small squares. The group is called Tetromino in English, and there are 7 types of squares in Chinese, they are named in the shape of S, Z, L, J, I, O, and T.

I: a maximum of four layers can be eliminated at a time.

J (left and right): eliminates a maximum of three layers, or eliminates two layers.

L: three layers can be eliminated at most, or two layers can be eliminated.

O: Eliminate Layer 1 to Layer 2

S (left and right): a maximum of two layers, which may easily cause holes

Z (left and right): a maximum of two layers, which may cause holes

T: a maximum of two layers

The square will slowly continue falling from the top of the area. Players can rotate the square in 90 degrees, move the square around in the unit of a grid, so that the square is accelerated. When the block is moved to the bottom of the area or the block cannot be moved to other blocks, it is fixed here, and the new block appears above the area and begins to fall. When a column in the area is filled with blocks, the column disappears and becomes the score of the player. The more columns are deleted, the higher the score index.

Analysis and Solution

When each block falls, we can:

1) rotate in the proper direction

2) horizontally move to a column

3) vertical drop to the bottom

First, you need to use a two-dimensional array, area [18] [10] represents the 18*10 game area. The value 0 in the array indicates null, and 1 indicates square.

There are 7 types of blocks, each of which has four directions. Define activeBlock [4]. The value of this array is predefined before compilation and used directly in the program.

Difficulties

1) border check.

?

1

2

3

4

5

6

7

8

9

10

11

12

// Check the left boundary and try to move one to the left to see if it is legal.

Function checkLeftBorder (){

For (var I = 0; I <activeBlock. length; I ++ ){

If (activeBlock [I]. y = 0 ){

Return false;

}

If (! IsCellValid (activeBlock [I]. x, activeBlock [I]. Y-1 )){

Return false;

}

}

Return true;

} // Similarly, the right boundary and bottom boundary must be detected.

2) The rotation requires mathematical logic. A point is rotated 90 degrees relative to another point.

3) Timing and listening to Keyboard Events allow the game to run automatically.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

// Start

Function begin (e ){

E. disabled = true;

Status = 1;

Tbl = document. getElementById ("area ");

If (! GenerateBlock ()){

Alert ("Game over! ");

Status = 2;

Return;

}

Paint ();

Timer = setInterval (moveDown, 1000 );

}

Document. onkeydown = keyControl;

Procedure

1) Click Start> to create an active image and set the timer.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

// The square of the current activity. It can be moved left and right down and changed. When the base is reached, the area is updated;

Var activeBlock;

// Produce the square shape, which has seven basic shapes.

Function generateBlock (){

ActiveBlock = null;

ActiveBlock = new Array (4 );

// Randomly generate an array ranging from 0 to 6, representing 7 forms.

Var t = (Math. floor (Math. random () * 20) + 1) % 7;

Switch (t ){

Case 0 :{

ActiveBlock [0] = {x: 0, y: 4 };

ActiveBlock [1] = {x: 1, y: 4 };

ActiveBlock [2] = {x: 0, y: 5 };

ActiveBlock [3] = {x: 1, y: 5 };

 

Break;

}

// Omit Part of the code ..............................

Case 6 :{

ActiveBlock [0] = {x: 0, y: 5 };

ActiveBlock [1] = {x: 1, y: 4 };

ActiveBlock [2] = {x: 1, y: 5 };

ActiveBlock [3] = {x: 1, y: 6 };

Break;

}

}

// Check whether the four small squares that have just been produced can be placed in the initialization position.

For (var I = 0; I <4; I ++ ){

If (! IsCellValid (activeBlock [I]. x, activeBlock [I]. y )){

Return false;

}

}

Return true;

}

2) After each downward movement, check whether the bottom is reached. If the bottom is reached, try to cancel the operation.

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

// Cancel

Function deleteLine (){

Var lines = 0;

For (var I = 0; I <18; I ++ ){

Var j = 0;

For (; j <10; j ++ ){

If (area [I] [j] = 0 ){

Break;

}

}

If (j = 10 ){

Lines ++;

If (I! = 0 ){

For (var k = I-1; k> = 0; k --){

Area [k + 1] = area [k];

}

}

Area [0] = generateBlankLine ();

}

}

Return lines;

}

3) create an activity chart and set the timer.

  

To be optimized

1) set the color of blocks of different shapes.

Idea: In the create block function, set activeBlockColor. The colors of the seven different forms of blocks vary. (In addition to modifying the generateBlock method, you also need to modify the paintarea method. Due to incomplete consideration at the beginning, after a row is eliminated, the color of the re-painted square is unified. Therefore, you can consider removing n rows in the table and then adding n rows at the top, to ensure the integrity of blocks is not eliminated ).

2) When the current square falls, you can view the next square in advance.

Idea: Split the generateBlock method into two parts. One part is used to randomly try the next square, and the other part is used to cache the square currently to be depicted. When the current square is fixed at the bottom, the next square begins to be depicted, and a new square is randomly generated again. This is repeated.

The above is all the content shared in this article. I hope you will like it.

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.