-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
494 lines (421 loc) · 16.4 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>연습</title>
<style>
body {
background-color: 'white';
margin: 0px;
}
.alert{
padding-left: 0;
padding-right: 0;
margin-left: auto;
margin-right: auto;
text-align:center;
font-size: medium;
}
#canvas{
background-color: #fbe5d6;
padding-left: 0;
padding-right: 0;
margin-left: auto;
margin-right: auto;
display: block;
}
</style>
</head>
<body>
<div class="alert" id="scoreboard">점수 : <br></div>
<canvas id="canvas" width="600px" height="600px"></canvas>
<div class="alert">Enter:게임시작, Spacebar:각 전환, 화살표방향키:이동</div>
<script>
var window_width = window.innerWidth-70;
var window_height= window.innerHeight-70;
if(window_width<window_height){
document.getElementById('canvas').style.width = window_width+"px";
document.getElementById('canvas').style.height = window_width+"px";
}
else{
document.getElementById('canvas').style.width = window_height+"px";
document.getElementById('canvas').style.height = window_height+"px";
}
</script>
</body>
<script>
//enemy 추가 시에 수정할 부분 : var enemyUnit var enemyProperty
var init_x;
var init_y;
var score;
var countup;
var canvas; // canvas태그의 canvas
var canvasCtx; // canvas의 렌더링 컨텍스트
var canvasBuffer; // 버퍼를 위해 새로 만들 canvas
var bufferCtx; // canvasBuffer의 렌더링 컨텍스트
var threadSpeed = 10; // setInterval 주기
var playerUnit = {};//유저
var shot = []; //총알
var randomlocate;
//이미지 따오기
var spacePlayer;//플레이어 이미지 따오는 변수
var spaceEnemy= [];//적 이미지 따오기 위한 배열
var spaceShot; // 총알 이미지 따오기 위한 객체
//enemy의 설정
var enemyUnit = [[],[],[],[]];//enemy객체를 담을 배열
var enemyProperty = [];
enemyProperty[0] = {
width : 50, height : 50,
speed : 1.2, amount : 200,
apperTime : 0, addTime : 2,
life : 23 , movePattern:2
};
enemyProperty[1] = {
width : 60, height : 60,
speed : 1.5, amount : 100,
apperTime : 10, addTime : 4,
life : 30 , movePattern:2
};
enemyProperty[2] = {
width : 40, height : 40,
speed : 0.5, amount : 50,
apperTime : 15, addTime : 4,
life : 15 , movePattern:1
};
enemyProperty[3] = {
width : 30, height : 30,
speed : 0.8, amount : 50,
apperTime : 25, addTime : 3,
life : 40 , movePattern:1
};
//키
var keyPressOn = {}; //pressed - true
var gameLoopThread; //animation Thread ID
var LoopStarted = false;
var reversed = 1;
var angle;
var distance_x;
var distance_y;
var distance;
window.addEventListener("load", init, false);
function init(){
//최초 1회만 실행되는 함수로 변수 및 객체 초기화, 함수 실행
//score와 countup 초기값 설정
canvas = document.getElementById("canvas");
canvasCtx = canvas.getContext("2d");
canvasBuffer= document.createElement("canvas");
canvasBuffer.width = canvas.width;
canvasBuffer.height = canvas.height;
bufferCtx = canvasBuffer.getContext("2d");
document.addEventListener("keydown", getKeyDown, false);
document.addEventListener("keyup", getKeyUp, false);
setImage();
//Start Message
canvasCtx.fillStyle = "black";
canvasCtx.strokeStyle = "black";
canvasCtx.font = "bold 30px _sans";
canvasCtx.textBaseline = "top";
canvasCtx.fillText("Enter to Start", 210, 250);
canvasCtx.strokeText("Enter to Start", 210, 250);
}
function startGameLoop(){
countup=0;
score=0;
angle=0;
//사용자와 적 유닛 설정
playerUnit = {
x:canvas.width/2 - 18, y:canvas.height/2 -18,
width: 30, height: 30, speed:3,
real_x: 1000, real_y : 1000
};//사용자 유닛
for(i=0; i<360; i++){
shot[i] = {
x:playerUnit.x, y:playerUnit.y,
width: 5, height: 5, speed:5, angle: 0, life:0,
real_x: 5, real_y : 5
};//총알
}
for(i=0;i<enemyUnit.length;i++){
for(j=0;j<enemyProperty[i].amount;j++){
randomlocate=Math.floor(Math.random()*4+1);
switch (randomlocate){
case 1:
init_x = Math.floor(Math.random()*canvas.width+1);
init_y = -150;
break;
case 2:
init_x = canvas.width+150;
init_y = Math.floor(Math.random()*canvas.height+1);
break;
case 3 :
init_x = Math.floor(Math.random()*canvas.width+1);
init_y = canvas.height+150;
break;
case 4 :
init_x = -150;
init_y = Math.floor(Math.random()*canvas.height+1);
break;
default : break;
}//random으로 init좌표 생성
enemyUnit[i][j] = {
x:init_x, y:init_y,
width: enemyProperty[i].width, height: enemyProperty[i].height,
speed:enemyProperty[i].speed, life:0,
real_x:-1000, real_y:-1000
};
}
}//적 유닛
enemyGivelife(0,0,10)//첫번째 적에게 생명력 넣기
//루프돌리기
gameLoopThread = setInterval(gameLoop, threadSpeed);
LoopStarted = true;
}
function stopGameLoop()
{
clearInterval(gameLoopThread);
LoopStarted = false;
}
function setImage(){
//이미지를 따오는 함수
spacePlayer = new Image();
spacePlayer.src = "player.jpg";
spaceShot = new Image();
spaceShot.src = "shot.jpg";
for(i=0;i<enemyUnit.length;i++){
spaceEnemy[i] = new Image();
spaceEnemy[i].src = "enemy"+ i +".jpg";
}
}
function timeflow(){
//시간흐름
countup += 1;
shot[countup%360].angle=shot[(countup-1)%360].angle+4*reversed;
//스코어 기록
document.getElementById("scoreboard").innerHTML="점수 : " + score + "<br>";
for(i=0;i<enemyUnit.length;i++){
for(j=0;j<enemyUnit[i].length;j++){
if (countup>100*enemyProperty[i].apperTime && countup%(enemyProperty[i].addTime*100)==0 && countup/(enemyProperty[i].addTime*100) < enemyUnit[i].length){
enemyGivelife(i,parseInt(countup/(enemyProperty[i].addTime*100)),enemyProperty[i].life);
}//각 enemy의 리젠 시간이 되고, 실제 개수보다 작을 때까지
}
}//enemyUnit에게 생명력 주기
shotGivelife((countup-1)%360)
}
function shotGivelife(i){
shot[i].life += 1;
shot[i].x=playerUnit.real_x;
shot[i].y=playerUnit.real_y;
}
function enemyGivelife(i,j,life){
enemyUnit[i][j].life = life;
}
function gameLoop(){
//실질적으로 반복되는 함수- 다른 함수들을 반복시킴
playerUnit.real_x=playerUnit.x+playerUnit.width/2;
playerUnit.real_y=playerUnit.y+playerUnit.height/2;
for(i=0;i<shot.length;i++){
shot[i].real_x=shot[i].x+shot[i].width/2;
shot[i].real_y=shot[i].y+shot[i].height/2;
}
for(i=0;i<enemyUnit.length;i++){
for(j=0;j<enemyUnit[i].length;j++){
enemyUnit[i][j].real_x=enemyUnit[i][j].x+enemyUnit[i][j].width/2;
enemyUnit[i][j].real_y=enemyUnit[i][j].y+enemyUnit[i][j].height/2;
}
}//실제 x,y좌표값 갱신
timeflow();
calcKeyInnput();
displayAll();
moveEnemy();
moveShot();
}
function moveShot(){
for(i=0;i<shot.length;i++){
if(shot[i].life>0){
shot[i].x += shot[i].speed*Math.cos(Math.PI*shot[i].angle/180);
shot[i].y += shot[i].speed*Math.sin(Math.PI*shot[i].angle/180);
}
}
}
function moveEnemy(){
// player를 따라오는 적의 함수
for(i=0;i<enemyUnit.length;i++){
switch(enemyProperty[i].movePattern){
case 1 :
for(j=0;j<enemyUnit[i].length;j++){
if(enemyUnit[i][j].life > 0){
if(playerUnit.real_x > enemyUnit[i][j].real_x)
enemyUnit[i][j].x += enemyUnit[i][j].speed/Math.sqrt(2);
else
enemyUnit[i][j].x -= enemyUnit[i][j].speed/Math.sqrt(2);
if(playerUnit.real_y >enemyUnit[i][j].real_y)
enemyUnit[i][j].y += enemyUnit[i][j].speed/Math.sqrt(2);
else
enemyUnit[i][j].y -= enemyUnit[i][j].speed/Math.sqrt(2);
}
}
break;
case 2 :
for(j=0;j<enemyUnit[i].length;j++){
if(enemyUnit[i][j].life > 0){
distance_x = enemyUnit[i][j].real_x - playerUnit.real_x;
distance_y = enemyUnit[i][j].real_y - playerUnit.real_y;
distance = Math.sqrt(Math.pow(distance_x,2) + Math.pow(distance_y,2));
enemyUnit[i][j].x -= (distance_x/distance)*enemyUnit[i][j].speed;
enemyUnit[i][j].y -= (distance_y/distance)*enemyUnit[i][j].speed;
}
}
break;
default :
break;
}
}
}
function displayAll(){
bufferCtx.fillStyle = "#fbe5d6";
bufferCtx.fillRect(0, 0, canvas.width, canvas.height);
displayShot()
displayPlayer();
displayEnemy();;
checkHitPlayer();
canvasCtx.drawImage(canvasBuffer, 0, 0);
if(!LoopStarted){
canvasCtx.fillStyle = "rgba(0,0,0,0.2)";
canvasCtx.fillRect(210,250,231,34);
canvasCtx.fillStyle="#ffffff";
canvasCtx.strokeStyle="#bb8d62";
canvasCtx.font = "bold 30px _sans";
canvasCtx.textBaseline = "top"
canvasCtx.fillText("Enter to Restart", 211, 250);
canvasCtx.strokeText("Enter to Restart", 211, 250);
canvasCtx.fillStyle = "rgba(255, 255, 255, 0.4)";
canvasCtx.fillRect(210, 285, 231, 14);
canvasCtx.fillStyle="#000000";
canvasCtx.font="bold 12px _sans";
canvasCtx.textBaseline="top";
canvasCtx.fillText("Your Record : " + score,212,285);
}
}
function displayShot(){
for(i=0;i<shot.length;i++){
if(shot[i].life>0){
bufferCtx.drawImage(spaceShot,
shot[i].x, shot[i].y,
shot[i].width, shot[i].height
);
if(shot[i].real_x<-50||shot[i].real_x>canvas.width+50||shot[i].real_y<-50||shot[i].real_y>canvas.height+50){
shot[i].life=0;
shot[i].x=100;
shot[i].y=100;
}
}
}
checkHitShot();
}
function checkHitShot(){
for(k=0;k<shot.length;k++){
if(shot[k].life>0){
for(i=0;i<enemyUnit.length;i++){
for(j=0;j<enemyUnit[i].length;j++){
if(enemyUnit[i][j].life>0){
if(shot[k].width/2 + enemyUnit[i][j].width/2 -1 > (Math.sqrt(Math.pow(shot[k].x - enemyUnit[i][j].real_x,2) + Math.pow(shot[k].y - enemyUnit[i][j].real_y,2)))){
shot[k].life=0;
shot[k].x=100;
shot[k].y=100;
enemyUnit[i][j].life-=1;
if(enemyUnit[i][j].life ==0){
score+=1;
}
return true;
}
}
}
}
}
}
return false;
}
function displayPlayer(){
bufferCtx.drawImage(spacePlayer, //Source Image
playerUnit.x, playerUnit.y, //View Position
playerUnit.width, playerUnit.height //View Size
);
if(checkHitPlayer())
stopGameLoop();
}
function displayEnemy(){
for(i=0;i<enemyUnit.length;i++){
for(j=0; j<enemyUnit[i].length; j++){
if(enemyUnit[i][j].life > 0 ){
bufferCtx.drawImage(spaceEnemy[i], //Source Image
enemyUnit[i][j].x, enemyUnit[i][j].y, //View Position
enemyUnit[i][j].width, enemyUnit[i][j].height //View Size
);
}
}
}
}
//맞았는지 확인
function checkHitPlayer(){
for(i=0;i<enemyUnit.length;i++){
for(j=0;j<enemyUnit[i].length;j++){
if(enemyUnit[i][j].life>0){
if(playerUnit.width/2 + enemyUnit[i][j].width/2 -5 > (Math.sqrt(Math.pow(playerUnit.real_x - enemyUnit[i][j].real_x,2) + Math.pow(playerUnit.real_y - enemyUnit[i][j].real_y,2)))){
return true;
}
}
}
}
return false;
}
//방향키
function getKeyDown(event){
var keyValue;
if(event == null){
keyValue=window.event.keyCode;
window.event.preventDefault();
}
else{
keyValue=event.keyCode;
event.preventDefault();
}
if(keyValue == "87") keyValue = "38"; //up
else if(keyValue == "83") keyValue = "40"; //down
else if(keyValue == "65") keyValue = "37"; //left
else if(keyValue == "68") keyValue = "39"; //right
keyPressOn[keyValue] = true;
}
function getKeyUp(event){
var keyValue;
if(event == null){
keyValue = window.event.keyCode;
window.event.preventDefault();
}
else{
keyValue = event.keyCode;
event.preventDefault();
}
if(keyValue == "87") keyValue = "38"; //up
else if(keyValue == "83") keyValue = "40"; //down
else if(keyValue == "65") keyValue = "37"; //left
else if(keyValue == "68") keyValue = "39"; //right
keyPressOn[keyValue] = false;
//enter추가
if(keyValue == "13" && !LoopStarted)
startGameLoop();
if(keyValue == "32" && LoopStarted){
reversed *= -1;
}
}
function calcKeyInnput(){
if(keyPressOn["38"] && playerUnit.y >= -playerUnit.height/2)
playerUnit.y -= playerUnit.speed; //up
if(keyPressOn["40"] && playerUnit.y <= canvas.height -playerUnit.height/2)
playerUnit.y += playerUnit.speed; //down
if(keyPressOn["37"] && playerUnit.x >= -playerUnit.width/2)
playerUnit.x -= playerUnit.speed; //left
if(keyPressOn["39"] && playerUnit.x <= canvas.width -playerUnit.width/2)
playerUnit.x += playerUnit.speed; //right
}
</script>
</html>