JSで書いたブロック移動ゲームです.
8256 ワード
<script>
//
var mark = false;
var starttime, endtime, finaltime = 0; //
var array = new Array(); //
var timerarr = new Array(); // id
function move(e){
var xy = document.getElementById("xy");
var self = document.getElementById("self");
x = e.clientX;
y = e.clientY;
xy.value = " :"+x +","+y;
if(x>=454 && x<=530 &&y>=285 && y<=355 && mark==false){
mark = true;
initothers(20);
}
target(e,x,y);
}
// 4px 36px target
function target(e,x,y){
var self = document.getElementById("self");
if(x>=12 && x<=855 && y>=40 &&y<=525 && mark ==true){
self.style.left = x +'px';
self.style.top = y +'px';
}
}
//
function init(){
var today = new Date();
starttime = today.getTime();
document.getElementById("self").style.left = 454+'px';
document.getElementById("self").style.top = 282+'px';
}
//
function piece(){
piece.prototype.id = -1; // 0
piece.prototype.dire = 0; // 1 2 3 4
piece.prototype.top = 0;
piece.prototype.left = 0;
}
// num
function initothers(num){
for(var i=0;i<num;i++){
document.getElementById("pan").innerHTML += "<div id='"+i+"'; style='height:36px;width:36px;background-color:#00FF00;z-index:100; position:absolute;left:-50px'></div>";
//num1 <1 >1 num2 >1 <1
var num1 = Math.random()*2;
var num2 = Math.random()*2;
var top1 = 43; //top1
var top2 = 563; //top2
var left1 = 14; //left1 -->
var left2 = 894; // -->
var p = new piece();
p.id = i;
if(num1<1){
if(num2>1){ //
p.dire = 2;
p.top = top1;
p.left = (Math.ceil(Math.random()*23)*40-26);
document.getElementById(i).style.left = p.left+'px';
document.getElementById(i).style.top = top1+'px';
}else{ //
p.dire = 1;
p.top = top2;
p.left = (Math.ceil(Math.random()*23)*40-26);
document.getElementById(i).style.left = p.left+'px';
document.getElementById(i).style.top = top2+'px';
}
}else{
//
if(num2>1){
p.dire = 4;
p.top = (Math.ceil(Math.random()*14)*40+3);
p.left = left1;
document.getElementById(i).style.left = left1+'px';
document.getElementById(i).style.top = p.top+'px';
}else{ //
p.dire = 3;
p.top = (Math.ceil(Math.random()*14)*40+3);
p.left = left2;
document.getElementById(i).style.left = left2+'px';
document.getElementById(i).style.top = p.top+'px';
}
}
array.push(p);
}
//
var n;
for(n in array){
go(n,array[n].id,array[n].dire);
}
}
// location
function location(timerid,i,n,p){
clearInterval(timerarr[timerid]);
//alert(timerarr[timerid]);
var num1 = Math.random()*2;
var num2 = Math.random()*2;
var top1 = 43;
var top2 = 563;
var left1 = 14;
var left2 = 894;
if(num1<1){
if(num2>1){
p.dire = 2;
p.top = top1;
p.left = (Math.ceil(Math.random()*23)*40-26);
document.getElementById(i).style.left = p.left+'px';
document.getElementById(i).style.top = top1+'px';
}else{
p.dire = 1;
p.top = top2;
p.left = (Math.ceil(Math.random()*23)*40-26);
document.getElementById(i).style.left = p.left+'px';
document.getElementById(i).style.top = top2+'px';
}
}else{
if(num2>1){
p.dire = 4;
p.top = (Math.ceil(Math.random()*14)*40+3);
p.left = left1;
document.getElementById(i).style.left = left1+'px';
document.getElementById(i).style.top = p.top+'px';
}else{
p.dire = 3;
p.top = (Math.ceil(Math.random()*14)*40+3);
p.left = left2;
document.getElementById(i).style.left = left2+'px';
document.getElementById(i).style.top = p.top+'px';
}
}
go(n,p.id,p.dire);
}
//
function go(n,i,dire){
//
var speed = Math.ceil(Math.random()*3)*10+10; //
if(dire==2){
timerarr[i] = setInterval("array["+n+"].top>=565 ? location("+i+","+i+","+n+",array["+n+"]): array["+n+"].top+=8;document.getElementById("+i+").style.top = array["+n+"].top+'px'; check(array["+i+"])",speed);
}
if(dire==1){
timerarr[i] = setInterval("array["+n+"].top<=41 ? location("+i+","+i+","+n+",array["+n+"]): array["+n+"].top-=8;document.getElementById("+i+").style.top = array["+n+"].top+'px';check(array["+i+"])",speed);
}
if(dire==4){
timerarr[i] = setInterval("array["+n+"].left>=896 ? location("+i+","+i+","+n+",array["+n+"]): array["+n+"].left+=8;document.getElementById("+i+").style.left = array["+n+"].left+'px';check(array["+i+"])",speed);
}
if(dire==3){
timerarr[i] = setInterval("array["+n+"].left<12 ? location("+i+","+i+","+n+",array["+n+"]): array["+n+"].left-=8;document.getElementById("+i+").style.left = array["+n+"].left+'px';check(array["+i+"])",speed);
}
}
//
function check(t){
if(t.left<=x+75 && t.left>=x-36 && t.top<=y+75 && t.top>=y-36){
var today = new Date();
endtime = today.getTime();
finaltime = (endtime - starttime - 0) / 1000;
window.alert(' ! ' + finaltime + ' …');
//alert(' '+x+','+y+' '+t.left+','+t.top);
//
document.location.reload();
}
}
</script>
<body onload="init();">
<input id ="xy" disabled=true/>
<!--<button id="self" style="position:relative;">ssss</button> -->
<div id="pan" style=" height: 565; width: 925; background-image:url(http://dl.iteye.com/upload/picture/pic/97696/26d1caeb-c0ca-38e2-87ce-56e862235a79.jpg);background-repeat:no-repeat;" onmousemove="move(event)">
<div id="self" style="height:76px;width:76px; background-color: white; position:absolute;border-width:1px; border-style:solid;border-color: red;"></div>
</div>
</body>