-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.min.html
15 lines (15 loc) · 7.94 KB
/
index.min.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!doctype html>
<head>
<title>
Event Horizon
</title>
</head>
<body style="background:black;text-align:center;color:white">
<canvas id="c"></canvas>
<br />
Player vs Player<input type="radio" name="ai" value="0" checked>
Player vs AI<input type="radio" name="ai" value="1" id="ai">
<script>
names=["Red","Blue"];tile=24;gamestate=0;a=new Image();a.src="a.png";context=c.getContext("2d");context.imageSmoothingEnabled=false;c.width=context.width=720;c.height=context.height=480;map=[];dead=-1;truetimer=0;timer=0;setInterval(function(){switch(gamestate){case 0:timer++;context.fillStyle="black";context.globalAlpha=0.3;context.fillRect(0,0,720,480);context.font='100px Verdana, Geneva, sans-serif';context.textBaseline='top';context.textAlign="center";context.lineWidth=30;for(i=0;i<30;i++){for(j=0;j<20;j++){context.globalAlpha=Math.abs((10-timer+i+j)%30)/30;context.drawImage(a,0,(7*i+5*j+50)%(tile*2),tile,tile,i*24,j*24,24,24);context.globalAlpha=1}}var gradient=context.createLinearGradient(0,100,0,400);gradient.addColorStop("0","#00214f");gradient.addColorStop(Math.abs(5-(timer+5)%10)/10,"#252525");gradient.addColorStop("1.0","#7200ad");context.strokeStyle=gradient;context.strokeText('Event',350+Math.round(Math.random()*14-7),50+Math.round(Math.random()*14-7));context.strokeText('horizon',350+Math.round(Math.random()*14-7),200+Math.round(Math.random()*14-7));context.lineWidth=8;var gradient=context.createLinearGradient(0,100,0,400);gradient.addColorStop("0","#006ac0");gradient.addColorStop(Math.abs(5-timer%10)/10,"#252525");gradient.addColorStop("1.0","#2e0251");context.strokeStyle=gradient;context.strokeText('Event',350+Math.round(Math.random()*2-1),50+Math.round(Math.random()*2-1));context.strokeText('horizon',350+Math.round(Math.random()*2-1),200+Math.round(Math.random()*2-1));context.fillStyle="white";context.fillText('Event',350+Math.round(Math.random()*14-7),50+Math.round(Math.random()*14-7));context.fillText('horizon',350+Math.round(Math.random()*14-7),200+Math.round(Math.random()*14-7));break;case 2:timer++;context.fillStyle="black";context.globalAlpha=0.3;context.fillRect(0,0,720,480);context.font='100px Verdana, Geneva, sans-serif';context.textBaseline='top';context.textAlign="center";context.lineWidth=30;for(i=0;i<30;i++){for(j=0;j<20;j++){context.globalAlpha=Math.abs((10-timer+i+j)%30)/30;context.drawImage(a,0,(7*i+5*j+50)%(tile*2),tile,tile,i*24,j*24,24,24);context.globalAlpha=1}}var gradient=context.createLinearGradient(0,100,0,400);gradient.addColorStop("0",winner);gradient.addColorStop(Math.abs(5-(timer+5)%10)/10,"#252525");gradient.addColorStop("1.0",winner);context.strokeStyle=gradient;context.strokeText(winner,350+Math.round(Math.random()*14-7),50+Math.round(Math.random()*14-7));context.strokeText('wins!',350+Math.round(Math.random()*14-7),200+Math.round(Math.random()*14-7));context.lineWidth=8;var gradient=context.createLinearGradient(0,100,0,400);gradient.addColorStop("0","#555");gradient.addColorStop(Math.abs(5-timer%10)/10,"#252525");gradient.addColorStop("1.0","#555");context.strokeStyle=gradient;context.strokeText(winner,350+Math.round(Math.random()*2-1),50+Math.round(Math.random()*2-1));context.strokeText('wins!',350+Math.round(Math.random()*2-1),200+Math.round(Math.random()*2-1));context.fillStyle="white";context.fillText(winner,350+Math.round(Math.random()*14-7),50+Math.round(Math.random()*14-7));context.fillText('wins!',350+Math.round(Math.random()*14-7),200+Math.round(Math.random()*14-7));break;case 1:truetimer+=0.5;timer=Math.round(truetimer);context.fillStyle="black";context.fillRect(0,0,720,480);for(i=0;i<30;i++){for(j=0;j<20;j++){switch(map[i][j]){case 0:if(currentPlayer==0){context.globalAlpha=Math.abs((10-timer+i+j)%20)/30+0.2}else{context.globalAlpha=Math.abs((10-timer-i+j)%20)/30+0.2}context.drawImage(a,0,(7*i+5*j+50)%(tile*2),tile,tile,i*24,j*24,24,24);context.globalAlpha=1;break;case 1:context.drawImage(a,tile,0,tile,tile,i*24,j*24,24,24);break;case 2:context.drawImage(a,tile*4,tile*(timer%2),tile,tile,i*24,j*24,24,24);break}}}context.drawImage(a,tile,tile,tile,tile,laser.x*24,laser.y*24,24,24);if(timer%4>2&&dead==-1){context.drawImage(a,tile,2*tile,tile,tile,players[currentPlayer].x*24,players[currentPlayer].y*24,24,24)}players.forEach(function(x){if(dead==x.id){return}context.save();context.translate(x.x*24+10,x.y*24+10);context.rotate(x.r);context.drawImage(a,tile*2+tile*x.id,tile*((x.id==currentPlayer)*(1+timer%2)),tile,tile,-10,-10,24,24);context.restore()});if(dead!=-1){context.drawImage(a,4*tile,2*tile,tile,tile,players[dead].x*24,players[dead].y*24,24,24)}break}},100);currentPlayer=0;function init(){dead=-1;for(i=0;i<30;i++){map[i]=[];for(j=0;j<20;j++){map[i][j]=Math.random()<0.2?1:0;map[i][j]=Math.random()<0.01?2:map[i][j]}}players=[{x:5,y:5,id:0,r:Math.PI/2},{x:25,y:15,id:1,r:-Math.PI/2}];players.forEach(function(x){map[x.x][x.y]=0});laser={x:100,y:100,vx:0,vy:0};input=false;gamestate=1}function tryMove(x,y){if(!input&&!(x==0&&y==0)){input=true;movePlayer(x,y,currentPlayer,true)}}function tryShoot(){if(!input){laser.x=players[currentPlayer].x;laser.y=players[currentPlayer].y;laser.vx=Math.sin(players[currentPlayer].r);laser.vy=-Math.cos(players[currentPlayer].r);input=true;moveProj()}}input=false;window.addEventListener("click",function(e){switch(gamestate){case 0:init();break;case 1:x=0;y=0;if(e.clientX/window.innerWidth<0.2){x=-1}if(e.clientX/window.innerWidth>0.8){x=1}if(e.clientY/window.innerHeight<0.2){y=-1;x=0}if(e.clientY/window.innerHeight>0.8){y=1;x=0}if(x==0&&y==0){tryShoot()}else{tryMove(x,y)}break;case 2:gamestate=0;break}});window.addEventListener("keydown",function(e){switch(gamestate){case 0:init();break;case 2:gamestate=0;break;case 1:x=0;y=0;x=e.keyCode==37?x-1:x;x=e.keyCode==39?x+1:x;y=e.keyCode==38?y-1:y;y=e.keyCode==40?y+1:y;tryMove(x,y);if(e.keyCode==32){tryShoot()}break}});function movePlayer(x,y,i,isfirst){players[i].r=Math.atan2(y,x)+Math.PI/2;if(map[players[i].x+x]&&map[players[i].x+x][players[i].y+y]==0&&!(players[i].y+y==players[(i+1)%2].y&&players[i].x+x==players[(i+1)%2].x)){ox=players[i].x;oy=players[i].y;players[i].x+=x;players[i].y+=y;if(map[ox+x][oy+y]==2){t=x;x=y;y=t}if(map[ox-y][oy-x]==2){t=x;x=-y;y=-t}setTimeout(movePlayer,200,x,y,i)}else if(map[players[i].x+x]&&map[players[i].x+x][players[i].y+y]==2){dead=i;winner=names[(i+1)%2];setTimeout(function(){gamestate=2},1000)}else{if(!isfirst){next()}else{input=false;return-1}}}function moveProj(){ox=laser.x;oy=laser.y;laser.x+=laser.vx;laser.y+=laser.vy;players.forEach(function(x){if(laser.y==x.y&&laser.x==x.x){winner=names[(x.id+1)%2];dead=x.id;setTimeout(function(){gamestate=2},1000)}});if(map[laser.x]&&map[laser.x][laser.y]==0){if(map[ox+laser.vy][oy+laser.vx]==2){t=laser.vx;laser.vx=laser.vy;laser.vy=t;do{i=Math.floor(Math.random()*30);j=Math.floor(Math.random()*20)}while(map[i][j]==2);map[i][j]=2}if(map[ox-laser.vy][oy-laser.vx]==2){t=laser.vx;laser.vx=-laser.vy;laser.vy=-t;do{i=Math.floor(Math.random()*30);j=Math.floor(Math.random()*20)}while(map[i][j]==2);map[i][j]=2}setTimeout(moveProj,100)}else{if(map[laser.x]&&map[laser.x][laser.y]==1){map[laser.x][laser.y]=0}if(map[laser.x]&&map[laser.x][laser.y]==2){do{i=Math.floor(Math.random()*30);j=Math.floor(Math.random()*20)}while(map[i][j]!=2);laser.x=i;laser.y=j;setTimeout(moveProj,500)}else{laser.x=laser.y=100;laser.vx+=laser.vy=0;next()}}}lastx=-1;lasty=-1;function AI(){if(currentPlayer==1&&document.getElementById("ai").checked){if(lastx==players[1].x&&players[1].y==lasty){laser.x=players[1].x;laser.y=players[1].y;laser.vx=Math.sin(players[1].r);laser.vy=-Math.cos(players[1].r);moveProj();lastx=-1;lasty=-1;return}lastx=players[1].x;lasty=players[1].y;x=0;y=0;if(players[1].y>players[0].y){y=-1}else{y=1}if(Math.abs(players[1].x-players[0].x)>5){if(players[1].x>players[0].x){x=-1;y=0}else{x=1;y=0}}while(movePlayer(x,y,currentPlayer)==-1){}}}function next(){currentPlayer=(currentPlayer+1)%2;if(dead==-1){AI()}setTimeout(function(){input=false},100);do{i=Math.floor(Math.random()*30);j=Math.floor(Math.random()*20)}while(map[i][j]==2);map[i][j]=2}
</script>
</body>