您好,登錄后才能下訂單哦!
都說(shuō)碼農(nóng)都該有自己的博客,我也整個(gè),這是第一次發(fā)。
自學(xué)JS/JQ半年多,半年前要是聽(tīng)說(shuō)誰(shuí)會(huì)用JS/JQ做個(gè)貪吃蛇我都會(huì)覺(jué)得他是大神,現(xiàn)在覺(jué)得,也就那樣了。
最近沒(méi)項(xiàng)目做,正好想到貪吃蛇我現(xiàn)在應(yīng)該能做出來(lái)了。
說(shuō)干就干,先不看別人的代碼,自己搞,半天的時(shí)間搞定了。
偷個(gè)懶用JQ寫的,也不知道有沒(méi)有啥問(wèn)題。
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>無(wú)標(biāo)題文檔</title>
- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- var hgn = 15;//橫格數(shù)
- var sgn = 10;//豎格數(shù)
- var sc = 3;//初始蛇長(zhǎng)
- var sind = new Array;//蛇索引
- var sd = 200;//速度
- var fxcur = null;
- var inv;
- var dan_ind;//膽索引
- var stop_onf = true;//暫停
- $(".body").css({width:50*hgn+"px"});
- $(".msg").css({width:$(".body").css("width")});
- for(var i=0;i<hgn;i++){
- for(var j=0;j<sgn;j++){
- $(".body ul").append("<li></li>");
- }
- }
- for(var i=0;i<sc;i++){
- sind[i]=i;
- }
- dan();
- she();
- move();
- //繪蛇
- function she(){
- $("ul li").removeClass("she");
- for(var i=0;i<sc;i++){
- $("ul li:eq("+sind[i]+")").addClass("she");
- }
- }
- //移動(dòng)
- function move(){
- if(inv){
- window.clearInterval(inv);
- }
- inv = window.setInterval(function(){
- goway(fxcur);
- },sd);
- }
- //移動(dòng)開(kāi)關(guān)
- function stopmove(){
- if(stop_onf){
- if(inv){
- window.clearInterval(inv);
- }
- $(".msg").animate({top:(parseInt($(".body").css("height"))/2-50)+"px"}).html("STOP");
- }else{
- $(".msg").html("GO").animate({top:"-50px"});
- move();
- }
- stop_onf = !stop_onf;
- }
- //結(jié)束
- function gameover(){
- if(inv){
- window.clearInterval(inv);
- }
- $(".msg").animate({top:(parseInt($(".body").css("height"))/2-50)+"px"}).html("GAME OVER");
- }
- //判斷撞到自己
- function zsf(){
- for(var i=0;i<sc-1;i++){
- if(sind[sc-1]==sind[i]){
- gameover();
- return true;
- }
- }
- return false;
- }
- //移動(dòng)功能
- function goway(fx){
- for(var i=0;i<sc-1;i++){
- if(sind[sc-1]==sind[i]){
- gameover();
- return;
- }
- }
- if(fx=="you"){
- if(sind[sc-1]+1==dan_ind){//吃膽
- scsc=sc+1;
- sind[sc-1]=dan_ind;
- dan();
- she();
- return;
- }else if((sind[sc-1]+1)%hgn==0){//撞墻
- gameover();
- return;
- }
- for(var i=0;i<sc;i++){
- if(i==sc-1){
- sind[i]=sind[i]+1;
- she();
- return;
- }
- sind[i]=sind[i+1];
- }
- }else if(fx=="xia"){
- if(sind[sc-1]+hgn==dan_ind){//吃膽
- scsc=sc+1;
- sind[sc-1]=dan_ind;
- dan();
- she();
- return;
- }else if(sind[sc-1]>hgn*sgn-hgn){//撞墻
- gameover();
- return;
- }
- for(var i=0;i<sc;i++){
- if(i==sc-1){
- sind[i]=sind[i]+hgn;
- she();
- return;
- }
- sind[i]=sind[i+1];
- }
- }else if(fx=="zuo"){
- if(sind[sc-1]-1==dan_ind){//吃膽
- scsc=sc+1;
- sind[sc-1]=dan_ind;
- dan();
- she();
- return;
- }else if((sind[sc-1])%hgn==0){//撞墻
- gameover();
- return;
- }
- for(var i=0;i<sc;i++){
- if(i==sc-1){
- sind[i]=sind[i]-1;
- she();
- return;
- }
- sind[i]=sind[i+1];
- }
- }else if(fx=="shang"){
- if(sind[sc-1]-hgn==dan_ind){//吃膽
- scsc=sc+1;
- sind[sc-1]=dan_ind;
- dan();
- she();
- return;
- }else if(sind[sc-1]<hgn){//撞墻
- gameover();
- return;
- }
- for(var i=0;i<sc;i++){
- if(i==sc-1){
- sind[i]=sind[i]-hgn;
- she();
- return;
- }
- sind[i]=sind[i+1];
- }
- }else{
- return;
- }
- }
- //判斷是否吃到膽
- function isdan(num){
- if(num==dan_ind){
- scsc=sc+1;
- sind[sc-1]=num;
- dan();
- }
- }
- //按鍵
- $(document).keydown(function(event){
- var down_num = event.which;
- var fx = null;
- if(down_num==37){//左
- fx = "zuo";
- }else if(down_num==38){//上
- fx = "shang";
- }else if(down_num==40){//下
- fx = "xia";
- }else if(down_num==39){//右
- fx = "you";
- }else if(down_num==32){
- stopmove();
- }
- //需判斷能否點(diǎn)擊
- if(fxcur=="shang"&&fx=="xia"||fxcur=="xia"&&fx=="shang"||fxcur=="zuo"&&fx=="you"||fxcur=="you"&&fx=="zuo"){
- return;
- }else{
- fxfxcur = fx;
- }
- });
- //繪膽
- function dan(){
- //需判斷生成膽是否在蛇索引上
- dan_ind = parseInt(hgn*sgn*Math.random()-1);
- for(var i=0;i<sc;i++){
- if(dan_ind==sind[i]){
- dan();
- return;
- }
- }
- $("ul li").removeClass("dan");
- $("ul li:eq("+dan_ind+")").addClass("dan");
- }
- })
- </script>
- <style>
- /*整體設(shè)置*/
- a:active {outline: none;star:expression(thisthis.onFocus=this.blur());}
- html{ overflow-y:scroll;}
- *{margin:0;padding:0;list-style:none;outline:none;word-wrap:break-word;}
- img{border:none}
- h2,h3,h4,h5,h6,h7,body,small{font-size:12px; font-weight:400; font-family:Arial,"宋體";}
- table{table-layout:fixed; border-collapse:collapse}
- a{ text-decoration:none;}
- body{ background:#fff;}
- .body{ overflow:hidden; border:1px solid #666; position:absolute; left:250px; top:0;}
- li{ width:48px; height:48px; overflow:hidden; float:left; border:1px solid #666; background:#6C9;}
- li.she{ background:#069;}
- li.dan{ background:#0CF;}
- .tel{ font-size:24px;}
- .msg{ overflow:hidden; font-size:24px; color:#333; text-align:center; position:absolute; left:250px; top:0;}
- </style>
- </head>
- <body>
- <div class="tel">方向鍵控制方向<br />空格鍵暫停</div>
- <div class="body">
- <ul>
- </ul>
- </div>
- <div class="msg"></div>
- </body>
- </html>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。