Triborg-333
@Triborg-333

Как сделать так, чтобы объекты шли на персонажа Game JavaScript?

Как сделать так, чтобы объекты шли на персонажа Game JavaScript ?

я управляю кораблём, и рыбки должны идти на меня.

5d5461ce98167082528783.png

код:

let canvas, ctx, width, height;

requestAnimationFrame = requestAnimationFrame || mozRequestAnimationFrame;

function Game(){

    //Подключаемся к canvas.

    canvas = document.getElementById("canvas");

    //Подключаемся присваеваем context = canvas.

    ctx = canvas.getContext("2d");

    game();

    //Запускаем функцию - "Методы - this"
    
    this.methods();
}

const fon = new Image();
let img = new Image();
let fish_img = new Image();
let fg = new Image();
let fire = new Image();
let mountain_left = new Image();
let mountain_right = new Image();

fon.src = "img/fon.jpg";
fg.src = "img/fg.png";
fire.src = "img/ammo.png";
mountain_left.src = "img/mountain.png";
mountain_right.src = "img/mountain-right.png";

function game(){
    update();
    render();
    requestAnimationFrame(game);
}


function methods(ship, game_box, speed_ship){
    this.ship = []; // Массив корабля.
    this.ship[0] = {x : 0, y : 350} // Начальная позиция корабля.
    this.game_box = 150;
    this.speed_ship = 155;
    this.current = 0; // 
    this.time_push = 0; //Время добавления в массив.
    this.algae = [];  //Массив нижнего блока.
    this.algae.push({x : 0, y : 0, dx: 3.5, dy:0}) // Начальная позиция нижнего блока.
    this.x = 150,y = 150,velY = 0,velX = 0,speed = 3.5,friction = 0.98,thank_settings = [];
    this.thank_settings[0] = {x:0, y : 0}; // начальная позиция x - 0, y - 0.
    this.ammo = [];
    this.mountain = []; //Массив горы.
    this.mountain[0] = {x : 350, y : 450} //Начальная позиция правой горы.
    this.mountain_two = [];
    this.mountain_two[0] = {x : 1350, y : 450} //Начальная позиция левой горы.
	this.fish_enemy = []; //Массив рыбки.
}

methods();

function update(){
  //Физика

    for(i in algae){
        algae[i].x--;

    }
   //Делаем повторение нижнего блока.
   
    if(algae[i].x == -120){
        algae.push({x : canvas.width + 50, y : 0, dx: 3.5, dy:0})
    }
    

    //Управление кораблём.

    if (thank_settings[38]) {
        if (velY > -speed) {
            velY--;
        }
    }
    
    if (thank_settings[40]) {
        if (velY < speed) {
            velY++;
        }
    }
    if (thank_settings[39]) {
        if (velX < speed) {
            velX++;
        }
    }
    if (thank_settings[37]) {
        if (velX > -speed) {
            velX--;
            
        }
    }

    velY *= friction;
    y += velY;
    velX *= friction;
    x += velX;

    if(x >= 900){
        x = 900;
    }else if(x <= 5){
        x = 5;
    }
    if(y >= 800){
        y = 800;
    }else if(y <= 5){
        y = 5;
    }
   //Делаем выстрел 

    time_push++;

	if(time_push%73==0){
         		fish_enemy.push({
					x : Math.floor(Math.random()* canvas.width + 2950 / 2), 
					y : Math.floor(Math.random()* 17 + 1)* 44,
					dx: Math.floor(Math.random()* 2+2),
					dy : 0
				})
    }
    
    for(i in ammo){
        //Делаем физику выстрела
        ammo[i].x=ammo[i].x+ammo[i].dx;
    }
	for(i in mountain){
		
	    if(mountain[i].x == 125){
			   
        mountain.push({
				x : Math.random() * canvas.width + 975,
				y : 450
			})		   
        }
	}
	
	for(i in fish_enemy){
		
		fish_enemy[i].x=fish_enemy[i].x-fish_enemy[i].dx;
		
		for(o in ammo){
		  
	   }

	}
	
			
	  

}



let ship = setInterval(() => {
    this.current = (this.current + 1) % images.length
    img.src = images[this.current]

}, this.speed_ship);


let fish_interval =  setInterval(() => {
    this.current = (this.current + 1) % fish.length
    fish_img.src = fish[this.current]

}, this.speed_ship);

Game.prototype.methods = function(){
    
}

  document.body.addEventListener("keydown", function (e) {
    thank_settings[e.keyCode] = true;
});
  document.body.addEventListener("keyup", function (e) {
    thank_settings[e.keyCode] = false;
});

//Выстрел при нажатии на пробел.

document.onkeydown = function(e){
    if(e.keyCode == 32){
        ammo.push({
            x:x+125,
            y:y+55,
            dx:Math.random() * 2+2,
            dy:1.5
        });
    }
}

function render(){

    ctx.drawImage(fon, 0, 0);

    for(i in mountain_two){
        ctx.drawImage(mountain_left, mountain_two[i].x, mountain_two[i].y, 950, 950);

        mountain_two[i].x--;

        if(mountain_two[i].x == 125){
            mountain_two.push({
                x : Math.random() * canvas.width + 1475,
                y : 450
            })
        }
    }
    for(i in mountain){
       ctx.drawImage(mountain_right, mountain[i].x, mountain[i].y, 750, 750);

       mountain[i].x--;
    }
     
     
    for(i in algae){
        ctx.drawImage(fg, algae[i].x, canvas.height - fg.height)
    }
	
    for(i in thank_settings)ctx.drawImage(img, x, y, 150, 150);
	
	for(i in fish_enemy)ctx.drawImage(fish_img, fish_enemy[i].x, fish_enemy[i].y, 90, 90);

    for(i in ammo)ctx.drawImage(fire, ammo[i].x, ammo[i].y, 40, 40);


}

window.onload = function(){
    new Game("ctx");
}
  • Вопрос задан
  • 127 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы