1
resposta

Chance de errar do oponente

olá as funções estão funcionando normalmente no jogo, porém, mesmo incluindo a função de chance de errar do oponente, não houve nenhum erro, por parte do oponente, testei diversos valores e, ou 100% das vezes a raquete colide com a bolinha, ou 100% das vezes a raquete erra a bolinha, não consegui encontrar um meio termo entre acerto ou erro do oponente Onde posso ter errado? ` //variable ball let xBall = 300; let yBall = 200; let diameter = 25; let radius = diameter / 2;

//variable movement ball let speedXBall = 8; let speedYBall = 4;

//variable rect let xRect = 5; let yRect = 150; let widthRect = 15; let heightRect = 90;

//variable rect2 let xRect2 = 580; let yRect2 = 150; let speedYRect2;

//variable colisium rect let colisium = false

//variable game score let myPoint = 0 let opponentPoint = 0

//variable sounds game let soundRect; let soundScore; let soundGame;

//variable chance to miss let chanceToMiss = 0

function preload (){ soundGame = loadSound ("trilha.mp3") soundScore = loadSound ("ponto.mp3") soundRect = loadSound ("raquetada.mp3") }

function setup() { createCanvas(600, 400); soundGame.loop(); }

function draw() { background(0); mostrarBolinha(); movementBall (); movementBallCollision (); mostrarRect(xRect, yRect); movementRect(); colisiumRect(); //colisiumRectBall (); mostrarRect (xRect2, yRect2); //movementRect2 (); movimentRect2Automatic (); colisiumRect2 (); gameScore (); score (); }

function mostrarBolinha () { circle(xBall,yBall,diameter); }

function movementBall () { xBall += speedXBall; yBall += speedYBall; }

function movementBallCollision () { if (xBall + radius > width || xBall - radius < 0) { speedXBall *= -1; } if (yBall + radius > height || yBall - radius < 0) { speedYBall *= -1; } }

function mostrarRect(x, y) { rect(x, y, widthRect, heightRect); }

function movementRect (){ if (keyIsDown(83)){ yRect +=10; } if (keyIsDown(87)){ yRect -=10; } }

function colisiumRect () { if (xBall - radius < xRect + widthRect && yBall - radius < yRect + heightRect && yBall + radius > yRect) { speedXBall *= -1; soundRect.play(); } }

function movementRect2 (){ if (keyIsDown(DOWN_ARROW)){ yRect2 +=10; } if (keyIsDown(UP_ARROW)){ yRect2 -=10; } }

function movimentRect2Automatic () { speedYRect2 = yBall - yRect2 - widthRect / 2 - 60; yRect2 += speedYRect2 + chanceToMiss calculateChanceToMiss }

function calculateChanceToMiss () { if (opponentPoint >= myPoint) { chanceToMiss += 1 if (chanceToMiss >= 39){ chanceToMiss = 40 } } else { chanceToMiss -=1 if (chanceToMiss <=35) { chanceToMiss = 35 } } }

function colisiumRect2 () { if (xBall + radius * 2 > xRect2 + widthRect && yBall - radius < yRect2 + heightRect && yBall + radius > yRect2) { speedXBall *= -1; soundRect.play(); } }

function colisiumRectBall (){ colisium = colisiumRect (x, y, widthRect, heightRect, xBall, yBall, radius); if (colisium){ speedXBall *= -1; } }

function gameScore (){ stroke (255); textAlign (CENTER); textSize(30); textStyle(BOLD); fill (color (255, 140,0)); rect (150,10,50,40); fill (255); text (myPoint, 175, 40); fill (color (255, 140,0)); rect (450,10,50,40); fill (255); text (opponentPoint, 475, 40); }

function score (){ if (xBall > 585) {myPoint += 1; soundScore.play(); } if (xBall < 15) {opponentPoint += 1; soundScore.play(); } }

1 resposta

talvez o problema seja na ideia que ele constrói o código do y parede "teleportando" para o y da bola

eu vi que no p5 tem função pra aceleração, será que funcionaria? ja que ele meio que deslizaria mais ou menos do que deve