Bonjour à tous,

Je suis un très gros débutant en Javascript (et en programmation tout court), et pour progresser, je me suis mis en tête de développer un petit shoot vertical et je rencontre un problème actuellement que je n'arrive pas à résoudre .

Le problème concerne la gestion du tir des adversaires. Je souhaite que ces derniers se dirigent vers le joueur.

Jusqu'ici pas de problème, j'ai utilisé une formule pour que l'ennemie tir avec un angle permettant d'atteindre le joueur. Mais voilà, le tir, si le joueur l'évite, reste à la position du joueur x et y, au lieu de continuer sa route et de sortir de l'écran comme illustré si dessous (désolé pour la capture :p).






Je suis persuadé que le problème réside dans la déclaration de la position du joueur lors du calcul de l'angle (ligne 252 du code complet), mais je sèche complètement sur la façon de remédier à ce bug :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
/*
this.cx = position X du joueur
this.cy = position Y du joueur
this.x = position X de l'ennemie
this.y = position Y de l'ennemie
 
*/
                this.angle = Math.atan2(this.cy - this.y, this.cx - this.x);
		this.angle *= (180/Math.PI);
 
		var frameRate = 1/60;  
		var speed = 175;
 
		var velocityX = Math.cos((this.angle) * Math.PI / 180) * (speed * frameRate);
		var velocityY = Math.sin((this.angle) * Math.PI / 180) * (speed * frameRate);
 
		this.x += velocityX;
                this.y += velocityY;
code complet :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
 
