JavaScript實(shí)現(xiàn)簡(jiǎn)潔的俄羅斯方塊完整實(shí)例
本文實(shí)例講述了JavaScript實(shí)現(xiàn)簡(jiǎn)潔的俄羅斯方塊。分享給大家供大家參考,具體如下:
先來(lái)看看運(yùn)行效果圖:
完整實(shí)例代碼如下:
<!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) { // 創(chuàng)建4個(gè)div用來(lái)組合出各種方塊 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; // 如果有傳參,優(yōu)先使用參數(shù)的,如果有預(yù)告,優(yōu)先使用預(yù)告,都沒(méi)有就自己生成 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("游戲結(jié)束"); } 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"; } } // 顯示預(yù)告 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"; } } // 顯示分?jǐn)?shù) this.showScore = function() { if(this.container && this.score) { this.score.innerHTML = "score:" + this.container.score; } } // 水平移動(dòng)方塊的位置 this.hMove = function(step) { if(this.container.check(this.x- -step,this.y,this.shape)) { this.x += step; this.show(); } } // 垂直移動(dòng)方塊位置 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(); } } // 旋轉(zhuǎn)方塊 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() { // 繪制方塊所在區(qū)域 var bgDiv = createElm("div","f"); bgDiv.style.width = size*col+"px"; bgDiv.style.height = size*row+"px"; // 繪制預(yù)告所在區(qū)域 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) { // 記錄最左邊水平坐標(biāo) if(shape[i]<leftmost) leftmost = shape[i]; // 記錄最右邊水平坐標(biāo) if(shape[i]>rightmost) rightmost = shape[i]; // 記錄最下邊垂直坐標(biāo) if(shape[i+1]>undermost) undermost = shape[i+1]; // 判斷是否碰撞 if(this[(shape[i+1]- -y)*100- -(shape[i]- -x)]) flag = true; } // 判斷是否到達(dá)極限高度 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]; } // 遍歷整個(gè)容器,判斷是否可以消除 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>
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《JavaScript查找算法技巧總結(jié)》、《JavaScript動(dòng)畫(huà)特效與技巧匯總》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
- javascript實(shí)現(xiàn)俄羅斯方塊游戲的思路和方法
- JS俄羅斯方塊,包含完整的設(shè)計(jì)理念
- JS 俄羅斯方塊完美注釋版代碼
- JAVASCRIPT代碼編寫(xiě)俄羅斯方塊網(wǎng)頁(yè)版
- 60行js代碼實(shí)現(xiàn)俄羅斯方塊
- 使用JS代碼實(shí)現(xiàn)俄羅斯方塊游戲
- JavaScript實(shí)現(xiàn)俄羅斯方塊游戲過(guò)程分析及源碼分享
- JS+Canvas實(shí)現(xiàn)的俄羅斯方塊游戲完整實(shí)例
- js html5 css俄羅斯方塊游戲再現(xiàn)
- JavaScript canvas實(shí)現(xiàn)俄羅斯方塊游戲
相關(guān)文章
JSON.parse損壞大數(shù)字的原因解析及解決方案
從10多年前JSON在線編輯器的早期開(kāi)始,用戶經(jīng)常反映編輯器有時(shí)會(huì)破壞他們JSON文檔中的大數(shù)字的問(wèn)題,這篇文章主要介紹了為什么JSON.parse會(huì)損壞大數(shù)字,如何解決這個(gè)問(wèn)題,需要的朋友可以參考下2022-10-10js實(shí)現(xiàn)樓層效果的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇js實(shí)現(xiàn)樓層效果的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07Three.js實(shí)現(xiàn)繪制字體模型示例代碼
最近在學(xué)習(xí)three.js,這篇文章屬于系列文章,下面這篇文章主要給大家介紹了關(guān)于Three.js如何繪制字體模型的相關(guān)資料,通過(guò)文中介紹的方法實(shí)現(xiàn)的效果非常的贊,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-09-09跨瀏覽器開(kāi)發(fā)經(jīng)驗(yàn)總結(jié)(四) 怎么寫(xiě)入剪貼板
讓你的操作剪切板的操作支持多瀏覽器,一般IE,Firefox2010-05-05javascript比較兩個(gè)日期相差天數(shù)的方法
這篇文章主要介紹了javascript比較兩個(gè)日期相差天數(shù)的方法,涉及javascript針對(duì)日期的轉(zhuǎn)換與數(shù)學(xué)運(yùn)算相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-07-07關(guān)于javascript document.createDocumentFragment()
documentFragment 是一個(gè)無(wú)父對(duì)象的document對(duì)象.2009-04-04javascript簡(jiǎn)單實(shí)現(xiàn)表格行間隔顯示顏色并高亮顯示
表格行間隔顯示顏色并實(shí)現(xiàn)高亮顯示,這種效果大家都有見(jiàn)到過(guò)吧,下面就為大家詳細(xì)介紹下,需要的朋友可不要錯(cuò)過(guò)2013-11-11JavaScript平鋪數(shù)組轉(zhuǎn)樹(shù)形結(jié)構(gòu)的實(shí)現(xiàn)示例
本文主要介紹了JavaScript平鋪數(shù)組轉(zhuǎn)樹(shù)形結(jié)構(gòu)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07element-ui?對(duì)話框dialog使用echarts報(bào)錯(cuò)'dom沒(méi)有獲取到'的問(wèn)題
這篇文章主要介紹了element-ui?對(duì)話框dialog里使用echarts,報(bào)錯(cuò)'dom沒(méi)有獲取到'的問(wèn)題,在這個(gè)事件里邊進(jìn)行echarts的初始化,執(zhí)行數(shù)據(jù),本文結(jié)合實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-11-11