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

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") ){

// 转化为身体

$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();

}

// 判断游戏是否结束

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("你准备好了吗?",function(){

initGame();

})

})

对话框

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,这是一个简单的C语言贪吃蛇小游戏源代码示例: ```c #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <windows.h> int width = 20, height = 20; int gameover; int x, y, fruitX, fruitY, score; int tailX[100], tailY[100]; int nTail; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; enum eDirection dir; void Setup() { gameover = 0; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("cls"); for (int i = 0; i < width + 2; i++) printf("#"); printf("\n"); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) printf("#"); if (i == y && j == x) printf("O"); else if (i == fruitY && j == fruitX) printf("F"); else { int printTail = 0; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { printf("o"); printTail = 1; } } if (!printTail) printf(" "); } if (j == width - 1) printf("#"); } printf("\n"); } for (int i = 0; i < width + 2; i++) printf("#"); printf("\n"); printf("Score:%d\n", score); } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameover = 1; break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x >= width) x = 0; else if (x < 0) x = width - 1; if (y >= height) y = 0; else if (y < 0) y = height - 1; for (int i = 0; i < nTail; i++) { if (tailX[i] == x && tailY[i] == y) gameover = 1; } if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { Setup(); while (!gameover) { Draw(); Input(); Logic(); Sleep(10); // 控制游戏速度 } return 0; } ``` 这个代码示例实现了一个基本的贪吃蛇小游戏,使用了C语言的基本语法和控制台输入输出函数。你可以将代码复制到一个C语言编译器中(如Dev-C++、Code::Blocks等)进行编译和运行。希望这能帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值