借助HTML5 Canvas API制作一个简单的猜字游戏
程序员文章站
2022-10-15 18:15:19
这篇文章主要介绍了借助HTML5 Canvas API制作一个简单的猜字游戏的实例分享,游戏中每局会自动生成一个字母,玩家按键盘来猜测该字母是哪一个,需要的朋友可以参考下... 16-03-25...
二话不说,先上效果图以及源代码~
html代码
xml/html code复制内容到剪贴板
- <!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代码
javascript code复制内容到剪贴板
- /**
- * @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
变量的声明
javascript code复制内容到剪贴板
- 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;
初始化游戏
javascript code复制内容到剪贴板
- 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
猜测出来的文字添加到已经猜测的文字数组当中
javascript code复制内容到剪贴板
- var letterpressed = string.fromcharcode(e.keycode);
- letterpressed = letterpressed.tolowercase();
- guesses++;
- letterguessed.push(letterpressed);
剩下的就只有判断 大和小了。
通过indexof函数 我们可以判断要猜测的文字和我们输入的文字在字符集上面的索引值。
如果我们输入的更靠前则提示《小了》反之《大了》
最终用户猜对了要猜测的文字 我们会在*用大号字体显示《你猜对了》
javascript code复制内容到剪贴板
- letterindex = letters.indexof(lettertoguess);
- guessindex = letters.indexof(letterpressed);
- if(guessindex < 0) {
- higherorlower = "请输入正确的字符";
- } else if(guessindex < letterindex) {
- higherorlower = "小了";
- } else {
- higherorlower = "大了";
- }
至此这个功能差不多完成了吧,我们还有一个小功能,那就是通过按下按钮的方式可以把屏幕结果抓去出来。
用的函数为todataurl(),有兴趣的朋友研究一下。
推荐阅读
-
利用HTML5 Canvas制作一个简单的打飞机游戏
-
借助HTML5 Canvas API制作一个简单的猜字游戏
-
借助HTML5 Canvas API制作一个简单的猜字游戏_html5教程技巧
-
html5 canvas-2.用canvas制作一个猜字母的小游戏_html5教程技巧
-
利用HTML5 Canvas制作一个简单的打飞机游戏
-
利用HTML5 Canvas制作一个简单的打飞机游戏_html5教程技巧
-
借助HTML5 Canvas API制作一个简单的猜字游戏_html5教程技巧
-
html5 canvas-2.用canvas制作一个猜字母的小游戏_html5教程技巧
-
利用HTML5 Canvas制作一个简单的打飞机游戏_html5教程技巧
-
HTML5 Canvas API制作简单的猜字游戏