本篇小编为大家分享一个用html5实现的简单贪吃蛇特效代码,喜欢的小伙伴们可以看一下
<html> <head> <meta charset='utf-8'/> <title>Snake</title> </head> <body> <canvas id="plank" style="border"></canvas> <script type="text/javascript"> //内置大量BUG,I'm sorry. var lev=100; //定时器间隔时间 var num=30; //网格大小,现在是30x30 var direction=3; //0:up 1:down 2:left 3:right var handle; //用于管理定时器 var score=0; //分数 var pause=true; //暂停使用 var canvas = document.getElementById('plank'); var context = canvas.getContext('2d'); var snakex=new Array(); //存储蛇身x坐标,下同 var snakey=new Array(); var prize=new Array(-1,-1); //食物的位置 function rand(){ //产生随机数 return parseInt(Math.random()*num); } function chk(x,y){ //检查是否结束,包括越界 if(x<0||y<0) return false; if(x>num-1||y>num-1) return false; for (var i=0; i!=snakex.length-1;i++) { if(snakex[i]==x&&snakey[i]==y) {return false;} }; return true; } function drawScore(text){ //打印分数 context.clearRect(0,0,300,25); context.fillText("Score:"+text,5,5); } function makeprize(){ //产生食物的位置 var flag=false; var prizepre=new Array(2); //使用链表会更好 while(!flag){ //食物位置不能在蛇体内 flag=true; prizepre[0]=rand();prizepre[1]=rand(); for (var i=0; i!=snakex.length;i++) { if((snakex[i]==prizepre[0])&&(snakey[i]==prizepre[1])) {flag=false;} } } prize=prizepre; } function runscore(x,y){ //判断是否吃到食物,并做处理 if(prize[0]==x&&prize[1]==y){ score=score+1; drawScore(score); snakex[snakex.length]=prize[0]; snakey[snakey.length]=prize[1]; makeprize(); drawNode(prize[0],prize[1]); return true; } return false; } function run(){ //定时器用来判断snake行进方向等等 switch(direction){ //方向 case 0: snakex[snakex.length]=snakex[snakex.length-1];snakey[snakey.length]=snakey[snakey.length-1]-1;break; case 1: snakex[snakex.length]=snakex[snakex.length-1];snakey[snakey.length]=snakey[snakey.length-1]+1;break; case 2: snakex[snakex.length]=snakex[snakex.length-1]-1;snakey[snakey.length]=snakey[snakey.length-1];break; case 3: snakex[snakex.length]=snakex[snakex.length-1]+1;snakey[snakey.length]=snakey[snakey.length-1];break; } if(!runscore(snakex[snakex.length-1],snakey[snakey.length-1])){ if(chk(snakex[snakex.length-1],snakey[snakey.length-1])==false) { clearInterval(handle); drawScore('\\tGame over'); return; } drawNode(snakex[snakex.length-1],snakey[snakey.length-1]); } clearNode(snakex[0],snakey[0]); snakex.shift(); snakey.shift(); } function drawNode(x,y){ //画点,共30X30个点(10*10像素算1个点) context.fillRect(x*10+1,y*10+31,10,10); } function clearNode(x,y){ context.clearRect(x*10+1,y*10+31,10,10); } function init(){ //初始化,设置画布大小,启动定时器等等 canvas.width = 510; canvas.height = 600; context.font = "normal 20px Airl"; context.textBaseline = "top"; context.fillText('P键开始/暂停,方向键控制',0,350); drawScore(''); context.strokeRect(0,30,302,302); makeprize(); drawNode(prize[0],prize[1]); snakex[0]=0;snakex[1]=1;snakex[2]=2; snakey[0]=0;snakey[1]=0;snakey[2]=0; drawNode(snakex[0],snakey[0]);drawNode(snakex[1],snakey[1]);drawNode(snakex[2],snakey[2]); } document.onkeydown=function(event){ //注册键盘事件,up,down,left,right,暂停键p var e = event || window.event; if(e&&e.keyCode==38){ direction=0; } if(e&&e.keyCode==40){ direction=1; } if(e&&e.keyCode==37){ direction=2; } if(e&&e.keyCode==39){ direction=3; } if(e&&e.keyCode==80){ if(pause) {pause=false;handle=setInterval(run,lev);} else {pause=true;clearInterval(handle);} } } init(); </script> </body> </html>
【相关推荐】
1. 免费h5在线视频教程
2. HTML5 完整版手册
The above is the detailed content of Share a snake special effects code implemented in html5. For more information, please follow other related articles on the PHP Chinese website!

H5 is HTML5, the fifth version of HTML. HTML5 improves the expressiveness and interactivity of web pages, introduces new features such as semantic tags, multimedia support, offline storage and Canvas drawing, and promotes the development of Web technology.

Accessibility and compliance with network standards are essential to the website. 1) Accessibility ensures that all users have equal access to the website, 2) Network standards follow to improve accessibility and consistency of the website, 3) Accessibility requires the use of semantic HTML, keyboard navigation, color contrast and alternative text, 4) Following these principles is not only a moral and legal requirement, but also amplifying user base.

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

WebStorm Mac version
Useful JavaScript development tools
