-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
126 lines (117 loc) · 2.62 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<link rel="stylesheet" href="">
<style>
body{
text-align: center;
}
#c1{
background: #000;
cursor:pointer;
}
#begin{
position: absolute;
top:30%;
left:50%;
}
</style>
</head>
<body>
<div id="container">
<canvas id="c1"></canvas>
<div>
<h2>计时</h2>
<p>
<span id="counter">0</span>
秒</p>
</div>
<div id="begin">
<button>开始</button>
</div>
</div>
<script>
//设置主页面的宽、高、位置
var cw=500,ch=500;
c1.height=ch;
c1.width=cw;
//设置全局变量
var ctx=c1.getContext("2d");
var mx,my;//鼠标移动位置
var bullet=[],bn=50,r=2;//子弹数据
var pw=14,ph=14;//飞机尺寸
var gameOver=false;
c1.onmousemove=function(e){
mx=e.offsetX;
my=e.offsetY;
if(mx<=0||mx>=cw||my<=0||my>=ch){
gameOver=true;
}
}
//设置子弹起始位置
function bulletCreater(){
for(var i=0;i<bn;){
var bpx=Math.round(Math.random()*cw);
var bpy=Math.round(Math.random()*ch);
if(
!((bpx>=cw/5&&bpx<=cw*4/5)&&(bpy>=ch/5&&bpy<=ch*4/5))
){
bullet[i]={};
bullet[i].x=bpx;
bullet[i].y=bpy;
if(bpx<=cw/2){bullet[i].dx=1;}
if(bpx>=cw/2){bullet[i].dx=-1;}
if(bpy<=ch/2){bullet[i].dy=1;}
if(bpy>=ch/2){bullet[i].dy=-1;}
i++;
}
}
}
//设置子弹运动
function bulletMove(){
for(var i=0;i<bullet.length;i++){
ctx.beginPath();
ctx.arc(bullet[i].x,bullet[i].y,r,0,360*Math.PI/180);
ctx.fillStyle="yellow";
ctx.fill();
ctx.closePath();
bullet[i].x+=bullet[i].dx*(Math.random()<0.5?2:3);
bullet[i].y+=bullet[i].dy*(Math.random()<0.5?2:3);
var ex=mx,ey=my;
if(
((bullet[i].x>=(ex))&&(bullet[i].x<=(ex+pw)))
&&
((bullet[i].y>=(ey))&&(bullet[i].y<=(ey+ph)))
){
gameOver=true;
begin.style.display="block";
}
if(bullet[i].x<=0||bullet[i].x>=cw){bullet[i].dx=-bullet[i].dx;}
if(bullet[i].y<=0||bullet[i].y>=ch){bullet[i].dy=-bullet[i].dy;}
}
}
function gameStart(){
gameOver=false;
begin.style.display="none";
bulletCreater();
var cn=0;
var t=setInterval(function(){
ctx.clearRect(0,0,cw,ch);
bulletMove();
counter.innerHTML=Math.floor(cn);
cn+=20/1000;
if(gameOver===true){
clearInterval(t);
t=null;
}
},20);
}
begin.onclick=function(){
gameStart();
}
</script>
</body>
</html>