撑100s小游戏

<!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"></script>
<style type="text/css">
* {
padding: 0px;
margin: 0px;
}

.div-container {
position: relative;
height: 400px;
width: 400px;
left: 200px;
top: 0px;
border: 1px solid gray;
box-sizing: border-box;
overflow: hidden;
z-index: 100;
}

.span-dian {
position: absolute;
background: black;
height: 2px;
width: 2px;
overflow: hidden;
}

.mydiv {
position: absolute;
height: 10px;
width: 10px;
background-color: black;
top: 0px;
left: 0px;
}
.div-die{
color:red;
font-size:2em;
font-weight:bold;
}
</style>
</head>
<body>

<div class="div-container"><div class="mydiv"></div></div>
<div>你的分数:<span id="span-grade"></span></div>
<div class="div-die">撑100s</div>
<button onclick="refresh()">重新开始</button>
</body>
</html>
<script type="text/javascript">
var n = 1;//障碍个数
var grades = 0;//成绩
var startArry = [];//开始位置数组
var speed = 5000;//游戏速度
var count = 100;//障碍个数
var myCon = [];//玩家控制的坐标
getMyCon(0, 0);//配置玩家控制的坐标
var flag = false;
function getMyCon(myLeft, myTop) {
myCon = [];
myLeft = myLeft * 1;
myTop = myTop * 1;
for (var myi = myLeft ; myi < myLeft + 10; myi++) {
for (var myj = myTop; myj < myTop + 10; myj++) {
myCon.push([myi, myj]);
}
}
}
//配置开始位置数组
for (var i = 0; i <= 400; i++) {
startArry.push([-2, i]);
startArry.push([402, i]);
startArry.push([i, -2]);
startArry.push([i, 402]);
}
//鼠标控制移动
$(".div-container").mousemove(function (e) {
e = event || e;
var mouseX = e.clientX * 1 - parseInt($(this).css("left")) - 5;
var mouseY = e.clientY * 1 - parseInt($(this).css("top")) - 5;
getMyCon(mouseX, mouseY);
$(".mydiv").css({ left: mouseX, top: mouseY });
})

//添加障碍数量
var s = setInterval("add()", count);
function add() {
var startIndex = Math.floor(Math.random() * 1600);
var endIndex = Math.floor(Math.random() * 1600);
$(".div-container").append("<span class='span-dian' title='" + n + "' style='top:" + startArry[startIndex][0] + "px;left:" + startArry[startIndex][1] + "px'></span>");
$(".span-dian").each(function () {
if ($(this).attr("title") == n) {
$(this).animate({ top: startArry[endIndex][0], left: startArry[endIndex][1] }, speed, function () { $(this).remove() });
n++;
}
})
}

//慢慢增加游戏障碍数量
setInterval("Start()", 1000);
function Start() {
clearInterval(s);
if (count > 10) {
count = count - 5;
}
s = setInterval("add()", count);
}

//判断是否死亡
setInterval("fnDie()", 1);
function fnDie() {
if (flag) {
return false;
}
$(".span-dian").each(function () {
var x = parseInt($(this).css("left"));
var y = parseInt($(this).css("top"));
for (var sss = 0; sss < myCon.length; sss++) {
if (myCon[sss][0] == x && myCon[sss][1] == y) {
flag = true;
console.log(myCon[sss][0] + "," + myCon[sss][1])
console.log(x+ "," + y)
break;
}
}
})
if (flag) {
$(".div-die").html("you are die!");
clearInterval(g);
}
}

//计算成绩
var g = setInterval(function () { grades = grades * 1 + 0.01; $("#span-grade").html(grades.toFixed(2)) }, 10);
function refresh() {
history.go(0);
}
</script>

转载于:https://www.cnblogs.com/-maomao/p/5029233.html

猜你喜欢

转载自blog.csdn.net/weixin_33766805/article/details/93760829