window.onload = function()
{
	var canvas = document.getElementById("canvas");
	var ctx = canvas.getContext("2d");
 
	var Scrolling = new Image ();
 
	Scrolling.src = "test_scrolling.PNG";
 
	var posX = 0;
 
	var posY = 0;
 
	var posY2 = -711;
 
	function drawBG(){
 
		ctx.drawImage(Scrolling, posX, posY);
		ctx.drawImage(Scrolling, posX, posY2);
		if (posY > 711) {
			posY = -711;
		}
 
		if (posY2 > 711) {
			posY2 = -711;
		}
 
		posY += 2;
		posY2 += 2;
 
	};
 
	var arrowLeft = 81; //q
	var arrowRight = 68; //d
	var shoot =  32;
 
	var shots = [];	
	var tir = false; 
	var score =0;
 
	var shotsEnemies = [];	
	var listeEnemies = [];	
 
	var goRight = false;
	var goLeft = false;
	var vLeft = false;
	var vRight = false;
 
	var imageVaisseau = new Image();
 
	imageVaisseau.src = "Spaceship_Spritesheet.png";
 
	var bullet = new Image();
	bullet.src = "Sprite_Bullets.png";
 
	var invader = new Image();
 
	invader.src = "Vaisseau_neutre.png";
 
	var compteurG = 0;
 
	var compteurD = 0;
 
	window.addEventListener("keydown", function (e) {
		if (e.keyCode === shoot) {		
			tir = true;	
			setTimeout(function () {
			tir = false;}, 250);
		} 	
	});
 
	window.addEventListener("keyup", function (e) {
		if (e.keyCode === shoot) {		
		 tir = false;						
		}
	});
 
	var Perso = 
	{
		init : function (x, y, largeur, hauteur,vitesseL,vitesseR)
		{
		this.x = x;
		this.y = y;
		this.largeur = largeur;
		this.hauteur = hauteur;
		this.vitesseL = vitesseL;
		this.vitesseR = vitesseR;		
		},
 
		draw : function ()	
	{
		if (goLeft === false && goRight === false) {
		ctx.drawImage(imageVaisseau, 10, 7, 95, 96, this.x, this.y, this.largeur, this.hauteur);		
		};
 
		if (goLeft === true && compteurG <=19) {
		ctx.drawImage(imageVaisseau, 123, 7, 95, 96, this.x, this.y, this.largeur, this.hauteur);		
		};
 
		if (goLeft === true && compteurG >= 20 && compteurG <= 99 ) {
		ctx.drawImage(imageVaisseau, 236, 7, 95, 96, this.x, this.y, this.largeur, this.hauteur);		
		};
 
		if (goLeft === true && compteurG >= 100) {
		ctx.drawImage(imageVaisseau, 349, 7, 95, 96, this.x, this.y, this.largeur, this.hauteur);		
		};
 
		if (goRight === true  && compteurD <= 19) {
		ctx.drawImage(imageVaisseau, 123, 121, 95, 96, this.x, this.y, this.largeur, this.hauteur);		
		};
 
		if (goRight === true  && compteurD >= 20 && compteurD <= 99) {
		ctx.drawImage(imageVaisseau, 236, 121, 95, 96, this.x, this.y, this.largeur, this.hauteur);			
		};
 
		if (goRight === true && compteurD >= 100) {
		ctx.drawImage(imageVaisseau, 349, 121, 95, 96, this.x, this.y, this.largeur, this.hauteur);			
		};
 
	},
 
		inp : function ()		
		{
			window.addEventListener("keydown", function (e) {			
				if (e.keyCode === arrowLeft) {
				goLeft = true;
				goRight = false;
				}
 
				if (e.keyCode === arrowRight) {	
				goLeft = false;
				goRight = true;
				}
			});
 
			window.addEventListener("keyup", function (e) {			
				if (e.keyCode === arrowLeft) {
				goLeft = false;    
				vLeft = true;
				}
 
				if (e.keyCode === arrowRight) {	
				goRight = false;
				vRight = true;
				}			
			});
 
		},
 
		dep : function ()
		{
			if (goLeft === true && this.x > 5)
			{
			vRight = false;
			this.x +=  -4 ;			
			}
			if (goRight === true && this.x <canvas.width - this.largeur - 5)
			{
			vLeft = false;	
			this.x += 4	;
			}
 
			if (vLeft === true && this.x > 5)
			{
				this.x +=  (this.vitesseL *= 0.94);
				this.vitesseR = 4;
			}
 
			if (vRight === true && this.x <canvas.width - this.largeur - 5)
			{			
				this.x +=  (this.vitesseR *= 0.94);						
				this.vitesseL  = -4;
			}
 
		},
 
	};
 
	var Player = Object.create(Perso);
	Player.init(canvas.width/2 - 35,350, 95, 98,-3,3);
 
	function  Shot(x,y,dx,dy){
		this.x = x;
		this.y = y ;
		this.dx = dx;
		this.dy = dy;		
	}
 
	Shot.prototype.display = function () {
		ctx.drawImage(bullet, 0, 0, 12, 29, this.x, this.y, this.dx, this.dy);
		this.y -= 5;		
	};	
 
	function Enemies( x, y, dx, dy, v, type){
		this.x = x;
		this.y = y;
		this.dx = dx;
		this.dy = dy;
		this.v = v;		
		this.type = type;
	};	
 
	Enemies.prototype.display = function (){
		ctx.drawImage(invader, 0, 0, 128, 128, this.x, this.y, this.dx, this.dy);		
 
		if (this.type === "horz"){
		this.x += this.v;
		};
 
		if (this.type === "vert"){
		this.y += this.v;		
		};
 
	};	
 
	function  ShotEnemies(x,y,dx,dy, cx,cy, angle){
		this.x = x;
		this.y = y ;
		this.dx = dx;
		this.dy = dy;	
		this.cx = cx;
		this.cy = cy
		this.angle = angle;		
	}	
 
	ShotEnemies.prototype.display = function () {
		ctx.drawImage(bullet, 0, 0, 12, 29, this.x, this.y, this.dx, this.dy);		
	}
 
	ShotEnemies.prototype.mouv = function () {
 
		this.angle = Math.atan2(this.cy - this.y, this.cx - this.x );
		this.angle *= (180/Math.PI);
 
		var frameRate = 1/60;  
		var speed = 175;
 
		var velocityX = Math.cos((this.angle) * Math.PI / 180) * (speed * frameRate);
		var velocityY = Math.sin((this.angle) * Math.PI / 180) * (speed * frameRate);
 
		this.x += velocityX;
        this.y += velocityY;
	};
 
	Enemies.prototype.Tir = function (){
		var cx = Player.x + Player.largeur/2 ;
		var cy = Player.y + Player.hauteur/2;
		var chance = 5;
		if(chance > Math.random()*2000) {
			listeEnemies.forEach(function (Enemies) {
				shotsEnemies.push(new ShotEnemies(Enemies.x + Enemies.dx/2, Enemies.y + Enemies.dy/2, 12, 29, cx, cy));
			});
		};
	};
 
	function vague(){
 
		listeEnemies.push(new Enemies(0 - 100, Math.random()*canvas.height/2, 50, 40, 2, "horz"));
 
		listeEnemies.push(new Enemies(0 - 100, Math.random()*canvas.height/2, 50, 40, 2, "horz"));
 
		listeEnemies.push(new Enemies(Math.random()*canvas.width, 0 - 100, 50, 40, 2, "vert"));
 
		listeEnemies.push(new Enemies(Math.random()*canvas.width, 0 - 100, 50, 40, 2, "vert"));
 
		if ( score > 500) {						
 
			listeEnemies.push(new Enemies(0 - 100, Math.random()*canvas.height/2, 50, 40, 2, "horz"));
 
			listeEnemies.push(new Enemies(Math.random()*canvas.width, 0 - 100, 50, 40, 2, "vert"));	
 
		};		
	};
 
	vague();
 
 
	function animer()
	{	
		ctx.clearRect(0, 0, canvas.width, canvas.height);
		drawBG();
		Player.draw();
		Player.inp();
		Player.dep();
		drawHUD();
 
 
		if (goLeft === false && goRight === false) {	
			compteurG = 0
			compteurD = 0
		};
 
		if (goLeft === true){
			compteurG ++
			compteurD = 0
		};
 
		if (goRight === true){
			compteurD ++
			compteurG = 0
		};	
 
		if (tir === true) {
			shots.push(new Shot(Player.x + Player.largeur/2 , Player.y, 12, 29));	  
		};
 
		for(var j = 0; j< shotsEnemies.length; j++){
			var shotE = shotsEnemies[j];
 
			if (shotE.x > 0 && shotE.y < canvas.height){					
				shotE.display();
				shotE.mouv();				
			} else {
				shotsEnemies.splice(j--, 1);					
			}; 			
 
			if (shotE.x >= Player.x 
			&& shotE.x < Player.x + Player.largeur 
			&& shotE.y >= Player.y 
			&& shotE.y < Player.y + Player.hauteur  ){
				shotsEnemies.splice(j--, 1);
			};					
		};	
 
		for(var i = 0; i< shots.length; i++){				
			var shot = shots[i];
 
			if(i % 10 === 0){
				if (shot.x >= 0 && shot.y > 0){	
				shot.display();							
				} else {
					shots.splice(i--, 10)	
				};					
			};			
		};	
 
		if(listeEnemies.length < 1){	
			vague();			
		};
 
		for (var o = 0; o < listeEnemies.length; o++){				
			var ho = listeEnemies[o];				
			ho.display();
			ho.Tir();	
 
			if(ho.x > canvas.width || ho.y > canvas.height){
				listeEnemies.splice(o--, 1);
			};			
		};
 
 
		for (var o = 0; o < listeEnemies.length; o++){
			var ho = listeEnemies[o];
			var bang = false;
 
			for(var i = 0; i< shots.length; i++){				
				var shot = shots[i];
				if(	shot.x >= ho.x 
				&& shot.x < ho.x + ho.dx 
				&& shot.y >= ho.y 
				&& shot.y < ho.y + ho.dy)
				{						
					bang = true;
					shots.splice(i--, 10);
					score += 100;
				};
			};
 
			if (bang){							
				listeEnemies.splice (o--,1);				
			};	
		};	
 
		requestAnimationFrame(animer);		
	};		
	requestAnimationFrame(animer);	
};
Merci d'avance pour me donner une piste de réflexion pour résoudre ce petit problème