Update app.js

master
pidajo 2022-06-06 14:11:00 +02:00 committed by GitHub
parent fe5c293c46
commit bb1f8d6c2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 83 additions and 84 deletions

View File

@ -22,8 +22,7 @@ class Ball {
let test = false; let test = false;
if (isLeft) { if (isLeft) {
test = this.x <= that.w + this.w && this.y > that.y && this.y < that.y + that.h; test = this.x <= that.w + this.w && this.y > that.y && this.y < that.y + that.h;
} } else {
else {
test = this.x >= that.x + this.w && this.y > that.y && this.y < that.y + that.h; test = this.x >= that.x + this.w && this.y > that.y && this.y < that.y + that.h;
} }
if (test) { if (test) {
@ -60,10 +59,10 @@ class Ball {
this.velY = -this.velY; this.velY = -this.velY;
} }
if(this.x >= width) { if (this.x >= width) {
left.scored(); left.scored();
restart(); restart();
} else if(this.x < 0) { } else if (this.x < 0) {
right.scored(); right.scored();
restart(); restart();
} }
@ -74,11 +73,11 @@ class Ball {
class Paddle { class Paddle {
constructor(side) { constructor(side) {
this.side = side; this.side = side;
this.w = 4;//15; this.w = 4; //15;
this.h = 30;//80; this.h = 30; //80;
this.y = height / 2 - this.h/2; this.y = height / 2 - this.h / 2;
this.follow = null; this.follow = null;
this.target = height / 2 - this.h/2; this.target = height / 2 - this.h / 2;
this.score = 99; this.score = 99;
this.hasLost = false; this.hasLost = false;
} }
@ -86,8 +85,8 @@ class Paddle {
reset() { reset() {
this.follow = null; this.follow = null;
this.hasLost = false; this.hasLost = false;
this.target = height / 2 - this.h/2; this.target = height / 2 - this.h / 2;
this.y = height / 2 - this.h/2; this.y = height / 2 - this.h / 2;
this.move(); this.move();
} }
@ -101,19 +100,17 @@ class Paddle {
} }
if (this.score < value) { if (this.score < value) {
this.score++; this.score++;
} } else {
else {
this.score = value; this.score = value;
} }
} }
move() { move() {
if (this.follow && ! this.hasLost) { if (this.follow && !this.hasLost) {
var dy = this.follow.y - this.y - this.h / 2; var dy = this.follow.y - this.y - this.h / 2;
this.y += dy / 2; this.y += dy / 2;
} } else {
else {
this.y += (this.target - this.y) / 10; this.y += (this.target - this.y) / 10;
} }
if (this.y < 0) { if (this.y < 0) {
@ -126,16 +123,17 @@ class Paddle {
} }
var updateTimeout = null; var updateTimeout = null;
function update() { function update() {
var d = new Date(); var d = new Date();
var lastStep = Date.now(); var lastStep = Date.now();
left.move(); left.move();
right.move(); right.move();
if(d.getHours() != left.score) { if (d.getHours() != left.score) {
right.follow = null; right.follow = null;
right.hasLost = true; right.hasLost = true;
} }
if(d.getMinutes() != right.score) { if (d.getMinutes() != right.score) {
left.follow = null; left.follow = null;
left.hasLost = true; left.hasLost = true;
} }
@ -212,6 +210,7 @@ function stop() {
} }
var pauseTimeout = null; var pauseTimeout = null;
function pause() { function pause() {
stop(); stop();
left.scored(); left.scored();