Update app.js

pull/1886/head
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

@ -1,119 +1,116 @@
class Ball { class Ball {
constructor(collision) { constructor(collision) {
this.collision = collision; this.collision = collision;
this.w = 4; this.w = 4;
this.h = this.w; this.h = this.w;
this.y = height / 2 - this.h / 2; this.y = height / 2 - this.h / 2;
this.x = width / 2 - this.w / 2; this.x = width / 2 - this.w / 2;
this.oldX = this.x; this.oldX = this.x;
this.oldY = this.y; this.oldY = this.y;
this.velX = 6; this.velX = 6;
this.velY = 3.5 + Math.random(); this.velY = 3.5 + Math.random();
} }
reset() { reset() {
this.y = height / 2 - this.h / 2; this.y = height / 2 - this.h / 2;
this.x = width / 2 - this.w / 2; this.x = width / 2 - this.w / 2;
this.velX = 6; this.velX = 6;
this.velY = 3.5 + Math.random(); this.velY = 3.5 + Math.random();
} }
checkCollision(that, isLeft) { checkCollision(that, isLeft) {
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) {
this.velX = -this.velX; this.velX = -this.velX;
this.velY = (3.5 + 2 * Math.random()) * this.velY / Math.abs(this.velY); this.velY = (3.5 + 2 * Math.random()) * this.velY / Math.abs(this.velY);
if (isLeft) { if (isLeft) {
right.follow = this; right.follow = this;
left.follow = null; left.follow = null;
} else { } else {
left.follow = this; left.follow = this;
right.follow = null; right.follow = null;
} }
} }
} }
move() { move() {
if (this.velX > 0) { if (this.velX > 0) {
this.checkCollision(right, false); this.checkCollision(right, false);
} else { } else {
this.checkCollision(left, true); this.checkCollision(left, true);
} }
this.x += this.velX; this.x += this.velX;
this.y += this.velY; this.y += this.velY;
if (this.y <= this.h) { if (this.y <= this.h) {
this.y = this.h; this.y = this.h;
this.velY = -this.velY; this.velY = -this.velY;
} }
if (this.y >= height - this.h) { if (this.y >= height - this.h) {
this.y = height - this.h; this.y = height - this.h;
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();
} }
} }
} }
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;
} }
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();
} }
scored() { scored() {
let d = new Date(); let d = new Date();
let value = 0; let value = 0;
if (this.side == "left") { if (this.side == "left") {
value = d.getHours(); value = d.getHours();
} else { } else {
value = d.getMinutes(); value = d.getMinutes();
} }
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) {
@ -122,25 +119,26 @@ class Paddle {
if (this.y > height - this.h) { if (this.y > height - this.h) {
this.y = height - this.h; this.y = height - this.h;
} }
} }
} }
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;
} }
ball.move(); ball.move();
redraw(); redraw();
var nextStep = 40 - (Date.now() - lastStep); var nextStep = 40 - (Date.now() - lastStep);
//console.log(nextStep); //console.log(nextStep);
@ -212,6 +210,7 @@ function stop() {
} }
var pauseTimeout = null; var pauseTimeout = null;
function pause() { function pause() {
stop(); stop();
left.scored(); left.scored();
@ -235,7 +234,7 @@ Bangle.setUI("clock");
//setup play area //setup play area
var height = g.getHeight(), var height = g.getHeight(),
width = g.getWidth(); width = g.getWidth();
var top = 0; var top = 0;
g.reset(); g.reset();