-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
280 lines (264 loc) · 6.28 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>大鱼吃小鱼</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
overflow: hidden;
}
canvas {
background: url(img/bg.jpg)no-repeat center;
background-size: cover;
}
</style>
</head>
<body>
<canvas id="can"></canvas>
<script>
var obj = document.getElementById("can"),
hb = obj.getContext("2d"),
h = obj.height = window.innerHeight,
w = obj.width = window.innerWidth,
my = {
img: [
createImage('img/me.png'),
createImage('img/me_1.png')
],
x: 0,
y: 0,
sw: 50,
sh: 25,
endx:0,
endy:0,
lift:0
},
arr = [],
maxD = 15,//定义 最大15条鱼
Dsmall = [
'img/small_1.png',
'img/small_2.png',
'img/small_3.png',
'img/small_4.png',
'img/small_5.png',
'img/z_1.png',
'img/z_2.png',
'img/z_3.png',
'img/z_4.png',
'img/z_5.png',
'img/big_1.png',
'img/big_2.png',
'img/big_3.png',
]
//设置随机数
function random(max,min){
return Math.random(max - min) + min;
}
//构造函数,创建所有敌人的鱼
function creatfoot(){
}
creatfoot.prototype = {
int:function(){//敌人鱼的初始化参数
var num = Math.floor(Math.random()*Dsmall.length);
switch(num){
case 0://小虾米
this.src = createImage(Dsmall[0]);
this.w = 20;
this.h = 10;
this.sd = 1;
this.lift = 11;
break;
case 1://小鱼一级
this.src = createImage(Dsmall[1]);
this.w = 24;
this.h = 12;
this.sd = 1.2;
this.lift = 10;
break;
case 2://小鱼二级
this.src = createImage(Dsmall[2]);
this.w = 28;
this.h = 14;
this.sd = 1.3;
this.lift = 10;
break;
case 3://小鱼三级
this.src = createImage(Dsmall[3]);
this.w = 30;
this.h = 16;
this.sd = 1.35;
this.lift = 4;
break;
case 4://小鱼四级
this.src = createImage(Dsmall[4]);
this.w = 34;
this.h = 17;
this.sd = 0.9;
this.lift = 5;
break;
case 5://中鱼一级
this.src = createImage(Dsmall[5]);
this.w = 40;
this.h = 22;
this.sd = 0.65;
this.lift = 10;
break;
case 6://中鱼二级
this.src = createImage(Dsmall[6]);
this.w = 46;
this.h = 25;
this.sd = 0.82;
this.lift = 12;
break;
case 7://中鱼三级
this.src = createImage(Dsmall[7]);
this.w = 55;
this.h = 29;
this.sd = 1.5;
this.lift = 16;
break;
case 8://中鱼四级
this.src = createImage(Dsmall[8]);
this.w = 62;
this.h = 33;
this.sd = 1.22;
this.lift = 18;
break;
case 9://中鱼五级
this.src = createImage(Dsmall[9]);
this.w = 70;
this.h = 40;
this.sd = 0.85;
this.lift = 20;
break;
case 10://大鱼一级
this.src = createImage(Dsmall[10]);
this.w = 120;
this.h = 70;
this.sd = 1.44;
this.lift = 25;
break;
case 11://大鱼二级
this.src = createImage(Dsmall[11]);
this.w = 200;
this.h = 100;
this.sd = 0.8;
this.lift = 50;
break;
case 12://大鱼三级
this.src = createImage(Dsmall[12]);
this.w = 230;
this.h = 160;
this.sd = 1.3;
this.lift = 100;
break;
}
this.x = getFx('x',-this.w);
this.y = getFx('y');
this.sdx = this.x <= this.w ? Math.random() * (this.sd - 0.5) + 0.5 : Math.random() * -(this.sd - 0.5) - 0.5;
this.sdy = this.y < h / 2 - 100 ? Math.random() * (this.sd - 0.5) : Math.random() * -(this.sd - 0.5);
},
draw:function(){
this.x += this.sdx;
this.y += this.sdy;
if(this.x >= my.x && this.x + this.w <= my.x + my.sw && this.y >= my.y && this.y + this.h <= my.y + my.sh){
my.lift += this.lift;
this.int();
}
this.pengzhuang();
hb.drawImage(this.src, this.x, this.y, this.w, this.h);
},
pengzhuang:function(){
if(this.x > w || this.x < -this.w){
this.int();
}
}
}
function Dyd(){
for(x in arr){
arr[x].draw();
}
}
//刚出来的敌人鱼的坐标
function getFx(a,b){
if(a == 'x'){
var arr1 = [b,w];
return arr1[Math.floor(Math.random()*2)];
}else{
return Math.random()*h;
}
}
//创建图片对象的方法
function createImage(src) {
var o = new Image();
o.src = src;
return o;
}
//自己鱼的方法
function chi(){
if(my.lift >= 10 && my.lift < 50){
my.sw = 70;
my.sh = 35;
}else if(my.lift >= 50 && my.lift < 100){
my.sw = 100;
my.sh = 50;
}else if(my.lift >= 100 && my.lift < 200){
my.sw = 120;
my.sh = 60;
}else if(my.lift >= 200 && my.lift < 300){
my.sw = 150;
my.sh = 75;
}else if(my.lift >= 300 && my.lift < 400){
my.sw = 200;
my.sh = 100;
}else if(my.lift >= 400 && my.lift < 500){
my.sw = 250;
my.sh = 125;
}
}
//自己鱼移动的方法
document.addEventListener('mousemove',function(e){
my.endx = e.clientX;
my.endy = e.clientY;
},false)
//每个敌人鱼对象的方法
window.onload = function(){
for(var i=0; i<maxD; i++){
(function(j){
setTimeout(function(){
var o = new creatfoot();
o.int();
arr.push(o);
},500 * j)
})(i)
}
//console.log(arr)
}
var timer = setInterval(function(){
hb.clearRect(0,0,w,h);
my.x += (my.endx - my.x)/100;
my.y += (my.endy - my.y)/100;
my.x = Math.min(my.x , w - my.sw);//不超出屏幕宽度
my.y = Math.min(my.y , h - my.sh);//不超出屏幕高度
Dyd()
chi()
if(my.endx - my.x > 0){
hb.drawImage(my.img[1], my.x, my.y, my.sw, my.sh);//向右走
}else{
hb.drawImage(my.img[0], my.x, my.y, my.sw, my.sh);//向左走
}
// if((my.x + my.sw) > w){//不超出屏幕宽度
// my.x = w - my.sw;
// }
// if((my.y + my.sh) > h){//不超出屏幕高度
// my.y = h - my.sh;
// }
},20)
</script>
</body>
</html>