日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

html代码打猎小游戏,html贪吃蛇小游戏课件-附源代码

發(fā)布時間:2024/3/13 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html代码打猎小游戏,html贪吃蛇小游戏课件-附源代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

jQuery(function ($){

var playGround = {

x : 600,

y : 360

}

var step = 20;

var score = 0;

var timer;

var bodyArry = [];

var $head = $("#head");

var $gameBox = $("#gameBox");

var $egg;

var typeY = 0;

var typeX = 1;

// 綁定

$(document).bind("keydown",function(evt){

console.log(evt.keyCode);

move(evt.keyCode);

});

$("#reStart").bind("click",function(){

reStart();

})

function reStart(){

window.location.reload();

}

function addEgg(){

eggX = Math.round(Math.random() * (playGround.x/20-1)) *

step;

eggY = Math.round(Math.random() * (playGround.y/20-1)) *

step;

addBody(eggX, eggY, "egg");

}

function eatEgg(){

// 吃蛋,

if($head.css("top") == $egg.css("top")

&& $head.css("left") == $egg.css("left") ){

// 轉(zhuǎn)化為身體

$egg.removeClass("egg").addClass("snakeBody");

bodyArry.push($egg);

// 加分

$("#score").html(++score);

// 加蛋

addEgg();

}

}

function initGame(){

addBody(180,200);

addBody(160,200);

addEgg();

snakeRun(true);

}

function move(key){

if(key == 38){ //上

typeX = 0;

typeY = -1;

}else if(key == 39){ //右

typeX = 1;

typeY = 0;

}else if(key == 40){ //下

typeX = 0;

typeY = 1;

}else if(key == 37){ //左

typeX = -1;

typeY = 0;

}else if(key == 32){ //左

snakeRun(false);

}else if(key == 82 || key ==13){ //左

snakeRun(true);

}else{

if(key){

return;

}

}

// debugger;

var moveY = typeY * step;

var moveX = typeX * step;

// 保存頭部走動前位置

var headOldY = +$head.css("top").split("px")[0];

var headOldX = +$head.css("left").split("px")[0];

// 頭部走動一步

$head.css("top","+="+moveY+"px");

$head.css("left","+="+moveX+"px");

// 尾部移動到原來頭的位置

bodyArry[bodyArry.length-1].css("top",headOldY+"px");

bodyArry[bodyArry.length-1].css("left",headOldX+"px");

if(bodyArry.length>1){

bodyArry.unshift(bodyArry[bodyArry.length-1]);

bodyArry.pop();

}

// 判斷游戲是否結(jié)束

if(gameOver()){

clearInterval(timer);

adAlert("gameOver!!!", function(){

reStart();

});

}

// 是否吃蛋

eatEgg();

}

function gameOver (){

// 1.撞墻

var headY = +$head.css("top").split("px")[0];

var headX = +$head.css("left").split("px")[0];

console.log("x:"+headX+",y:"+headY);

if(headY < 0 || headY > playGround.y-1 || headX < 0

|| headX > playGround.x-1){

return true;

}

// 2.撞自己

for (var i = 0; i < bodyArry.length; i++) {

if( $head.css("top") == bodyArry[i].css("top") &&

$head.css("left") == bodyArry[i].css("left")){

return true;

};

};

return false;

}

var n = 1;

function addBody(x, y, type){

var type = type || "snakeBody";

var html = " ";

n++;

var $snakeBody = $(html);

$snakeBody.css("top",y+"px");

$snakeBody.css("left",x+"px");

$gameBox.append($snakeBody);

if(type != "egg"){

bodyArry.push($snakeBody);

}else{

$egg = $snakeBody;

}

}

function snakeRun(bool){

if(bool){

timer = setInterval(function(){

move();

},400);

}else{

clearInterval(timer);

}

}

function myAlert(str , fun){

$("#myAlert-content").html(str);

$("#myAlert-bk").show();

$("#myAlert").show();

$("#myAlert-btn-ok").unbind("click").bind("click",function(){

$("#myAlert-bk").hide();

$("#myAlert").hide();

// 重啟游戲

if(typeof fun == "function")fun();

})

}

adAlert("你準(zhǔn)備好了嗎?",function(){

initGame();

})

})

對話框

總結(jié)

以上是生活随笔為你收集整理的html代码打猎小游戏,html贪吃蛇小游戏课件-附源代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。