贪吃蛇
2015-11-27 10:57
387 查看
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<style type="text/css">
#div-container {
height: 200px;
width: 200px;
position: relative;
border: 1px solid black;
}
.divshe {
height: 5px;
width: 5px;
background-color: black;
position: absolute;
left: 0px;
top: 0px;
}
.divfood {
position: absolute;
height: 5px;
width: 5px;
background-color: black;
}
</style>
</head>
<body>
<div id="div-container">
<div id="div-she" class="divshe" tabindex="1" style="left:20px;"></div>
<div class="divshe" tabindex="1" style="left:15px;"></div>
<div class="divshe" tabindex="1" style="left:10px;"></div>
<div class="divshe" tabindex="1" style="left:5px;"></div>
</div>
</body>
</html>
<script type="text/javascript">
var she = $("#div-she");
var foodleft, foodtop, moveH, moveZ, speed;
speed = 100;
var flag = "d";
var height = $("#div-container").height() * 1;
var width = $("#div-container").width() * 1;
//键盘上下左右操作
window.document.onkeydown = function (e) {
e = event || e;
var press = e.keyCode;
moveH = parseInt(she.css("left"));
moveZ = parseInt(she.css("top"));
var a = [37, 38, 39, 40];
if (a.indexOf(press) >= 0) {
flag = press == 37 && flag != "d" ? "a" : press == 38 && flag != "s" ? "w" : press == 39 && flag != "a" ? "d" : press == 40 && flag != "w" ? "s" : flag;
var fanx = flag == "a" ? "left" : flag == "w" ? "top" : flag == "d" ? "left" : flag == "s" ? "top" : "";
var px = flag == "a" ? moveH : flag == "w" ? moveZ : flag == "d" ? moveH : flag == "s" ? moveZ : 0;
//she.css(fanx, px);
//move(fanx);
EatFood();
GetFood();
}
}
//吃到食物
function EatFood() {
if (foodleft == moveH && foodtop == moveZ) {
$(".divfood").addClass("divshe").removeClass("divfood");
}
speed = 104 - $(".divshe").length * 1;
clearInterval(s);
s = setInterval("ChangeMove()", speed);
}
//移动
function move() {
if (moveH < 0 || moveH > height - 5 || moveZ < 0 || moveZ > width - 5) {
alert("die");
window.location.href = window.location.href;
}
var arryl = [], arryt = [];
$(".divshe").each(function () {
arryl.push($(this).css("left"));
arryt.push($(this).css("top"));
})
for (var i = 1; i < arryl.length - 1; i++) {
if (moveH == parseInt(arryl[i]) && moveZ == parseInt(arryt[i])) {
alert("die");
window.location.href = window.location.href;
}
}
$(".divshe").each(function () {
$(this).css({ left: arryl[$(this).index() - 1], top: arryt[$(this).index() - 1] })
})
}
//出现食物
function GetFood() {
if (!$(".divfood").is(":visible")) {
var left = parseInt(Math.random() * 40) * 5;
var top = parseInt(Math.random() * 40) * 5;
foodleft = left;
foodtop = top;
var div = "<div class='divfood' style='left:" + left + "px;top:" + top + "px'></div>";
$("#div-container").append(div);
}
}
//改变移动方向
function ChangeMove() {
moveH = parseInt(she.css("left"));
moveZ = parseInt(she.css("top"));
move();
var fanx = flag == "a" ? "left" : flag == "w" ? "top" : flag == "d" ? "left" : flag == "s" ? "top" : "";
var px = flag == "a" ? moveH - 5 : flag == "w" ? moveZ - 5 : flag == "d" ? moveH + 5 : flag == "s" ? moveZ + 5 : 0;
she.css(fanx, px);
EatFood(moveH, moveZ);
GetFood();
}
//设置运行速度
var s = setInterval("ChangeMove()", speed);
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<style type="text/css">
#div-container {
height: 200px;
width: 200px;
position: relative;
border: 1px solid black;
}
.divshe {
height: 5px;
width: 5px;
background-color: black;
position: absolute;
left: 0px;
top: 0px;
}
.divfood {
position: absolute;
height: 5px;
width: 5px;
background-color: black;
}
</style>
</head>
<body>
<div id="div-container">
<div id="div-she" class="divshe" tabindex="1" style="left:20px;"></div>
<div class="divshe" tabindex="1" style="left:15px;"></div>
<div class="divshe" tabindex="1" style="left:10px;"></div>
<div class="divshe" tabindex="1" style="left:5px;"></div>
</div>
</body>
</html>
<script type="text/javascript">
var she = $("#div-she");
var foodleft, foodtop, moveH, moveZ, speed;
speed = 100;
var flag = "d";
var height = $("#div-container").height() * 1;
var width = $("#div-container").width() * 1;
//键盘上下左右操作
window.document.onkeydown = function (e) {
e = event || e;
var press = e.keyCode;
moveH = parseInt(she.css("left"));
moveZ = parseInt(she.css("top"));
var a = [37, 38, 39, 40];
if (a.indexOf(press) >= 0) {
flag = press == 37 && flag != "d" ? "a" : press == 38 && flag != "s" ? "w" : press == 39 && flag != "a" ? "d" : press == 40 && flag != "w" ? "s" : flag;
var fanx = flag == "a" ? "left" : flag == "w" ? "top" : flag == "d" ? "left" : flag == "s" ? "top" : "";
var px = flag == "a" ? moveH : flag == "w" ? moveZ : flag == "d" ? moveH : flag == "s" ? moveZ : 0;
//she.css(fanx, px);
//move(fanx);
EatFood();
GetFood();
}
}
//吃到食物
function EatFood() {
if (foodleft == moveH && foodtop == moveZ) {
$(".divfood").addClass("divshe").removeClass("divfood");
}
speed = 104 - $(".divshe").length * 1;
clearInterval(s);
s = setInterval("ChangeMove()", speed);
}
//移动
function move() {
if (moveH < 0 || moveH > height - 5 || moveZ < 0 || moveZ > width - 5) {
alert("die");
window.location.href = window.location.href;
}
var arryl = [], arryt = [];
$(".divshe").each(function () {
arryl.push($(this).css("left"));
arryt.push($(this).css("top"));
})
for (var i = 1; i < arryl.length - 1; i++) {
if (moveH == parseInt(arryl[i]) && moveZ == parseInt(arryt[i])) {
alert("die");
window.location.href = window.location.href;
}
}
$(".divshe").each(function () {
$(this).css({ left: arryl[$(this).index() - 1], top: arryt[$(this).index() - 1] })
})
}
//出现食物
function GetFood() {
if (!$(".divfood").is(":visible")) {
var left = parseInt(Math.random() * 40) * 5;
var top = parseInt(Math.random() * 40) * 5;
foodleft = left;
foodtop = top;
var div = "<div class='divfood' style='left:" + left + "px;top:" + top + "px'></div>";
$("#div-container").append(div);
}
}
//改变移动方向
function ChangeMove() {
moveH = parseInt(she.css("left"));
moveZ = parseInt(she.css("top"));
move();
var fanx = flag == "a" ? "left" : flag == "w" ? "top" : flag == "d" ? "left" : flag == "s" ? "top" : "";
var px = flag == "a" ? moveH - 5 : flag == "w" ? moveZ - 5 : flag == "d" ? moveH + 5 : flag == "s" ? moveZ + 5 : 0;
she.css(fanx, px);
EatFood(moveH, moveZ);
GetFood();
}
//设置运行速度
var s = setInterval("ChangeMove()", speed);
</script>
相关文章推荐
- Remove Duplicates from Sorted List
- /var/lock/subsys作用
- 简易记录日志
- javaweb学习总结(三十九)——数据库连接池
- jdk环境变量配置
- 二、Camera显示之Framework层设置显示窗口
- Windows和Linux下取得系统时间(毫秒级)
- List<T>与Dictionary<string,T>频繁检索的性能差距
- 逻辑架构、体系架构、整体架构、功能架构
- stand up meeting 11/26/2015
- [ActionScript 3.0] Away3D 天空盒(skybox)例子2
- Docker 监控实战
- IOS静态库制作
- 黑马程序员——JAVA基础——网络编程
- Ubuntu安装pycharm
- 五个免费UML建模工具推荐
- android客户端与服务器端交互 如何保持session
- 一、Camera显示之app实现简单camera
- kali攻防第5章 内网称霸之HTTPS账号密码获取
- 随想08:待杀的猪