Comments: Tetris has 7 parts, each of which occupies a different number and position of rectangles. Therefore, a component class is created and an array is created to store 7 parts, each part contains an array to store the number and position of the rectangle occupied by this part. The following is a detailed introduction.Basic principles of this game:
The game area is a limited size area. The game area of this game has 21x25 rectangles, each of which is 10 in width, heght is 6 units (the absolute unit of canvas is fixed, not pixel ).
The create RusBlock class contains the corresponding data and behavior, and creates a two-dimensional array aState [21] [25] to record the marked rectangle in the game area.
Tetris has 7 parts, each of which occupies a different number and position of rectangles. Therefore, a component class is created and an array is created to store 7 parts, each part contains an array to store the number and position of the rectangle occupied by the part. When the falling part ends, a new part is generated, and the marked rectangle of the part is assigned to the array of the game area.
In the game loop function, print the falling parts, fixed parts, and the falling parts.
Basic knowledge:
HTML5 CSS JS
This game contains three files:
RusBlock.html: Set Element
RusBlock.css: Set the style
RusBlock. js: script control
Step 1: Set the interface and prepare materials
RusBlock.html
The Code is as follows:
<! DOCTYPE html>
<Html>
<Head>
<Title> RusBlock </title>
<Link rel = "stylesheet" type = "text/css" href?#rusblock.css ">
<Script type = "text/javascript">
Function ShareGame (){
Var URL = "http://share.renren.com/share/buttonshare.do? Link = "+ document. URL +" & title = RusBlock ";
Window. showModalDialog ([URL]);
}
</Script>
</Head>
<Body onkeyup = "Action (event)">
<Audio loop = "loop" id = "Background-AudioPlayer" preload = "auto">
<Source src = "audio/background″" type = "audio/mp3"/>
</Audio>
<Audio id = "GameOver-AudioPlayer" preload = "auto">
<Source src = "audio/gameover.ogg" type = "audio/ogg">
</Audio>
<Audio id = "Score-AudioPlayer" preload = "auto">
<Source src = "audio/scorew." type =" audio/mp3 "/>
</Audio>
<Div id = "Game-Area">
<Div id = "Button-Area">
<H1 id = "Game-Name"> RusBlock <Button id = "Button-Game-Start" onclick = "GameStart ()"> Start </button>
<Button id = "Button-Game-End" onclick = "GameEnd ()"> End </button>
<Form id = "Form-Game-Level">
<Select id = "Select-Game-Level">
<Option value = "500" selected = "selected"> Easy </option>
<Option value = "300"> Normal </option>
<Option value = "200"> Hard </option>
</Select>
</Form>
<Button onclick = "ShareGame ()" id = "Button-Game-Share"> Share to everyone </button>
</Div>
<Canvas id = "Game-Canvas"> </canvas>
<Div id = "Score-Area">
<H2> Score <P id = "Game-Score"> 0 </p>
</Div>
</Div>
<Script type = "text/javascript" src = "RusBlock. js"> </script>
</Body>
</Html>
Step 2: Style
RosBlock.css
The Code is as follows:
Body {
Background-color: gray;
Text-align: center;
Font-family: 'times New Roman ';
Background-image: url ("");
}
H1 # Game-Name {
Background-color: white;
Width: 100%;
Font-size: x-large;
}
H2, # Game-Score {
Font-size: x-large;
Background-color: white;
}
# Game-Area {
Position: absolute;
Left: 10%;
Width: 80%;
Height: 99%;
}
Canvas # Game-Canvas {
Background-color: white;
Width: 80%;
Height: 98%;
Float: left;
}
# Button-Area, # Score-Area {
Width: 10%;
Height: 100%;
Float: left;
}
# Button-Game-Start, # Button-Game-End, # Button-Game-Share, # Select-Game-Level {
Width: 100%;
Height: 10%;
Font-size: larger;
Border-right-width: 3px;
Background-color: white;
}
# Select-Game-Level {
Width: 100%;
Height: 100%;
Font-size: x-large;
Border-color: gray;
}
Step 3: Compile js Code
RusBlock. js
The Rusblock class includes Member parsing:
Data:
NCurrentComID: ID of the current falling part
AState [21] [25]: an array of game region statuses
CurrentCom: Current falling part
NextCom: Next part
PtIndex: Index of the currently falling part relative to the game Region
Function:
NewNextCom (): generates the next part.
NextComToCurrentCom (): transfers data of the next part to the current falling part.
CanDown (): determines whether the current part can still fall
CanNew (): determines whether new parts can be generated.
Left (): The current part moves to the Left.
Right (): Move the current widget to the Right
Rotate (): The current part rotates clockwise.
Acceleratet (): The current part is accelerated downward.
Disappear (): deletes a row.
CheckFail (): determines whether the game fails.
InvalidateRect (): refreshes the region of the current part.
Complete: Download Demo