please format code with </> button * homework policy * asking questions
|my coding for 2 player pong game in p5js.
in my coding function collision do not work.
ball bounces from all walls but do not bounce by hitting paddle.
please help
coding is as under.
//----------------VARIABLES------------------------
var score1 = 0;
var score2 = 0;
//---------------------------------------------------
//---------------------------------------------------
// CREATING BALL OBJECT
var ball={
x:250,
y:250,
w:20,
h:20,
spdx:3,
spdy:3
};
//-----------------------------------------------------------
// CREATING PADDLE OBJECTS OF TWO PLAYERS
var player1={
x:10, y:200, w:15, h:100, spd:3, up:false, down:false
};
var player2={
x:475, y:200, w:15, h:100, spd:3, up:false, down:false
};
//---------------------------------------------------
//CODE TO ASSIGN PADDLE MOVEMENTS WITH ASSOCIATED KEY PRESS
var keyPressed = function(){
// UP PLAYER2
if(keyCode === UP_ARROW){
player2.up=true;
}
// DOWN PLAYER2
if(keyCode === DOWN_ARROW){
player2.down=true;
}
// UP PLAYER1
if(keyCode === 87){ // w key
player1.up=true;
}
// DOWN PLAYER1
if(keyCode === 83){ // s key
player1.down=true;
}
};
//---------------------------------------------------
//CODE TO ASSIGN PADDLE MOVEMENTS WITH ASSOCIATED KEY RELEASE
var keyReleased = function(){
// UP PLAYER2
if(keyCode === UP_ARROW){
player2.up=false;
}
// DOWN PLAYER2
if(keyCode === DOWN_ARROW){
player2.down=false;
}
// UP PLAYER1
if(keyCode === 87){ // w key
player1.up=false;
}
// DOWN PLAYER1
if(keyCode === 83){ // s key
player1.down=false;
}
};
//-----------------VARIABLES-----------------------
//------------------SETUP FUNCTION--------------------
function setup() {
createCanvas(500, 500);
// CREATING BALL OBJECT
ball={
x:250,
y:250,
w:20,
h:20,
spdx:random(-3,3),
spdy:random(-3,3)
};
// CREATING PADDLE OBJECTS
player1={
x:10, y:200, w:15, h:100, spd:3, up:false, down:false
};
player2={
x:475, y:200, w:15, h:100, spd:3, up:false, down:false
};
}
//------------------SETUP FUNCTION-------------------------
//-----------------DRAW FUNCTION---------------------------
function draw() {
background(0);
halfline();
drawPlayers();
movePlayers();
drawBall();
moveBall();
drawScore();
}
//-----------------DRAW FUNCTION---------------------------
//---------------USE DEFINED FUNCTIONS----------------------
//---------------------------------------------------
function halfline(){
// for loop
for(var h=0; h<height; h++){
noStroke();
fill(255);
rect(249, 20*h, 3,8);
}
}
//---------------------------------------------------
//---------------------------------------------------//DRAWING PADDLES OF TWO PLAYERS
function drawPlayers(){
//PLAYER1
fill(255,0,0);
rect(player1.x,player1.y,player1.w,player1.h);
//PLAYER2
fill(60,161,195);
rect(player2.x,player2.y,player2.w,player2.h);
}
//---------------------------------------------------
//---------------------------------------------------
// CODE TO ASSIGN MOVEMENT KEYS OF TWO PADDLES UP AND DOWN
function movePlayers(){
//UP PLAYER1
if(player1.up && player1.y >-1){
player1.y -=player1.spd;
}
//DOWN PLAYER1
if(player1.down && player1.y <800 - player1.y){
player1.y += player1.spd;
}
//UP PLAYER2
if(player2.up && player2.y >-1){
player2.y -=player2.spd;
}
//DOWN PLAYER2
if(player2.down && player2.y <800 - player2.y){
player2.y += player2.spd;
}
}
//---------------------------------------------------
//-----------------------------------------------------------
//drawing ball object
function drawBall(){
ellipseMode(CORNER);
fill(255);
ellipse(ball.x, ball.y, ball.w, ball.h);
}
//---------------------------------------------------
//---------------------------------------------------
// CODE TO MOVE BALL OBJECT
function moveBall(){
//MOVEMENT X AND Y
ball.x += ball.spdx;
ball.y += ball.spdy;
//BOUNCE
//X AXIS
if(ball.x > 500-ball.w){
ball.spdx *= -1;
//setup();
score1++;
}
if(ball.x <0){
ball.spdx *= -1;
//setup();
score2++;
}
//Y AXIS
if(ball.y > 500-ball.h){
ball.spdy *= -1;
}
if(ball.y <0){
ball.spdy *= -1;
}
}
// HITS PADDLE
//PLAYER1
if(collision(player1,ball)){
ball.spdx *= -1;
ball.spdy *= -1;
ball.spdx += 0.5;//faster P1
score1++;
}
//PLAYER2
if(collision(player2,ball)){
print(âRIGHT DONEâ);
ball.spdx *= -1;
ball.spdy *= -1;
ball.spdx -= 0.5;//faster P2
score2++;
}
//--------------------------------------------------
//---------------------------------------------------
//DRAWING SCORE ON CANVAS
function drawScore(){
textSize(24);
fill(255,0,0); // RED
text("P1: "+score1,10,25);
fill(60,161,195); // BLUE
text("P2: "+score2,420,25);
}
//-----------------------------------------------------
//---------------------------------------------------
//WHEN PADDLE HITS BALL
function collision(obj1,obj2){
if(
obj1.x + obj1.w > obj2.x
&&
obj1.x < obj2.x + obj2.w
&&
obj2.y + obj2.h > obj1.y
&&
obj2.y < obj1.y + obj1.h
)
{return true;}
else{return false;}
}
//---------------------------------------------------
//---------------USE DEFINED FUNCTIONS----------------|