// класс определяющий параметры игрового прямоугольника и метод для его отрисовки
function rect(color, x, y, width, height) {
this.color = color; // цвет прямоугольника
this.x = x; // координата х
this.y = y; // координата у
this.width = width; // ширина
this.height = height; // высота
// функция рисует прямоугольник согласно заданным параметрам
this.draw = function() {
context.fillStyle = this.color;
context.fillRect(this.x, this.y, this.width, this.height);
};
}
// функция проверяет пересекаются ли переданные ей прямоугольные объекты
function collision(objA, objB) {
if (objA.x + objA.width > objB.x && objA.x < objB.x + objB.width && objA.y + objA.height > objB.y && objA.y < objB.y + objB.height) {
return true;
}
else {
return false;
}
}
// движение оппонента
function aiMove() {
var y;
// делаем скорость оппонента зависимой от скорости шарика
switch (ball.vY) {
case 2:
vY = 2;
break;
case 3:
vY = 3;
break;
case 4:
vY = 4;
break;
case 5:
vY = 5;
break;
case 6:
vY = 5;
break;
case 7:
vY = 6;
break;
case 8:
vY = 6;
break;
case 9:
vY = 6;
break;
case 0:
vY = 0;
break;
}
if (ball.y < ai.y + ai.height / 2) {
y = ai.y - vY;
}
if (ball.y > ai.y + ai.height / 2) {
y = ai.y + vY;
}
if (10 < y && y < game.height - ai.height - 10) {
ai.y = y;
}
}
// движение игрока
function playerMove(e) {
if (start) {
var y = e.pageY;
// условие проверяет не выходит ли ракетка за пределы поля
if (player.height / 2 + 10 < y && y < game.height - player.height / 2 - 10) {
// привязываем положение мыши к середине ракетки
player.y = y - player.height / 2;
}
}
}
function startGame() {
if (!start) {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.