


A complete example of simple Tetris implementation in JavaScript_javascript skills
The example in this article describes how to implement simple Tetris in JavaScript. Share it with everyone for your reference, the details are as follows:
Let’s take a look at the operation renderings first:
The complete example code is as follows:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>俄罗斯方块</title> <style type="text/css"> .c{margin:1px; width:19px; height:19px; background:red; position:absolute;} .d{margin:1px; width:19px; height:19px; background:gray; position:absolute;} .f{top:0px; left:0px; background:black; position:absolute;} .e{top:0px; background:#151515; position:absolute;} .g{width:100px; height:20px; color:white; position:absolute;} </style> <script type="text/javascript"> var row = 18; var col = 10; var announcement = 6; var size = 20; var isOver = false; var shapes = ("0,1,1,1,2,1,3,1;1,0,1,1,1,2,2,2;2,0,2,1,2,2,1,2;0,1,1,1,1,2,2,2;1,2,2,2,2,1,3,1;1,1,2,1,1,2,2,2;0,2,1,2,1,1,2,2").split(";"); var tetris; var container; function createElm(tag,css) { var elm = document.createElement(tag); elm.className = css; document.body.appendChild(elm); return elm; } function Tetris(css,x,y,shape) { // 创建4个div用来组合出各种方块 var myCss = css?css:"c"; this.divs = [createElm("div",myCss),createElm("div",myCss),createElm("div",myCss),createElm("div",myCss)]; if(!shape) { this.divs2 = [createElm("div",myCss),createElm("div",myCss),createElm("div",myCss),createElm("div",myCss)]; this.score = createElm("div","g"); this.score.style.top = 10*size+"px"; this.score.style.left = (col- -1)*size+"px"; this.score.innerHTML = "score:0"; } this.container = null; this.refresh = function() { this.x = (typeof(x)!='undefined')?x:3; this.y = (typeof(y)!='undefined')?y:0; // 如果有传参,优先使用参数的,如果有预告,优先使用预告,都没有就自己生成 if(shape) this.shape = shape; else if(this.shape2) this.shape = this.shape2; else this.shape = shape?shape:shapes[Math.floor((Math.random()*shapes.length-0.000000001))].split(","); this.shape2 = shapes[Math.floor((Math.random()*shapes.length-0.000000001))].split(","); if(this.container && !this.container.check(this.x,this.y,this.shape)) { isOver = true; alert("游戏结束"); } else { this.show(); this.showScore(); this.showAnnouncement(); } } // 显示方块 this.show = function() { for(var i in this.divs) { this.divs[i].style.top = (this.shape[i*2+1]- -this.y)*size+"px"; this.divs[i].style.left = (this.shape[i*2]- -this.x)*size+"px"; } } // 显示预告 this.showAnnouncement = function() { for(var i in this.divs2) { this.divs2[i].style.top = (this.shape2[i*2+1]- -1)*size+"px"; this.divs2[i].style.left = (this.shape2[i*2]- -1- -col)*size+"px"; } } // 显示分数 this.showScore = function() { if(this.container && this.score) { this.score.innerHTML = "score:" + this.container.score; } } // 水平移动方块的位置 this.hMove = function(step) { if(this.container.check(this.x- -step,this.y,this.shape)) { this.x += step; this.show(); } } // 垂直移动方块位置 this.vMove = function(step) { if(this.container.check(this.x,this.y- -step,this.shape)) { this.y += step; this.show(); } else { this.container.fixShape(this.x,this.y,this.shape); this.container.findFull(); this.refresh(); } } // 旋转方块 this.rotate = function() { var newShape = [this.shape[1],3-this.shape[0],this.shape[3],3-this.shape[2],this.shape[5],3-this.shape[4],this.shape[7],3-this.shape[6]]; if(this.container.check(this.x,this.y,newShape)) { this.shape = newShape; this.show(); } } this.refresh(); } function Container() { this.init = function() { // 绘制方块所在区域 var bgDiv = createElm("div","f"); bgDiv.style.width = size*col+"px"; bgDiv.style.height = size*row+"px"; // 绘制预告所在区域 var bgDiv = createElm("div","e"); bgDiv.style.left = size*col+"px"; bgDiv.style.width = size*announcement+"px"; bgDiv.style.height = size*row+"px"; // 清空积分 this.score = 0; } this.check = function(x,y,shape) { // 检查边界越界 var flag = false; var leftmost=col; var rightmost=0; var undermost=0; for(var i=0;i<8;i+=2) { // 记录最左边水平坐标 if(shape[i]<leftmost) leftmost = shape[i]; // 记录最右边水平坐标 if(shape[i]>rightmost) rightmost = shape[i]; // 记录最下边垂直坐标 if(shape[i+1]>undermost) undermost = shape[i+1]; // 判断是否碰撞 if(this[(shape[i+1]- -y)*100- -(shape[i]- -x)]) flag = true; } // 判断是否到达极限高度 for(var m=0;m<3;m++) for(var n=0;n<col;n++) if(this[m*100+n]) flag = true; if((rightmost- -x+1)>col || (leftmost- -x)<0 || (undermost- -y+1)>row || flag) return false; return true; } // 用灰色方块替换红色方块,并在容器中记录灰色方块的位置 this.fixShape = function(x,y,shape) { var t = new Tetris("d",x,y,shape); for(var i=0;i<8;i+=2) this[(shape[i+1]- -y)*100- -(shape[i]- -x)] = t.divs[i/2]; } // 遍历整个容器,判断是否可以消除 this.findFull = function() { var s = 0; for(var m=0;m<row;m++) { var count = 0; for(var n=0;n<col;n++) if(this[m*100+n]) count++; if(count==col) { s++; this.removeLine(m); } } this.score -= -this.calScore(s); } this.calScore = function(s) { if(s!=0) return s- -this.calScore(s-1) else return 0; } // 消除指定一行方块 this.removeLine = function(row) { // 移除一行方块 for(var n=0;n<col;n++) document.body.removeChild(this[row*100+n]); // 把所消除行上面所有的方块下移一行 for(var i=row;i>0;i--) { for(var j=0;j<col;j++) { this[i*100- -j] = this[(i-1)*100- -j] if(this[i*100- -j]) this[i*100- -j].style.top = i*size + "px"; } } } } function init() { container = new Container(); container.init(); tetris = new Tetris(); tetris.container = container; document.onkeydown = function(e) { if(isOver) return; var e = window.event?window.event:e; switch(e.keyCode) { case 38: //up tetris.rotate(); break; case 40: //down tetris.vMove(1); break; case 37: //left tetris.hMove(-1); break; case 39: //right tetris.hMove(1); break; } } setInterval("if(!isOver) tetris.vMove(1)",500); } </script> </head> <body onload="init()"> </body> </html>
Readers who are interested in more JavaScript-related content can check out the special topics on this site: "Summary of JavaScript search algorithm techniques", "Summary of JavaScript animation special effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm techniques", "Summary of JavaScript traversal algorithms and techniques" and "JavaScript Mathematics Summary of operation usage》
I hope this article will be helpful to everyone in JavaScript programming.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
