-
Notifications
You must be signed in to change notification settings - Fork 0
/
person.js
256 lines (230 loc) · 7.79 KB
/
person.js
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
/**
* Created by wuguoyuan on 2016/6/22.
*/
function person(option) {
this._init(option);
}
person.prototype = {
_init: function (option) {
//五个键位
this.left = option.left,
this.up = option.up,
this.right = option.right,
this.down = option.down,
this.fireKey = option.fireKey,
this.width = option.width;//绘制小人图片的宽度
this.height = option.height;//绘制小人图片的高度
this.x = option.x;//小人初始化坐标
this.y = option.y;
this.imgSrc = option.imgSrc;//小人图片
this.speed = option.speed;//速度
this.dirNum = 0;//方向标识,即纵向人物图片切换索引
this.step = option.step;//步长
this.stepX = 0;//水平方向已走的路程
this.stepY = 0;//垂直方向已走的路程
this.fImg = option.fImg;//子弹图片
},
render: function (ctx) {
var img = new Image();
var _this = this;
img.src = this.imgSrc;
img.onload = function () {
var index = 0;//是横向人物图片切换索引
var originX = _this.x;
var originY = _this.y;
setInterval(function () {
ctx.beginPath();
ctx.clearRect(_this.x, _this.y, _this.width, _this.height);
_this.x = originX + _this.stepX;
_this.y = originY + _this.stepY;
ctx.drawImage(
img,
_this.width * index,
_this.height * _this.dirNum,
_this.width,
_this.height,
_this.x,
_this.y,
_this.width,
_this.height
);
index++;
index %= 4;
}, 1000 / _this.speed)
}
this.anKey(_this.left,_this.up,_this.right,_this.down,_this.fireKey);
},
anKey: function (a,b,c,d,f) {
var _this = this;
document.addEventListener('keydown',function (e) {
//对于偶尔出现的人物跑出界,是按键延时,图片尺寸和步长的倍数不等于canvas的尺寸
var e = e || window.event;
if (e && (e.keyCode == a)) {
_this.move(1, 'x', 0)
}
if (e && (e.keyCode == b)) {
_this.move(3, 'y', 0)
}
if (e && (e.keyCode == c)) {
_this.move(2, 'x', 600)
}
if (e && (e.keyCode == d)) {
_this.move(0, 'y', 600)
}
if (e && (e.keyCode == f)) {
_this.fire(_this.fImg);
}
})
},
//dirNum方向标识,即纵向人物图片切换索引,flag标记是x轴还是y轴的移动,size是边界的位置,tank表示是坦克移动
move: function (dirNum, flag, size,tank) {
this.dirNum = dirNum;
if (dirNum === 1 || dirNum === 3) {
if (flag === 'x') {
if (this.x <= size) {
console.log(this.x);
if(tank =='tank'){
dirNum++;
}
return;
}
this.stepX -= this.step;
}
if (flag === 'y') {
if (this.y <= size) {
console.log(this.y);
return;
}
this.stepY -= this.step;
}
}
if (dirNum === 0 || dirNum === 2) {
if (flag === 'x') {
if (this.x + this.width >= size) {
console.log(this.x + this.width)
return;
}
this.stepX += this.step;
}
if (flag === 'y') {
if (this.y + this.height >= size) {
console.log(this.y + this.height)
return;
}
this.stepY += this.step;
}
}
},
fire: function (fImg) {
var bullet = new Image();
bullet.src = fImg;
var _this = this;
bullet.onload = function () {
_this.fireMove(bullet, 5);
}
},
fireMove: function (bullet,speed) {
var bulletX = this.x;
var bulletY = this.y;
var _this = this;
var timer;
var k = 0;
var bWidth = bullet.width/4;
var padiingX = (_this.width-bWidth)/2;//子弹与人物x轴的距离
var padiingY = (_this.height-bullet.height)/2;//子弹与人物y轴的距离
if (_this.dirNum === 1) {
timer = setInterval(function () {
ctx.clearRect(bulletX - bWidth, bulletY+padiingY, bWidth, bullet.height);
bulletX -= speed;
ctx.drawImage(
bullet,
(bWidth) * k,
0,
bWidth,
bullet.height,
bulletX - bWidth,
bulletY+padiingY,
bWidth,
bullet.height
);
k++;
k %= 4;
if (bulletX - bWidth <= 0) {
clearInterval(timer);
ctx.clearRect(bulletX - bWidth, bulletY+padiingY, bWidth, bullet.height)
}
}, 10)
}
if(_this.dirNum ===3){
timer = setInterval(function () {
ctx.clearRect(bulletX+padiingX, bulletY-bullet.height, bWidth, bullet.height);
bulletY -= speed;
ctx.drawImage(
bullet,
(bWidth) * k,
0,
bWidth,
bullet.height,
bulletX+padiingX,
bulletY-bullet.height,
bWidth,
bullet.height
);
k++;
k %= 4;
if (bulletY-bullet.height <= 0) {
clearInterval(timer);
ctx.clearRect(bulletX+padiingX, bulletY-bullet.height, bWidth, bullet.height)
}
}, 10)
}
if(_this.dirNum ===2){
timer = setInterval(function () {
ctx.clearRect(bulletX + _this.width, bulletY+padiingY, bWidth, bullet.height);
bulletX += speed;
ctx.drawImage(
bullet,
(bWidth) * k,
0,
bWidth,
bullet.height,
bulletX + _this.width,
bulletY+padiingY,
bWidth,
bullet.height
);
k++;
k %= 4;
if (bulletX + _this.width+bWidth >= 600) {
console.log(bulletX + _this.width+bWidth)
clearInterval(timer);
ctx.clearRect(bulletX + _this.width, bulletY+padiingY, bWidth, bullet.height)
}
}, 10)
}
if(_this.dirNum ===0){
timer = setInterval(function () {
ctx.clearRect(bulletX+padiingX, bulletY+_this.height, bWidth, bullet.height);
bulletY += speed;
ctx.drawImage(
bullet,
(bWidth) * k,
0,
bWidth,
bullet.height,
bulletX+padiingX,
bulletY+_this.height,
bWidth,
bullet.height
);
k++;
k %= 4;
if (bulletY+_this.height+bullet.height >= 600) {
console.log(bulletY + _this.height + bullet.height);
clearInterval(timer);
ctx.clearRect(bulletX+padiingX, bulletY+_this.height, bWidth, bullet.height)
}
}, 10)
}
}
}