這篇文章主要介紹了借助HTML5 Canvas API製作一個簡單的猜字遊戲的實例分享,遊戲中每局會自動生成一個字母,玩家按鍵盤來猜測該字母是哪一個,需要的朋友可以參考下
二話不說,先上效果圖以及原始碼~
#HTML程式碼
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <script type="text/javascript" src="chp1_guess_the_letter.js"></script> <script type="text/javascript" src="modernizr.custom.99886.js"></script> </head> <body> <canvas id="canvas_guess_the_letter" width="500" height="300"> 你的浏览器不支持HTML5 Canvas </canvas> <form> <input type="button" id="createImageData" value="Export Canvas Image" />; </form> </body> </html>
JS程式碼
/** * @author Rafael */ window.addEventListener("load", eventWindowLoaded, false); var Debugger = function() { }; Debugger.log = function(message) { try { console.log(message); } catch(exception) { return; } } function eventWindowLoaded() { canvasApp(); } function canvasSupport() { return Modernizr.canvas; } function canvasApp() { var guesses = 0; var message = "Guess The Letter From a(lower) to z(higher)"; var letters = ["a","b","c","d","e","f","g","h","i","j","k","l", "m","n","o","p","q","r","s","t","u","v","w","x","y","z"]; var today = new Date(); var letterToGuess = ""; var higherOrLower = ""; var letterGuessed = []; var gameOver = false; if(!canvasSupport()) { return; } var theCanvas = document.getElementById("canvas_guess_the_letter"); var context = theCanvas.getContext("2d"); initGame(); function initGame() { var letterIndex = Math.floor(Math.random() * letters.length); letterToGuess = letters[letterIndex]; guesses = 0; lettersGuessed = []; gameOver = false; window.addEventListener("keyup", eventKeyPressed, true); var formElement = document.getElementById("createImageData"); formElement.addEventListener('click', createImageDataPressed, false); drawScreen(); } function eventKeyPressed(e) { if(!gameOver) { var letterPressed = String.fromCharCode(e.keyCode); letterPressed = letterPressed.toLowerCase(); guesses++; letterGuessed.push(letterPressed); if(letterPressed == letterToGuess) { gameOver = true; } else { letterIndex = letters.indexOf(letterToGuess); guessIndex = letters.indexOf(letterPressed); if(guessIndex < 0) { higherOrLower = "请输入正确的字符"; } else if(guessIndex < letterIndex) { higherOrLower = "小了"; } else { higherOrLower = "大了"; } } drawScreen(); } } function drawScreen() { //背景 context.fillStyle = "#ffffaa"; context.fillRect(0, 0, 500, 300); //箱子 context.strokeStyle = "#000000"; context.strokeRect(5, 5, 490, 290); context.textBaseLine = "top"; //日期 context.fillStyle = "#000000"; context.font = "10px _sans"; context.fillText(today, 150, 20); //消息 context.fillStyle = "#FF0000"; context.font = "14px _sans"; context.fillText(message, 125, 40); //猜测次数 context.fillStyle = "#109900"; context.font = "16px _sans"; context.fillText("猜测次数: "+guesses, 215, 60); //大还是小 context.fillStyle = "#000000"; context.font = "16px _sans"; context.fillText("大还是小: "+higherOrLower, 150, 135); //已经猜测的字符 context.fillStyle = "#FF0000"; context.font = "16px _sans"; context.fillText("已经猜测的字符: "+letterGuessed.toString(), 10, 260); if(gameOver) { context.fillStyle = "#FF0000"; context.font = "40px _sans"; context.fillText("你猜中了", 150, 180); } } function createImageDataPressed(e) { window.open(theCanvas.toDataURL(), "canvasImage","left=0, top=0, width="+theCanvas.width+", height="+theCanvas.height+", toolbar=0, resizable=0"); } }
從遊戲名稱可以看出,該遊戲是猜字遊戲。每局系統都會自動產生一個字母,玩家會按鍵盤來猜測字母是哪一個。
例如生成的是s,玩家按了h,則遊戲就會提示《小了》,因為英文字母當中h的索引比s的索引更靠前。
案例當中涉及的變數。
guesses:猜測次數
message:文字提示,指導使用者如何玩該遊戲
letters:文字數組,存放我們要猜測的文字的集合。這個例子用的是a到z
today:今天的日期
letterToGuess:要猜測的文字
higherOrLower:是《大了》還是《小了》
letterGuessed:已經猜測過得文字
gameOver:遊戲是否結束,是布林變量,開始的時候是false,猜對後設為true
變數的聲明
var guesses = 0; var message = "Guess The Letter From a(lower) to z(higher)"; var letters = ["a","b","c","d","e","f","g","h","i","j","k","l", "m","n","o","p","q","r","s","t","u","v","w","x","y","z"]; var today = new Date(); var letterToGuess = ""; var higherOrLower = ""; var letterGuessed = []; var gameOver = false;
#初始化遊戲
function initGame() { var letterIndex = Math.floor(Math.random() * letters.length); letterToGuess = letters[letterIndex]; guesses = 0; lettersGuessed = []; gameOver = false; window.addEventListener("keyup", eventKeyPressed, true); var formElement = document.getElementById("createImageData"); formElement.addEventListener('click', createImageDataPressed, false); drawScreen(); }
透過使用Math的random()函數和floor()函數,根據文字的陣列產生要猜測的文字。
並且當使用者按鍵盤的時候監聽《keyup》事件,根據傳遞過來的event,產生按下的鍵值。
因為猜測遊戲對大小寫不敏感,為防止使用者按大寫字母,我們需要把值轉換成小寫形式。
猜測次數1
猜測出來的文字加到已經猜測的文字數組當中
var letterPressed = String.fromCharCode(e.keyCode); letterPressed = letterPressed.toLowerCase(); guesses++; letterGuessed.push(letterPressed);
剩下的就只有判斷大和小了。
透過indexOf函數 我們可以判斷要猜測的文字和我們輸入的文字在字元集上面的索引值。
如果我們輸入的更靠前則提示《小了》反之《大了》
最終用戶猜對了要猜測的文字我們會在中央用大號字體顯示《你猜對了》
letterIndex = letters.indexOf(letterToGuess); guessIndex = letters.indexOf(letterPressed); if(guessIndex < 0) { higherOrLower = "请输入正确的字符"; } else if(guessIndex < letterIndex) { higherOrLower = "小了"; } else { higherOrLower = "大了"; }
至此這個功能差不多完成了吧,我們還有一個小功能,那就是透過按下按鈕的方式可以把螢幕結果抓去出來。
用的函數為toDataUrl(),有興趣的朋友研究一下。
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!
相關推薦:
以上是HTML5 Canvas API製作簡單的猜字遊戲的詳細內容。更多資訊請關注PHP中文網其他相關文章!