-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.js
165 lines (134 loc) · 3.71 KB
/
main2.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
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
canvas.width = 700;
canvas.height = 700;
var dino_left = new Image();
dino_left.src = './image/among1.png';
var dino_right = new Image();
dino_right.src = './image/among2.png';
var knifeImage = new Image();
knifeImage.src = './image/knife.png';
var backgraoundImage = new Image();
backgraoundImage.src = './image/space1.jpg';
// 주인공
var dino = {
x: 10,
y: 180,
jumpSpeed: 30,
isJumpStart: false,
characterSizeX: 77,
characterSizeY: 90,
// 충돌범위
getRangeX() {
return {
start: this.x,
end: this.x + this.characterSizeX - 15 // 안 닿았는데 자꾸 닿았다고 판정되서 조절
}
},
getRangeY() {
return {
start: this.y - 10, // 머리에 닿았는데 안 닿았다고 인식해서 조절
end: this.y + this.characterSizeY
}
},
draw(f) {
if(this.isJumpStart) {
this.y -= this.jumpSpeed;
this.jumpSpeed -= 2;
if(this.y >= 180) {
this.isJumpStart = false;
this.jumpSpeed = 30;
}
}
if(f % 10 < 5) {
ctx.drawImage(dino_left, this.x, this.y);
// ctx.drawImage(knifeImage, 110, 110);
} else {
ctx.drawImage(dino_right, this.x, this.y);
// ctx.drawImage(knifeImage, 220, 220);
}
},
}
class background {
constructor(x, y) {
this.x = x;
this.y = y;
}
draw() {
ctx.drawImage(backgraoundImage, this.x, this.y);
}
move() {
this.x -= 10;
if(this.x < -700) {
this.x = 700;
}
}
}
class knife {
constructor(x, y) {
this.x = x;
this.y = y;
ctx.drawImage(knifeImage, this.x, this.y);
}
move() {
this.x -= 8;
}
draw() {
ctx.drawImage(knifeImage, this.x, this.y);
}
getX() {
return this.x;
}
getY() {
return this.y;
}
}
let f = 0;
let knifeList = [];
let isGameOver = false;
// 배경 2개
const background1 = new background(0, 0);
const background2 = new background(700, 0);
function frame() {
// 게임오버인 경우 종료
if(isGameOver) {
return;
}
f++;
animation = requestAnimationFrame(frame);
ctx.clearRect(0,0, canvas.width, canvas.height); // 화면 초기화 (이거 안하면 이전 그림들 다 화면에 잔상처럼 남음)
// 배경 그리기
background1.draw();
background2.draw();
background1.move();
background2.move();
// 캐릭터 그리기
dino.draw(f);
// 현재 칼이 없는 경우
if(knifeList.length <= 0) {
let heightRandom = Math.random() * 200 + 1; // y좌표는 랜덤
knifeList.push(new knife(550, heightRandom));
} else {
// 칼이 있는 경우 칼을 그려주고 움직인다
knifeList[0].draw();
knifeList[0].move();
// 칼과 캐릭터가 닿은 경우 게임오버
const isRangeX = dino.getRangeX().start < knifeList[0].getX() && dino.getRangeX().end > knifeList[0].getX();
const isRangeY = dino.getRangeY().start < knifeList[0].getY() && dino.getRangeY().end > knifeList[0].getY();
if(isRangeX && isRangeY) {
isGameOver = true;
console.log("닿았음");
}
// 칼이 화면 끝까지 간 경우 제거
if(knifeList[0].getX() < -30) {
knifeList.pop();
}
}
}
frame();
document.addEventListener('keydown', function(e) {
if (e.code === 'KeyA' && !dino.isJumpStart) {
console.log("점프시작");
dino.isJumpStart = true;
}
})