Javascript Russian square game code explanation! (1)

Source: Internet
Author: User

Javascript Russian square game code explanation! (1)

The code of this Russian square is written in JavaScript, which is easy to understand;

The full code consists of static classes and static variable members;

The full script configures OLSFK. Options = {...} globally by implementing the code {...}

Defines the starting coordinate of the square and the respective rotation points;

Start from the initialization of the Tetris interface, then listen to the keyboard events; as well as left and right, downward and rotating action judgment, re-rendering the square location;

Determine whether or not to cancel the operation, as well as the corresponding addition level judgment, execution speed, and extra points for execution;

Finally, judge whether the current level is greater than the defined maximum level to determine whether the end is over;

Code Description

 
 
  1. OLSFK. Options = {// related parameters
  2. Width: 12, // number of horizontal squares on the Interface
  3. Height: 20, // number of vertical squares on the Interface
  4. BoxWidth: '16px ',
  5. CurLevel: 1,
  6. Speed: 1000, // setInterval, setTimeout
  7. Direct: {// you can set whether it is a s d w, or even running →
  8. Down: 40,/* run speed */
  9. Left: 37,
  10. Right: 39,
  11. Rotate: 38
  12. },
  13. Move: true, // whether the object is being moved
  14. Eventing: false,
  15. Levels :{
  16. 1:1000,
  17. 2: 900,
  18. 3: 800,
  19. 4: 700,
  20. Five: 600,
  21. 6: 500,
  22. 7: 400,
  23. 8: 300,
  24. 9: 200,
  25. },
  26. CurBlock: 4, // The Name Of The currently moved Image
  27. NextBlock: 0,
  28. GampMap: new Object (),
  29. Timer: null,
  30. Deline: 0,
  31. Score: 0,
  32. Deling: false,
  33. Start: false,
  34. LineNum: 10, // delete a few rows and add the level
  35. ScoreNum: 40 // delete one row plus points
  36. }

Direct indicates that the orientation key of the keyboard is used to operate the movement direction of the square;

Which direction keys do you like to use? For example, the keys A, S, D, W, or the numbers on the right keypad are encoded by their respective keyboards;

For example, the top (rotation), bottom, left, and right direction keys are encoded as 38, 40, 37, and 39;

Levels: indicates the level configuration. This configuration is divided into 10 Levels. The falling speed of each level is the scheduled execution interval;

CurBlock: the current active block;

NextBlock: The Block index to be executed next. The preview box in the upper-right corner of the page is displayed;

GampMap: used to store the data information of each cell in a game table based on the number of defined columns;

OLSFK.Options.GampMap[x+'_'+y] = 0;

The object table is: id: "box _" + x + "_" + y;

The initial data is '0', indicating that the table is not in use. When there is an occupation, set the value to '1 ';

Timer: scheduled actuator; setTimeout: the frequency at which the execution blocks fall; the smaller the timing time, the faster the speed;

Deling;

LineNum: indicates that more than 10 rows are eliminated, and a level is added;

ScoreNum: indicates the score added to each line;

 
 
  1. OLSFK. ReItems = function (cur) {// key rotation point
  2. Switch (cur)
  3. {
  4. Case 1:
  5. OLSFK. Items [1] = {// long LongBlock
  6. 1: {x: 4, y: 0 },
  7. 2: {x: 5, y: 0 },
  8. 3: {x: 6, y: 0 },
  9. 4: {x: 7, y: 0 },
  10. 5: {x: 5, y: 0} // rotation point
  11. };
  12. Break;
  13. //....
  14. }
  15. }

This method is used to restore the initial setting of a square;

 
 
  1. OLSFK. Next = {// key rotation point
  2. // Long LongBlock
  3. 1 :{
  4. 1: {x: 0, y: 1 },
  5. 2: {x: 1, y: 1 },
  6. 3: {x: 2, y: 1 },
  7. 4: {x: 3, y: 1}
  8. },
  9. //...
  10. }

