-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
226 lines (173 loc) · 5.23 KB
/
main.js
File metadata and controls
226 lines (173 loc) · 5.23 KB
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
import { Mario, Background, Cactus, Rocket, Coin } from './Character.js';
import { collispeCactus, collispeRocket, collispeCoin } from './Action.js';
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
canvas.width = window.innerWidth-50;
canvas.height = window.innerHeight-50;
let mario = new Mario();
let timer =0;
let Jumptimer =0;
let Jumping = false;
let score =0;
let cactuses =[];
let rockets=[];
let coins =[];
let backgrounds =[];
var animation;
let coinScore =0;
let GameOverCondition = false;
let HiScore = 0;
let Downing = false;
let speed =3; //기본속도
let jumpspeed =4;
let Jumptimerspeed = 25;
const inner = document.getElementById('score');
const innerCoin = document.getElementById('coin');
const innerHi = document.getElementById('Hi');
document.addEventListener('keydown' , function(e){
if(e.code === 'Space' && mario.dis == false){
Jumping = true;
}
else if(e.code === 'ArrowUp' && mario.dis == false){
Jumping = true;
}
else if(e.code === 'ArrowDown'){
Downing = true;
}
});
document.addEventListener('keyup' , function(e){
if(e.code === 'ArrowDown'){
Downing = false;
}
});
function frame(){
ctx.clearRect(0,0,canvas.width, canvas.height);
animation = requestAnimationFrame(frame);
timer++;
inner.innerText = `score: ${(score++)+coinScore*2}`; //점수 생성
innerCoin.innerText =`Coin: ${coinScore}`; //코인생성
innerHi.innerText = `Hi: ${HiScore}`; //최고점수
if(timer %2000 ==0){
speed +=1;
jumpspeed +=1;
Jumptimerspeed -= 7;
}
let termCactus = Math.floor(Math.random() * 300) + 250;
let termMario = 3; //마리오 생성
let termRocket = Math.floor(Math.random() * 600) + 200;
let termCoin = Math.floor(Math.random() * 100) + 100;
if(timer % termCoin ===0){
let coin = new Coin();
coins.push(coin);
}
while(termRocket - termCactus < 200 || termRocket - termCoin < 200){
termRocket = Math.floor(Math.random() * 600) + 200;
}
if(timer % termRocket ===0){
let rocket = new Rocket();
rockets.push(rocket);
}
if(timer % termCactus === 0){
let cactus = new Cactus(); //120프레임마다 장애물 생성하고 array에 집어 넣음
cactuses.push(cactus);
}
let termBack = 158;
if(timer % termBack === 0){
let background = new Background(); //120프레임마다 배경 생성하고 array에 집어 넣음
backgrounds.push(background);
}
backgrounds.forEach((a) =>{
a.x-=speed;
a.draw();
})
cactuses.forEach((a, i, o) => {
if(a.x <0){
o.splice(i, 1);
}
a.x-=speed;
if(collispeCactus(mario, a, ctx, animation, canvas)){
GameOverCondition = true;
o.splice(i, 1);
}
a.draw(); // array에 있던거 다 그림
})
rockets.forEach((a, i, o) => {
if(a.x <0){
o.splice(i, 1);
}
a.x-=speed;
if(collispeRocket(mario, a, ctx, animation, canvas)){
GameOverCondition = true;
o.splice(i, 1);
}
a.draw(); // array에 있던거 다 그림
})
coins.forEach((a, i, o) => {
if(a.x <0){
o.splice(i, 1);
}
a.x-=speed;
coinScore = collispeCoin(mario, a, ctx, coinScore);
a.draw(); // array에 있던거 다 그림
})
if(Jumping == true ){
mario.y-=jumpspeed;
Jumptimer++;
mario.drawJump();
mario.dis = true;
}
if(Jumping == false){
if(mario.y <560){
mario.y+=jumpspeed;
mario.dis = true; //뛰고 있을때는 점프안됨
mario.drawJump();
}
else{
mario.dis = false;
}
}
if(Downing == true && mario.dis == false){
mario.height =40;
mario.y =570;
}
if(Downing == false && mario.dis == false){
mario.height =50;
mario.y =560;
}
if(Jumptimer >Jumptimerspeed){
Jumping =false;
Jumptimer =0;
}
if(timer %2 ===0 && timer % termMario ==0 && mario.dis == false){
mario.drawLeft();
}
else{
mario.drawRight();
}
if( GameOverCondition){
cancelAnimationFrame(animation);
if (confirm("Game Over! Do you want to restart?")) {
if(score > HiScore){
HiScore = score; ///최고 점수 갱신
}
speed =3; //기본속도
jumpspeed =4;
Jumptimerspeed = 25;
timer = 0;
Jumptimer = 0;
Jumping = false;
score = 0;
cactuses = [];
rockets = [];
coins = [];
coinScore = 0;
GameOverCondition = false;
// Restart the game loop
animation = requestAnimationFrame(frame);
} else {
// If the user chooses not to restart, you can perform other actions or end the game.
alert("Thanks for playing!");
}
}
}
frame();