Apenas compartilhar meu código para analisarmos melhorias e meios possíveis de fazer;
class State {
constructor(){
this.lastToch = {score: 0}
this.players = {
Player1: { name: "Robot1", mov:{y: 225, velocidade: 20}, score: 0, robot: true },
Player2: { name: "Robot2", mov:{y: 225, velocidade: 20}, score: 0, robot: true }
};
this.ball = { x: 400, y: 300, vx: 5, vy: 5 }
}
stateFulBall(){
//Move Bola
this.ball.x += this.ball.vx
this.ball.y += this.ball.vy
//Colisão Bola Jogador1
if(
this.ball.x < 55 &&
this.ball.y > this.players[Object.keys(this.players)[0]].mov.y &&
this.ball.y < this.players[Object.keys(this.players)[0]].mov.y + 150
){
this.lastToch = this.players[Object.keys(this.players)[0]]
this.ball.vx = this.ball.vx * -1;
}
//Colisão Bola Jogador2
if(
this.ball.x > 750 &&
this.ball.y > this.players[Object.keys(this.players)[1]].mov.y &&
this.ball.y < this.players[Object.keys(this.players)[1]].mov.y + 150
){
this.lastToch = this.players[Object.keys(this.players)[1]]
this.ball.vx = this.ball.vx * -1;
}
//Colisão Bola Window
if(this.ball.y > 600 || this.ball.y < 0){
this.ball.vy = this.ball.vy * -1;
}
//Colisão Bola Ponto
if(this.ball.x == 780 || this.ball.x == 5){
this.ball = {x: 400, y: 300, vx: 5, vy: 5};
this.players[Object.keys(this.players)[0]].mov = {y: 225, velocidade: 20},
this.players[Object.keys(this.players)[1]].mov = {y: 225, velocidade: 20};
this.lastToch.score ++;
this.lastToch = {score: 0};
}
}
userInput(keyPress){
let player =
[this.players[Object.keys(this.players)[1]],
this.players[Object.keys(this.players)[0]]]
let acceptMov = {
arrowdown:()=>{
player[0].mov.y += player[0].mov.velocidade;
player[0].robot = false;
},
arrowup: ()=>{
player[0].mov.y -= player[0].mov.velocidade;
player[0].robot = false;
},
s:()=>{
player[1].mov.y += player[1].mov.velocidade;
player[1].robot = false;
},
w: ()=>{
player[1].mov.y -= player[1].mov.velocidade;
player[1].robot = false;
}
}
if(acceptMov[keyPress]){ acceptMov[keyPress]() }else{ console.log(keyPress) }
}
robot(){
if(this.players[Object.keys(this.players)[0]].robot){
this.players[Object.keys(this.players)[0]].mov.y = this.ball.y -75
}else{
this.players[Object.keys(this.players)[0]].name = "Player1"
}
if(this.players[Object.keys(this.players)[1]].robot){
this.players[Object.keys(this.players)[1]].mov.y = this.ball.y -75
}else{
this.players[Object.keys(this.players)[1]].name = "Player2"
}
}}
var game = new State()
document.addEventListener('keydown', (event) => {
game.userInput(event.key.toLowerCase());
});
function setup() {
createCanvas(800, 600);
}
function draw() {
background(0)
fill(255)
rect( 20, game.players[Object.keys(game.players)[0]].mov.y, 15, 150)
rect(770, game.players[Object.keys(game.players)[1]].mov.y, 15, 150)
circle(game.ball.x, game.ball.y, 30)
game.stateFulBall()
game.robot()
textSize(32);
text(`${game.players[Object.keys(game.players)[0]].name}: ${game.players[Object.keys(game.players)[0]].score}`, 180, 40)
text(`${game.players[Object.keys(game.players)[1]].name}: ${game.players[Object.keys(game.players)[1]].score}`, 520, 40)
fill(color('rgba(255, 0, 0, 0.3)'));
rect( 0, 0, 15, 600)
rect(785, 0, 15, 600)
}