您好,登錄后才能下訂單哦!
本文實例為大家分享了js擲骰子小游戲的具體代碼,供大家參考,具體內容如下
因為這次作業是第一個實驗,比較簡單,但是要求面向對象來做,而我這個時候又剛好在學JS,JS來做骰子游戲即有前端了,又省了后臺處理,代碼量會比用JAVA來做少了非常非常多。考慮到這種實驗實在不想浪費時間去用JAVA寫,于是有了一下JS實現的版本。但是大家都知道JS的面向對象其實是偽面向對象。我感覺并不好用,因為用句柄的方式無法傳值,你封裝成對象了還得拆出來成為過程(單一方法)來用。
版本1: (沒有用面向對象)
<!DOCTYPE> <html> <head> <title>擲骰子游戲 author:SenDog</title> <meta charset="UTF-8"> <script> var leftX = 150; var topY = 100; var diceX = 80; var diceY = 80; var dotR = 4; var count = 0; var lastNum = 0; var flag = false; function clickMe() { count = 0; if(flag) { return false; } flag = true; var ctx = document.getElementById("canvas").getContext('2d'); ctx.beginPath(); // ctx.strokeRect(leftX,topY,diceX,diceY);//繪制矩形 加粗 //ctx.strokeRect(leftX+150,topY,diceX,diceY); setTimeout(function(){ random(ctx);},200); } function drawDice(ctx,randomNum,randomNum2) {//繪制 骰子 123456的點數 ctx.clearRect(leftX,topY,diceX,diceY); switch(randomNum) { case 1: draw1(); break; case 2: draw2(); break; case 3: draw3(); break; case 4: draw4(); break; case 5: draw5(); break; case 6: draw6(); break; } ctx.clearRect(leftX+150,topY,diceX,diceY); switch(randomNum2) { case 1: draw11(); break; case 2: draw22(); break; case 3: draw33(); break; case 4: draw44(); break; case 5: draw55(); break; case 6: draw66(); break; } count++; if(count>=20) { if(randomNum+randomNum2==7) { alert("骰子1:"+randomNum+",骰子2:"+randomNum2+"總和為7您贏了"); } else { alert("骰子1:"+randomNum+";骰子2:"+randomNum2); } flag = false; return false; } else { setTimeout(function(){ random(ctx); },200-count); } } function random(ctx) {//生成骰子點數,如果和上一步的點數相同重新生成點數,并繪制 var randomNum = Math.floor(Math.random()*6)+1; var randomNum2 = Math.floor(Math.random()*6)+1; if(randomNum == lastNum) { random(ctx); } else { lastNum = randomNum; drawDice(ctx,randomNum,randomNum2); } } function commonDraw(ctx,dotX,dotY) { ctx.beginPath(); ctx.arc(dotX,dotY,dotR,0,2*Math.PI,false); ctx.stroke(); ctx.fill(); } function draw1() { var ctx = document.getElementById("canvas").getContext('2d'); ctx.fillStyle="#0000ff"; var dotX = leftX+diceX/2; var dotY = topY+diceY/2; commonDraw(ctx,dotX,dotY); } function draw2() { var ctx = document.getElementById("canvas").getContext('2d'); ctx.fillStyle="#99FF66"; var dotX = leftX+4*dotR; var dotY = topY+4*dotR; commonDraw(ctx,dotX,dotY); var dotX = leftX+diceX-4*dotR; var dotY = topY+diceY-4*dotR; commonDraw(ctx,dotX,dotY); } function draw3() { draw1(); draw2(); } function draw4() { draw2(); var ctx = document.getElementById("canvas").getContext('2d'); ctx.fillStyle="#99CC00"; var dotX = leftX+diceX-4*dotR; var dotY = topY+4*dotR; commonDraw(ctx,dotX,dotY); var dotX = leftX+4*dotR; var dotY = topY+diceY-4*dotR; commonDraw(ctx,dotX,dotY); } function draw5(){ draw1(); draw4(); } function draw6(){ var ctx = document.getElementById("canvas").getContext('2d'); ctx.fillStyle="#996633"; var dotX = leftX+4*dotR; var dotY = topY+diceY/2 commonDraw(ctx,dotX,dotY); var dotX = leftX+diceY-4*dotR; commonDraw(ctx,dotX,dotY); draw4(); } /* -------------骰子2----------------*/ function draw11() { var ctx = document.getElementById("canvas").getContext('2d'); ctx.fillStyle="#0000ff"; var dotX = leftX+diceX/2+150; var dotY = topY+diceY/2; commonDraw(ctx,dotX,dotY); } function draw22() { var ctx = document.getElementById("canvas").getContext('2d'); ctx.fillStyle="#99FF66"; var dotX = leftX+4*dotR+150; var dotY = topY+4*dotR; commonDraw(ctx,dotX,dotY); var dotX = leftX+diceX-4*dotR+150; var dotY = topY+diceY-4*dotR; commonDraw(ctx,dotX,dotY); } function draw33() { draw11(); draw22(); } function draw44() { draw22(); var ctx = document.getElementById("canvas").getContext('2d'); ctx.fillStyle="#99CC00"; var dotX = leftX+diceX-4*dotR+150; var dotY = topY+4*dotR; commonDraw(ctx,dotX,dotY); var dotX = leftX+4*dotR+150; var dotY = topY+diceY-4*dotR; commonDraw(ctx,dotX,dotY); } function draw55(){ draw11(); draw44(); } function draw66(){ var ctx = document.getElementById("canvas").getContext('2d'); ctx.fillStyle="#996633"; var dotX = leftX+150+4*dotR; var dotY = topY+diceY/2 commonDraw(ctx,dotX,dotY); var dotX = leftX+150+diceY-4*dotR; commonDraw(ctx,dotX,dotY); draw44(); } function init() { var ctx = document.getElementById("canvas").getContext('2d'); ctx.beginPath(); ctx.strokeRect(leftX,topY,diceX,diceY);//繪制灰色框框 第一個骰子 ctx.strokeRect(leftX+150,topY,diceX,diceY);//第二個骰子的灰色框框 ctx.stroke(); draw6(); draw66(); } </script> </head> <body οnlοad="init();"> <canvas id="canvas" width="600" height="300" > your brower is not support html5 </canvas> <input type="button" value="開始" οnclick="clickMe();"/> </body> </html>
版本2,面向對象。但是JS真的很不適合面向對象,這只是偽裝面向對象。
輸入用戶名后, 會吧用戶的姓名和搖出的點數和存入cookie中。 cookie只會保存對應用戶最高的點數合。如果搖出更高的點數合,舊的就會被替換掉。通過查詢按鈕可以查詢任意姓名的最高點數合記錄。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>author:senDog 擲骰子游戲</title> </head> <body> 請輸入用戶姓名:<input id="userName1" type="text" > <button id="btn" > play </button> 查詢用戶最高成績<button id="btn2" >查詢 </button> <script language="JavaScript" type="text/javascript"> /*封裝骰子類*/ function shaizi(){ var num = parseInt(1+Math.random()*5); return{ "getNum":function(){ return num; }, "alertNum":function(){ alert("骰子點數:"+num); } }; } var s1 = new shaizi(); var s2 = new shaizi(); var x = document.getElementById("btn"); x.addEventListener("click",s1.alertNum); x.addEventListener("click",s2.alertNum); /*封裝cookie類*/ function cookie(){ return{ /*把總點數存入客戶端瀏覽器cookie,并設置每次關閉瀏覽器cookie消失*/ "addCookie":function(userName,num3){ var str = userName + "=" + escape(num3); document.cookie = str; //alert("str:"+str); }, "getCookie":function(userName){//獲取指定名稱的cookie的值 var strCookie=document.cookie; var arrCookie=strCookie.split("; "); for(var i=0;i<arrCookie.length;i++){ var arr=arrCookie[i].split("="); if(arr[0]==userName)return arr[1]; } return ""; } } } var ck = new cookie(); /*記錄用戶最高總點數,存入cookie,然后可以通過查詢用戶名查出用戶最高點數的記錄*/ function readName(){ userName1 = document.getElementById("userName1").value; } function ckSet(){ var num1=s1.getNum(); var num2 = s2.getNum(); num3 = num1+num2; var n = ck.getCookie(userName1); if(n!=null && n<num3) ck.addCookie(userName1,num3); } function ckGetName(){ // var userName1 = document.getElementById("userName1").value; alert("username:"+userName1); } function ckGetNum(){ //var userName1 = document.getElementById("userName1").value; var num=ck.getCookie(userName1); alert("用戶最高總點數:"+num); } x.addEventListener("click",readName); x.addEventListener("click",ckSet); x.addEventListener("click",ckGetName); x.addEventListener("click",ckGetNum); var y = document.getElementById("btn2"); y.addEventListener("click",readName); y.addEventListener("click",ckGetName); y.addEventListener("click",ckGetNum); </script> </body> </html>
把代碼復制到到TXT文檔,后綴改為html即可運行。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。