
Код:
let game_score = 0;
const game = $('.game');
const game_gun = $('.game-gun');
const game_ducks = $('.game-ducks');
function shot() {
if (!game_gun.hasClass('shot')) {
game_gun.addClass('shot');
}
game_gun.on('animationend', function () {
game_gun.removeClass('shot');
});
}
game.click(function (event) {
// Получаем координаты клика
shot();
let cursorX = event.pageX;
let cursorY = event.pageY;
let centerX = game_gun.offset().left + (game_gun.width() / 2);
let centerY = game_gun.offset().top + (game_gun.height() / 2);
let angle = Math.atan2(cursorY - centerY, cursorX - centerX);
angle = angle * (180 / Math.PI);
game_gun.css('rotate', `${angle}deg`);
if (event.target.className.split(' ')[0] == 'game-duck') {
game_score += 1;
} else {
game_score -= 1;
}
$('.game-score span').html(game_score);
});
$(document).on('click', '.game-duck', function () {
$(this).remove();
});
Счёт: 0
Код генерации уток (если понадобится) function get_game_ducks(amount) {
for (let i = 0; i < amount; i++) {
// создаем новый элемент img
let duck = $('
');
let width = game_ducks[0].clientWidth;
interval = setInterval(function () {
let height = game_ducks[0].clientHeight;
let x = Math.floor(Math.random() * width);
let y = Math.floor(Math.random() * height);
duck.css({
top: y + "px",
left: x + "px"
});
game_ducks.append(duck);
}, 1000);
}
}
get_game_ducks(10);
Также если будут советы что убрать или добавить с радостью их выслушаю, всем спасибо кто хотя бы по пытается помочь?