分享一个以前写的小游戏,纯js游戏,代码很简单。欢迎大家来喷呦!
效果图:
代码展示://直接复制到html文件即可 支持IE9+版本
<!DOCTYPE html>
<html>
<head>
<meta charset="{CHARSET}">
<title>Best Fly Car</title>
<style>
input {
border: 1px solid yellow;
margin-left: 20px;
margin-top: 20px;
} .way-content {
margin-left: auto;
margin-right: auto;
width: 1024px;
height: 700px;
position: relative;
border: 1px solid burlywood;
background-color: darkolivegreen;
} .car {
color:black;
width: 20px;
height: 20px;
border: 1px solid brown;
background: greenyellow;
position: absolute;
left: 502px;
top: 678px;
text-align: center;
z-index: ;
} .C-car {
color:black;
width: 20px;
height: 15px;
border: 1px solid brown;
background: red;
position: absolute;
left: 502px;
top: 0px;
text-align: center;
z-index: ;
} .way-grid {
margin: ;
margin-left: 412px;
background-color: grey;
border: none;
border-right: 1px solid brown;
border-left: 1px solid brown;
min-width: 100px;
max-width: 1024px;
width: 200px;
height: 10px;
color: white;
font-size: 5px;
position: relative;
text-align: center;
} .go {
background: greenyellow;
width: 100px;
height: 50px;
text-align: center;
}
</style>
</head>
<body>
<div id="way-content" class="way-content">
<div id="car" class="car">++</div>
</div>
<br /> The width of the road:<input id="wayWhite" type="number" value="" /> <br /> Keyboard sensitivity: <input type="number" id="LMD" value="" /><br /> The game difficulty:<input type="number" id="ND" value="" /><br />
<input class="go" id="go" type="button" value="go" /><br />
<script type="text/javascript">
var wayLeft = [],
cO = null,
LC = null,
RC = null,
st = null,
clickLR = false,
s = null,
LMD = ,
wayWhite = ,
ND = , //n*2+1;
NDArry = [, , -],
gridStr = 'way-grid-',
PX = 'px',
length = ,
NDIndex = ,
waysetIndex = ,
waysetValue = ( - wayWhite) / ,
delvalue = ,
n = ,
waitvalue = / n,
checkOut = function () {
var index = wayLeft[wayLeft.length - ].wayLeft,
CCarvalue = wayLeft[wayLeft.length - ].CCarvalue + index,
carIndex = parseInt(document.getElementById('car').offsetLeft) + ;
if (carIndex < index + wayWhite && carIndex > index && (CCarvalue == || !(CCarvalue < carIndex && CCarvalue + > carIndex))) {
return true;
} else {
clearInterval(st);
clearInterval(cO);
clearInterval(LC);
clearInterval(RC);
var e = new Date();
alert('totle :' + parseInt((e.getTime() - s.getTime()) / ) + 's');
document.onkeydown = null;
document.onkeyup = null;
clearInterval(cO);
clearInterval(LC);
clearInterval(RC);
}
},
//初始道路坐标
getFitstArray = function () {
waysetValue = ( - wayWhite) / ;
for (var i = ; i < length; i++) {
wayLeft[i] = {};
wayLeft[i].wayLeft = ( - wayWhite) / ;
}
},
//初始化道路
setWay = function () {
var docElem = document.createDocumentFragment();
for (var i = ; i < length; i++) {
var grid = document.createElement('div');
grid.className = 'way-grid';
grid.id = gridStr + i;
grid.textContent = '|';
docElem.appendChild(grid);
}
document.getElementById('way-content').appendChild(docElem); },
//生成下一个道路
getNextL = function (firstL) {
var CCarvalue = ;
//渐变道路
if (waysetIndex > ) {
waysetIndex -= ;
waysetValue = firstL + delvalue;
} else if (waitvalue > ) {
//直线缓冲道路
waitvalue--;
waysetValue = firstL;
} else {
//渐变规则
delvalue = NDArry[parseInt(Math.random() * )] * parseInt(n / + );
CCarvalue = parseInt(Math.random() * wayWhite);
waysetIndex = NDIndex;
waitvalue = / n;
waysetValue = firstL + delvalue;
} return waysetValue >= && waysetValue <= ( - wayWhite) ? { wayLeft: waysetValue, CCarvalue: CCarvalue } : { wayLeft: firstL, CCarvalue: CCarvalue };
},
//根据数组刷新道路
getWayByArry = function () {
var CCarvalueList = document.getElementsByClassName('C-car');
while (CCarvalueList[]) {
CCarvalueList[].remove();
}
for (var i = ; i < wayLeft.length; i++) {
var grid = document.getElementById(gridStr + i);
grid.style['width'] = wayWhite + PX;
grid.style['margin-left'] = wayLeft[i].wayLeft + PX;
if (wayLeft[i].CCarvalue) {
var CCarvalue = document.createElement('div');
CCarvalue.id = 'CCarvalue' + i;
CCarvalue.className = 'C-car';
CCarvalue.textContent = '+';
CCarvalue.style['left'] = wayLeft[i].CCarvalue + PX;
grid.appendChild(CCarvalue);
}
}
},
//左键事件
lClick = function () {
if (document.onkeydown) {
var a = document.getElementById('car')
a.style['left'] = (a.offsetLeft - LMD) + PX;
}
},
//右键事件
rClick = function () {
if (document.onkeydown) {
var a = document.getElementById('car')
a.style['left'] = (a.offsetLeft + LMD) + PX;
}
},
//确定事件
goClick = function () {
clearInterval(st);
clearInterval(cO);
clearInterval(LC);
clearInterval(RC);
document.onkeydown = null;
document.onkeyup = null;
LMD = parseInt(document.getElementById('LMD').value) / ;
wayWhite = parseInt(document.getElementById('wayWhite').value);
ND = parseInt(document.getElementById('ND').value) * + ;
NDIndex = ;
getFitstArray();
getWayByArry();
s = new Date();
document.onkeydown = function (evt) {
var evt = window.event ? window.event : evt;
if (clickLR) { } else if (evt.keyCode == ) {
//锁定当前按键
clickLR = true;
LC = setInterval(lClick, );
} else if (evt.keyCode == ) {
//锁定当前按键
clickLR = true;
RC = setInterval(rClick, );
}
};
document.onkeyup = function (evt) {
//清除锁定
clickLR = false;
clearInterval(LC);
clearInterval(RC);
}
document.getElementById('car').style['left'] = + PX;
st = setInterval(stratBC, / ND);
cO = setInterval(checkOut, );
},
stratBC = function () {
setWayArray();
getWayByArry();
},
setWayArray = function () {
var firstL = wayLeft[],
nextL = ( - wayWhite) / ;
nextL = getNextL(firstL.wayLeft);
for (var i = ; i < wayLeft.length; i++) {
firstL = wayLeft[i];
wayLeft[i] = nextL;
nextL = firstL;
}
};
getFitstArray();
setWay();
getWayByArry();
document.getElementById('go').onclick = goClick;
</script>
</body>
</html>
爱情就是他妈这么个过程:
确定喜欢的类型是需求分析;
初步接触是概要设计,深入接触是详细设计;
确定恋爱关系是编写代码;
同居就是测试;
吵架就是fix bug;
结婚就是上线;
生孩子就是项目升级;
离婚就是他妈的项目下线。