Cannot conflict with the settings of the game square, and the object configuration of the next random square is independent;

 
 
  1. OLSFK. Items = {// key rotation point
  2. // Long LongBlock
  3. 1 :{
  4. 1: {x: 4, y: 0 },
  5. 2: {x: 5, y: 0 },
  6. 3: {x: 6, y: 0 },
  7. 4: {x: 7, y: 0 },
  8. 5: {x: 5, y: 0}
  9. },
  10. // Box
  11. 2 :{
  12. 1: {x: 4, y: 0 },
  13. 2: {x: 5, y: 0 },
  14. 3: {x: 4, y: 1 },
  15. 4: {x: 5, y: 1 },
  16. 5: {x: 0, y: 0}
  17. },
  18. // Convex block TuBlock
  19. 3 :{
  20. 1: {x: 4, y: 1 },
  21. 2: {x: 5, y: 0 },
  22. 3: {x: 5, y: 1 },
  23. 4: {x: 6, y: 1 },
  24. 5: {x: 5, y: 1}
  25. },
  26. // L Block LBlock
  27. 4 :{
  28. 1: {x: 5, y: 0 },
  29. 2: {x: 5, y: 1 },
  30. 3: {x: 5, y: 2 },
  31. 4: {x: 6, y: 2 },
  32. 5: {x: 5, y: 2}
  33. },
  34. 5: {// Reverse L Block FLBlock
  35. 1: {x: 5, y: 2 },
  36. 2: {x: 6, y: 2 },
  37. 3: {x: 6, y: 1 },
  38. 4: {x: 6, y: 0 },
  39. 5: {x: 6, y: 2}
  40. },
  41. // Z-block ZBlock
  42. 6 :{
  43. 1: {x: 4, y: 0 },
  44. 2: {x: 5, y: 0 },
  45. 3: {x: 5, y: 1 },
  46. 4: {x: 6, y: 1 },
  47. 5: {x: 5, y: 0}
  48. },
  49. 7: {// reverse Z block FZBlock
  50. 1: {x: 4, y: 1 },
  51. 2: {x: 5, y: 1 },
  52. 3: {x: 5, y: 0 },
  53. 4: {x: 6, y: 0 },
  54. 5: {x: 5, y: 1}
  55. }
  56. }

Blocks are divided into several types: bars, blocks, convex blocks (T blocks), L blocks, anti-L blocks, Z blocks, and anti-Z blocks;

A total of seven squares are represented by index keys 1, 2, 3, 4, 5, 6, and 7. The square is composed of four small blocks, each of which has its own coordinates. 1-4 represents the initial Coordinate Position of the block, 5 indicates the rotation point;

 
 
  1. OLSFK. Init = function () {// initialization Interface
  2. //...
  3. }

Interface Initialization Method of Tetris. It will be called and executed in window. onload;

 
 
  1. var w = OLSFK.Options.width;  
  2.     var h = OLSFK.Options.height;  
  3.     var total = w * h;  
  4.  
  5.     var x=0,y=0;  
  6.     for (var i=0; i<total; i++)  
  7.     {  
  8.           
  9.         OLSFK.Options.GampMap[x+'_'+y] = 0;  
  10.  
  11.         Lib.Tag('SPAN',{id:"box_"+x+"_"+y,name:"cbox",style:{  
  12.             width:OLSFK.Options.boxWidth,  
  13.             height:OLSFK.Options.boxWidth,  
  14.             border:"2px outset #669",  
  15.             background:"#ddd",  
  16.             float:"left",  
  17.             overflow:"hidden" 
  18.         },innerHTML:"&nbsp;",className:"cssbox"},back);  
  19.  
  20.         var end = i + 1;  
  21.         x++;  
  22.         if (end >= w && end % w == 0)  
  23.         {  
  24.             x=0;  
  25.             y++;  
  26.             Lib.Tag('DIV',{style:{  
  27.                 clear:"both" 
  28.             }},back);  
  29.         }  
  30.  
  31.     } 

By setting Options. width, Options. height, the number of columns and the number of rows, and the configured small square width, a table interface with the width: Options. width is initialized, and the height is Options. height;

Lib. Tag is used to create a Tag object;

 
 
  1. Lib.Tag = function(TAG,json,pnode) {  
  2.     //...  
  3. }  

TAG is the TAG name, such as div and span;

Json sets the label style;

Pnode is the parent container of the Creation;

OLSFK. Init = function () {} also creates the next random block preview area, current level, and score next to the primary game area, as well as the "Start" and "pause" buttons;


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.