This repository has been archived by the owner on Jun 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index test.html
414 lines (373 loc) · 10.3 KB
/
index test.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
<canvas id="sonic"></canvas>
<script>
//Taille du Canvas
let canvasWidth = 1200;
let canvasHeight = 600;
//Taille du sprite
let spriteWidth = 810;
let spriteHeight = 1080;
//Structure du sprite
let rows = 9;
let cols = 9;
//Taille/structure Sprite
let width = spriteWidth/cols;
let height = spriteHeight/rows;
//Premiere image
let curFrame = 0;
//Total images
let frameCount = 9;
//Coordonées sprite
let x=400;
let y=350;
//x and y coordinates of the canvas to get the single frame
let srcX= 0;
let srcY= 0;
//Coordonnées background
let bX = 0;
let bY = 1080;
let tBX = bX;
let tBY = bY;
//sensibilité du saut
let oneJump = true;
let canvas = document.getElementById('sonic');
//Propriété du Canvas
canvas.width = canvasWidth;
canvas.height = canvasHeight;
let background = new Image();
background.src = "./images/maptest.png";
let ctx = canvas.getContext("2d");
//Creation de Sonic
let sonic = new Image();
sonic.src = "./images/sonic.png";
//Sprite Ring
let ring = new Image();
ring.src = "./images/ring.png";
let collisionRing = true;
let ringSound;
let ringWidth = 1020;
let ringHeight = 65;
let ringRows = 1;
let ringCols = 16;
let rWidth = ringWidth/ringCols;
let rHeight = ringHeight/ringRows;
let ringCurFrame = 0;
let ringFrameCount = 16;
let ringX=1200;
let ringY=370;
let ringSrcX= 0;
let ringSrcY= 0;
//Sprite DR.Robotnik
let robotnik = new Image();
robotnik.src = "./images/dr-robotnik1.png";
let robotnikWidth = 1320;
let robotnikHeight = 170;
let robotnikRows = 1;
let robotnikCols = 6;
let rkWidth = robotnikWidth/robotnikCols;
let rkHeight = robotnikHeight/robotnikRows;
let robotnikCurFrame = 0;
let robotnikFrameCount = 6;
let robotnikX=900;
let robotnikY=200;
let robotnikSrcX= 0;
let robotnikSrcY= 90;
let posRobotnik = true
let toursRobotnik = 0;
function updateFrame(){
//Rafraichissement
curFrame = ++curFrame % frameCount;
//Calcul coordonnées du sprite
srcX = curFrame * width;
ctx.clearRect(x,y,width,height);
robotnikCurFrame = ++robotnikCurFrame % robotnikFrameCount;
robotnikSrcX = robotnikCurFrame * rkWidth;
ctx.clearRect(robotnikX,robotnikY,rkWidth,rkHeight);
ringCurFrame = ++ringCurFrame % ringFrameCount;
ringSrcX = ringCurFrame * rWidth;
ctx.clearRect(ringX,ringY,rWidth,rHeight);
}
//Controls
//(delay saut retour position initiale)
let initRight = true;
let initLeft = false;
let keyState = {};
document.addEventListener('keydown',function(e){
keyState[e.keyCode || e.which] = true;
},true);
document.addEventListener('keyup',function(e){
keyState[e.keyCode || e.which] = false;
oneJump = true;
if(initRight){
initDroit();
} else { initGauche(); }
},true);
function gameLoop() {
//Collision rings
if((x+50) == ringX && collisionRing || (x+50)> ringX &&collisionRing ){
collisionRing = false;
ringSound = new Audio('./sounds/ring.mp3');
ringSound.play();
}
//Control droit
if (keyState[39] || keyState[68]){
bX += 2;
ringX -=2;
initRight = true;
initLeft = false;
droite();
}
if (keyState[39]&& keyState[16] || keyState[68] && keyState[16]){
bX += 3;
ringX -=3;
initRight = true;
initLeft = false;
courirDroit();
}
//Control gauche
if (keyState[37] || keyState[65]){
bX -= 2;
ringX +=2;
initRight = false;
initLeft = true;
gauche();
}
if (keyState[37]&& keyState[16] || keyState[65] && keyState[16]){
bX -= 3;
ringX +=3;
initRight = false;
initLeft = true;
courirGauche();
}
//Control Boule
if (keyState[83] || keyState[40]){
boule();
if (keyState[39]&& keyState[16] || keyState[68] && keyState[16]){
bX += 3;
ringX -=3;
bouleRapideDroit();
}
if (keyState[37]&& keyState[16] || keyState[65] && keyState[16]){
bX -= 3;
ringX +=3;
bouleRapideGauche();
}
}
//Control Saut
if (keyState[32]){
while(oneJump){
oneJump = false;
if(initRight){
sautDroit();
animSaut();
} else {
sautGauche();
animSaut();
}
}
}
if (keyState[32] && keyState[68] || keyState[32] && keyState[39]){
while(oneJump){
oneJump = false;
sautDroit();
animSaut();
}
}
if (keyState[32] && keyState[65] || keyState[32] && keyState[37]){
while(oneJump){
oneJump = false;
sautGauche();
animSaut();
}
}
setTimeout(gameLoop, 10);
}
gameLoop();
function draw(){
//Rafraichissement
updateFrame();
ctx.drawImage(background,bX,0,1400,bY,0,-100,1200,800);
animRobotnik();
ctx.drawImage(robotnik,robotnikSrcX, robotnikSrcY,rkWidth,rkHeight,robotnikX,robotnikY,rkWidth,rkHeight);
if(collisionRing){
ctx.drawImage(ring,ringSrcX, ringSrcY,rWidth,rHeight,ringX,ringY,rWidth,rHeight);
}
let projection = ctx.drawImage(sonic,srcX,srcY,width,height,x,y,width,height);
}
function initDroit(){
rows = 9;
cols = 9;
y=350;
spriteWidth = 810;
spriteHeight = 1080;
width = spriteWidth/cols;
height = spriteHeight/rows;
frameCount = 9;
srcX= 0;
srcY= 0;
}
function initGauche(){
rows = 9;
cols = 6;
y=350;
spriteWidth = 540;
spriteHeight = 1080;
width = spriteWidth/cols;
height = spriteHeight/rows;
frameCount = 6;
srcX= 0;
srcY= 715;
}
function droite(){
rows = 9;
cols = 3;
y=350;
spriteWidth = 288;
spriteHeight = 1080;
width = spriteWidth/cols;
height = spriteHeight/rows;
frameCount = 3;
srcX= 0;
srcY= 130;
}
function gauche(){
rows = 9;
cols = 3;
y=350;
spriteWidth = 285;
spriteHeight = 1080;
width = spriteWidth/cols;
height = spriteHeight/rows;
frameCount = 3;
srcX= 0;
srcY= 590;
}
function boule(){
rows = 9;
cols = 4;
y=410;
spriteWidth = 289;
spriteHeight = 600;
width = spriteWidth/cols;
height = spriteHeight/rows;
frameCount = 4;
srcX= 0;
srcY= 380;
}
function bouleRapideDroit(){
rows = 9;
cols = 3;
y=399;
spriteWidth = 330;
spriteHeight = 630;
width = spriteWidth/cols;
height = spriteHeight/rows;
frameCount = 3;
srcX= 0;
srcY= 980;
}
function bouleRapideGauche(){
rows = 9;
cols = 3;
y=399;
spriteWidth = 330;
spriteHeight = 630;
width = spriteWidth/cols;
height = spriteHeight/rows;
frameCount = 3;
srcX= 0;
srcY= 1065;
}
function courirDroit(){
rows = 9;
cols = 4;
y=350;
spriteWidth = 400;
spriteHeight = 1080;
width = spriteWidth/cols;
height = spriteHeight/rows;
frameCount = 4;
srcX= 0;
srcY= 258;
}
function courirGauche(){
rows = 9;
cols = 4;
y=350;
spriteWidth = 440;
spriteHeight = 1080;
width = spriteWidth/cols;
height = spriteHeight/rows;
frameCount = 4;
srcX= 0;
srcY= 850;
}
function sautDroit(){
rows = 9;
cols = 5;
y=350;
spriteWidth = 450;
spriteHeight = 1100;
width = spriteWidth/cols;
height = spriteHeight/rows;
frameCount = 4;
srcX= 0;
srcY= 456;
setTimeout(initDroit,500);
}
function sautGauche(){
rows = 9;
cols = 5;
y=350;
spriteWidth = 450;
spriteHeight = 1070;
width = spriteWidth/cols;
height = spriteHeight/rows;
frameCount = 4;
srcX= 0;
srcY= 1157;
setTimeout(initGauche,500);
}
function animSaut(){
setTimeout(function(){
bY-=100; ringY+=100
},100)
setTimeout(function(){
bY-=50; ringY+=100
},150)
setTimeout(function(){
bY-=50; ringY+=50
},250)
setTimeout(function(){
bY+=50; ringY-=50
},300)
setTimeout(function(){
bY+=100; ringY-=100
},350)
setTimeout(function(){
bY+=50; ringY-=100
},400)
}
function animRobotnik(){
if(posRobotnik){
robotnikSrcY= 90;
toursRobotnik += 1;
robotnikX -= 20;
if(toursRobotnik >= 20){
// robotnikTemp = robotnikX
posRobotnik = false;
toursRobotnik = 0;
// robotnikX=robotnikTemp;
}
}
else if (!posRobotnik) {
robotnikSrcY= 720;
robotnikX += 20;
toursRobotnik += 1;
if(toursRobotnik>= 20){
posRobotnik = true;
toursRobotnik = 0;
}
}
}
setInterval(draw, 80);
</script>