https://pastein.ru/t/vq
скопируйте уникальную ссылку для отправки
//function inizilize(){
const moveSpeed = 9;
var moveSpeedNow;
var ballSpeedX = 18;
var ballSpeedY = 0;
var rightPaddle = document.getElementById('right');
var leftPaddle = document.getElementById('left');
var field = document.getElementById('field');
var pressedKeysCodes = new Set();
var allCodes = [ 38 , 40 , 83 , 87 ];
var startTime = new Date;
var limitedTime = new Date;
//}
function go1(){
/*
for(let keyCodeFromSet of allCodes){
if(pressedKeysCodes.has(keyCodeFromSet)){
alert(keyCodeFromSet);
}
}
*/
//alert();
/*
if(pressedKeysCodes.has(40) && pressedKeysCodes.has(83)){//две вниз
alert("это 40 и 83"); //после добывления start и limited time показалось, до этого я не видел его
}
*/
if(pressedKeysCodes.has(40) && pressedKeysCodes.has(83)){//правая и левая вниз
//alert("это 40");
rightPaddle.style.top = ( ( (rightPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) + moveSpeed) ) + "px";
leftPaddle.style.top = ( ( (leftPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) + moveSpeed) ) + "px";
}
if(pressedKeysCodes.has(38) && pressedKeysCodes.has(87)){//правая и левая вверх
rightPaddle.style.top = ( ( (rightPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) - moveSpeed) ) + "px";
leftPaddle.style.top = ( ( (leftPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) - moveSpeed) ) + "px";
}
if(pressedKeysCodes.has(83) && pressedKeysCodes.has(38)){//левая вниз правая вверх
//alert("это 83");
leftPaddle.style.top = ( ( (leftPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) + moveSpeed) ) + "px";
rightPaddle.style.top = ( ( (rightPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) - moveSpeed) ) + "px";
}
if(pressedKeysCodes.has(87) && pressedKeysCodes.has(40)){//левая вверх правая вниз
leftPaddle.style.top = ( ( (leftPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) - moveSpeed) ) + "px";
rightPaddle.style.top = ( ( (rightPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) + moveSpeed) ) + "px";
}
if(pressedKeysCodes.has(40)){//правая вниз
//alert("это 40");
rightPaddle.style.top = ( ( (rightPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) + moveSpeed) ) + "px";
//pressedKeysCodes.clear();
}
if(pressedKeysCodes.has(38)){//правая вверх
rightPaddle.style.top = ( ( (rightPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) - moveSpeed) ) + "px";
//pressedKeysCodes.clear();
}
if(pressedKeysCodes.has(83)){//левая вниз
//alert("это 83");
leftPaddle.style.top = ( ( (leftPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) + moveSpeed) ) + "px";
//pressedKeysCodes.clear();
}
if(pressedKeysCodes.has(87)){//левая вверх
leftPaddle.style.top = ( ( (leftPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) - moveSpeed) ) + "px";
//pressedKeysCodes.clear();
}
limitedTime = new Date;
if(limitedTime - startTime >= 100){
pressedKeysCodes.clear();
startTime = new Date;
}
return;
/*
if(where == "up"){
moveSpeedNow = - moveSpeed;
}else if(where == "down"){
moveSpeedNow = moveSpeed;
}else{
alert("check yhe code for the direction of paddles!");
}
if(who == "right"){
rightPaddle.style.top = ( ( (rightPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) + moveSpeedNow) ) + "px";
}else if(who == "left"){
leftPaddle.style.top = ( ( (leftPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) + moveSpeedNow) ) + "px";
}else{
alert("check yhe code for the numbers of paddles!");
}
//rightPaddle.style.top = ( ( (rightPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top -7 ) + moveSpeed) ) + "px";
//alert(rightPaddle.style.top);
*/
}
addEventListener("keydown", function(event) {
//if (event.code == "KeyS"){
// для правой
// startTime = new Date();
//for(let i = 0; i < 9; i++){
pressedKeysCodes.add(event.keyCode);
//}
// alert
go1();
});
/*
function go1(){
if(where == "up"){
moveSpeedNow = - moveSpeed;
}else if(where == "down"){
moveSpeedNow = moveSpeed;
}else{
alert("check yhe code for the direction of paddles!");
}
if(who == "right"){
rightPaddle.style.top = ( ( (rightPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) + moveSpeedNow) ) + "px";
}else if(who == "left"){
leftPaddle.style.top = ( ( (leftPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) + moveSpeedNow) ) + "px";
}else{
alert("check yhe code for the numbers of paddles!");
}
//rightPaddle.style.top = ( ( (rightPaddle.getBoundingClientRect().top - field.getBoundingClientRect().top - 7 ) + moveSpeed) ) + "px";
//alert(rightPaddle.style.top);
}
*/
/*
if(event.keyCode == 40){ //правая вниз
go1( "down", "right");
}else if(event.keyCode == 38){//правая вверх
go1( "up", "right");
}
if(event.keyCode == 83){//левая вниз
go1( "down", "left");
}else if(event.keyCode == 87){//левая вверх
go1( "up", "left");
}
*/
/*
addEventListener("keydown", function(event) {
//if (event.code == "KeyS"){
//для левой
});
*/
/*
addEventListener("keydown", function(event) { // надо на каждую ракетку по обработчику, чтобы двигаться "параллельно"
//if (event.code == "KeyS"){ //ОНИ НЕ ДВИГАЮТСЯ ПАРАЛЛЕЛЬНО!
// для правой
if(event.keyCode == 40){ //правая вниз
go1( "down", "right");
}else if(event.keyCode == 38){//правая вверх
go1( "up", "right");
}
});
addEventListener("keydown", function(event) { // надо на каждую ракетку по обработчику, чтобы двигаться "параллельно"
//if (event.code == "KeyS"){
//для левой
if(event.keyCode == 83){//левая вниз
go1( "down", "left");
}else if(event.keyCode == 87){//левая вверх
go1( "up", "left");
}
});
*/
/*
addEventListener("keyup", function(event) {
});
*/
/*
addEventListener("keydown", function(event) {
if (event.keyCode == 86)
document.body.style.background = "violet";
});
addEventListener("keyup", function(event) {
if (event.keyCode == 86)
document.body.style.background = "";
});
*/