2016-03-08 79 views
0

我想做一个乒乓球比赛。目前,我得到球移动,并按下按键时,两个桨移动。但球没有反弹离开桨。有代码反弹player2桨,但它似乎不工作。这是我知道的很多代码。你能帮我弄清楚什么是错的吗?Ping Pong在JavaScript中?

"use strict"; 
 
// Variables 
 

 
var c = document.getElementById("sCanvas"); 
 
var ctx = sCanvas.getContext("2d"); 
 
var cHeight = sCanvas.height; 
 
var cWidth = sCanvas.width; 
 

 
//Objects 
 

 
//create paddle object 
 

 
class Paddle { 
 
    constructor(x, y) { 
 
    this.colour = "red"; 
 
    this.xPoss = x; 
 
    this.yPoss = y; 
 
    this.width = 12; 
 
    this.height = 60; 
 
    this.speed = 3; 
 
    } 
 

 
    drawMe() { 
 
    ctx.fillStyle = this.colour; 
 
    ctx.fillRect(this.xPoss, this.yPoss, this.width, this.height); 
 
    } 
 
} // end paddle object 
 

 
//create the sphere object 
 
class Sphere { 
 
    constructor() { 
 
    this.radius = (10); 
 
    this.colour = "blue"; 
 
    this.xPos = 65; //Math.random() * cWidth; 
 
    this.yPos = 100; //Math.random() * cHeight; 
 
    this.speedY = 5; //* Math.random(); 
 
    this.speedX = 5; //* Math.random(); 
 
    } 
 

 
    drawMe() { 
 
     //method to draw itself 
 
     ctx.beginPath(); 
 
     ctx.arc(this.xPos, this.yPos, this.radius, 0, Math.PI * 2, true); 
 
     ctx.fillStyle = this.colour; 
 
     ctx.fill(); 
 

 
    } 
 
    //method to move itself 
 
    moveMe() { 
 
     this.yPos += this.speedY; 
 
     this.xPos += this.speedX; 
 

 
     //bounce off the bottom wall 
 
     if (this.yPos > cHeight - this.radius) { 
 
     this.speedY = -this.speedY; 
 

 
     } //bounce off the top wall 
 
     else if (this.yPos < 0 + this.radius) { 
 
     this.speedY = -this.speedY; 
 
     } 
 

 
     //stop ball if hit right side 
 
     if (this.xPos > cWidth) { 
 
     this.speedX = 0; 
 
     this.speedY = 0; 
 

 
     } 
 
     //bounce off player 2 paddle 
 
     else if (this.xPos > player2.xPoss && (this.yPos > player2.yPoss && this.yPos < (player2.yPoss + player2.height))) { 
 
     this.speedX = -this.speedX; 
 

 
     } 
 

 
    } 
 
    //end moveMe function 
 
} // end Sphere object 
 

 
//****************** 
 
// create game objects 
 
//****************** 
 
var ball = new Sphere(); 
 
var player1 = new Paddle(10, 150); 
 
var player2 = new Paddle(580, 150); 
 

 
//********************* 
 
// Deal with key presses 
 
// ********************** 
 

 
var keysDown = []; //empty array to store which keys are being held down 
 

 

 
window.addEventListener("keydown", function(event) { 
 
    keysDown[event.keyCode] = true; //store the code for the key being pressed 
 
}); 
 

 
window.addEventListener("keyup", function(event) { 
 
    delete keysDown[event.keyCode]; 
 
}); 
 

 
function checkKeys() { 
 

 
    if (keysDown[90]) { 
 
    if (player1.yPoss > 0) { 
 
     player1.yPoss = -player1.speed; //z 
 
    } 
 
    } 
 

 
    if (keysDown[88]) { 
 
    if (player1.yPoss < (cHeight - player1.height)) { 
 
     player1.yPoss += player1.speed; //x 
 
    } 
 
    } 
 

 
    if (keysDown[190]) { 
 
    if (player2.yPoss > 0) { 
 
     player2.yPoss = -player2.speed; //"." 
 
    } 
 
    } 
 

 
    if (keysDown[188]) { 
 
    if (player2.yPoss < (cHeight - player2.height)) { 
 
     player2.yPoss += player2.speed; //"," 
 
    } 
 
    } 
 

 
} 
 

 
// your 2 new sets of code here for 2 more keys for player 2 
 

 

 

 

 

 
//********************* 
 
// Make the score board 
 
// ********************** 
 

 

 

 
//********************* 
 
// launch the ball from the centre, left and right, on space bar 
 
// ********************** 
 

 

 
function render() { 
 
    requestAnimationFrame(render); 
 
    ctx.clearRect(0, 0, cWidth, cHeight); 
 
    ball.drawMe(); 
 
    ball.moveMe(); 
 
    player1.drawMe(); 
 
    player2.drawMe(); 
 
    checkKeys(); 
 

 
} 
 

 
render(); //set the animation and drawing on canvas going
<canvas id="sCanvas" width="600" height="400" style="border: solid;"></canvas>

+0

我建议'console.log'ing出这表明发生了冲突的条件。这会告诉你哪一个不正确。 –

回答

0

我添加了一个简单的命中盒和检测器和图中黑色的框来说明。为了一致性,将xPoss,yPoss更改为xPos,yPos。在桨板运动上将= -更改为-=(向上移动时捕捉到顶部)。更改了class以及JavaScript函数和原型方法。

这应该足以让我们看看JavaScript如何实现对象以及如何进行简单的点击检测。这不是创建对象的唯一方式,但它接近您正在尝试执行的操作。

更新

做了一些改动,以打/冲突检测。现在可以从桨的底部和顶部击球。

"use strict"; 
 
// Variables 
 

 
var c = document.getElementById("sCanvas"); 
 
var ctx = sCanvas.getContext("2d"); 
 
var cHeight = sCanvas.height; 
 
var cWidth = sCanvas.width; 
 

 
//Objects 
 

 
//create paddle object 
 

 
function Paddle(x, y) { 
 

 
    this.colour = "red"; 
 
    this.xPos = x; 
 
    this.yPos = y; 
 
    this.width = 12; 
 
    this.height = 60; 
 
    this.speed = 3; 
 

 
} 
 

 
Paddle.prototype.drawMe = function() { 
 
    ctx.fillStyle = this.colour; 
 
    ctx.fillRect(this.xPos, this.yPos, this.width, this.height); 
 
}; // end paddle object 
 

 

 
/***** BEGIN COLLISION DECTECTION FUNCTIONS *****/ 
 
// optimized collision of boxes - Does a hit b? 
 
function hit(a, b) { 
 
    
 
    // Return immediately when the objects aren't touching. 
 
    if (
 
    a.x2 < b.x || // a.right is before b.left 
 
    b.x2 < a.x || // b.right is before a.left 
 
    a.y2 < b.y || // a.bottom is before b.top 
 
    b.y2 < a.y // b.bottom is before a.top 
 
) { 
 
    return false; 
 
    } 
 

 
    // The objects are touching. It is a hit or collision. 
 
    return true; 
 
} 
 

 
// does a hit the top of b? 
 
function hitTop(a, b) { 
 
    return (a.y2 > b.y && a.y < b.y) ? true : false; 
 
} 
 

 
// does a hit the bottom of b? 
 
function hitBottom(a, b) { 
 
    return (a.y < b.y2 && a.y2 > b.y2) ? true : false; 
 
} 
 

 
// Creates an obect of x, y, x2, y2 for hit detection. 
 
function hitObj(obj) { 
 

 
    var h = {x:0, y:0, x2:0, y2:0}; 
 
    
 
    if (obj.radius) { 
 
    h.x = obj.xPos - obj.radius; 
 
    h.x2 = obj.xPos + obj.radius; 
 
    h.y = obj.yPos - obj.radius; 
 
    h.y2 = obj.yPos + obj.radius; 
 
    } else { 
 
    h.x = obj.xPos; 
 
    h.x2 = obj.xPos + obj.width | (obj.radius * 2); 
 
    h.y = obj.yPos; 
 
    h.y2 = obj.yPos + obj.height | (obj.radius * 2); 
 
    } 
 
    
 
    // draw hit box - uncomment to see hit detection 
 
    /* 
 
    ctx.save(); 
 
    ctx.strokeStyle = "#000000"; 
 
    ctx.lineWidth = 5; 
 
    ctx.strokeRect(h.x, h.y, h.x2 - h.x, h.y2 - h.y); 
 
    ctx.restore(); 
 
    */ 
 

 
    return h; 
 
} 
 
/***** END COLLISION DETECTION FUNCTIONS *****/ 
 

 
//create the sphere object 
 
function Sphere() { 
 

 
    this.radius = (10); 
 
    this.colour = "blue"; 
 
    this.xPos = 25; //Math.random() * cWidth; 
 
    this.yPos = 5; //Math.random() * cHeight; 
 
    this.speedY = 5; //* Math.random(); 
 
    this.speedX = 5; //* Math.random(); 
 
} 
 

 
Sphere.prototype.drawMe = function() { 
 
    //method to draw itself 
 
    ctx.beginPath(); 
 
    ctx.arc(this.xPos, this.yPos, this.radius, 0, Math.PI * 2, true); 
 
    ctx.fillStyle = this.colour; 
 
    ctx.fill(); 
 
}; 
 

 
Sphere.prototype.moveMe = function() { 
 
    //method to move itself 
 

 
    // save start position. change back to start 
 
    // position when there's a hit so we don't 
 
    // get stuck in an object. 
 
    var pos = { 
 
    x: this.xPos, 
 
    y: this.yPos 
 
    }; 
 

 
    // move 
 
    this.yPos += this.speedY; 
 
    this.xPos += this.speedX; 
 

 
    //bounce off the bottom wall 
 
    if (this.yPos > cHeight - this.radius) { 
 
    this.yPos = pos.y; 
 
    this.speedY = -this.speedY; 
 

 

 
    } //bounce off the top wall 
 
    else if (this.yPos < 0 + this.radius) { 
 
    this.yPos = pos.y; 
 
    this.speedY = -this.speedY; 
 
    } 
 
/* 
 
    //stop ball if hit right side 
 
    if (this.xPos > cWidth) { 
 
    this.speedX = 0; 
 
    this.speedY = 0; 
 

 
    } 
 
*/ 
 
    // Bounce off left and right sides. 
 
    if ((this.xPos+this.radius) >= cWidth || this.xPos <= 0) { 
 
    this.xPos = pos.x; 
 
    this.speedX = -this.speedX; 
 
    } 
 
    
 
    //bounce off player paddle 
 
    else { 
 
    
 
    var pad1 = hitObj(player1); 
 
    var pad2 = hitObj(player2); 
 
    var ball = hitObj(this); 
 
     
 
    if (hit(ball, pad2)) { 
 
     // hit player2 
 
     this.xPos = pos.x; 
 
     this.speedX *= -1; 
 
     // if the ball travels down and hits top OR 
 
     // if the ball travels up and hits bottom then bounce back 
 
     if (
 
     (this.speedY > 0 && hitTop(ball, pad2)) ||  
 
     (this.speedY < 0 && hitBottom(ball, pad2)) 
 
    ) { 
 
     this.yPos = pos.y; 
 
     this.speedY *= -1; 
 
     } 
 
    } else if (hit(ball, pad1)) { 
 
     // hit player1 
 
     this.xPos = pos.x; 
 
     this.speedX *= -1; 
 
     // if the ball travels down and hits top OR 
 
     // if the ball travels up and hits bottom then bounce back 
 
     if (
 
     (this.speedY > 0 && hitTop(ball, pad1)) ||  
 
     (this.speedY < 0 && hitBottom(ball, pad1)) 
 
    ) { 
 
     this.yPos = pos.y; 
 
     this.speedY *= -1; 
 
     } 
 
    } 
 
    } 
 
}; 
 
//end moveMe function 
 

 

 
//****************** 
 
// create game objects 
 
//****************** 
 
var ball = new Sphere(); 
 
var player1 = new Paddle(10, 150); 
 
var player2 = new Paddle(580, 150); 
 

 
//********************* 
 
// Deal with key presses 
 
// ********************** 
 

 
var keysDown = []; //empty array to store which keys are being held down 
 

 

 
window.addEventListener("keydown", function(event) { 
 
    keysDown[event.keyCode] = true; //store the code for the key being pressed 
 
}); 
 

 
window.addEventListener("keyup", function(event) { 
 
    delete keysDown[event.keyCode]; 
 
}); 
 

 
function checkKeys() { 
 

 
    if (keysDown[90]) { 
 
    if (player1.yPos > 0) { 
 
     player1.yPos -= player1.speed; //z 
 

 
    } 
 
    } 
 

 
    if (keysDown[88]) { 
 
    if (player1.yPos < (cHeight - player1.height)) { 
 
     player1.yPos += player1.speed; //x 
 
    } 
 
    } 
 

 
    if (keysDown[190]) { 
 
    if (player2.yPos > 0) { 
 
     player2.yPos -= player2.speed; //"." 
 
    } 
 
    } 
 

 
    if (keysDown[188]) { 
 
    if (player2.yPos < (cHeight - player2.height)) { 
 
     player2.yPos += player2.speed; //"," 
 
    } 
 
    } 
 

 
} 
 

 
// your 2 new sets of code here for 2 more keys for player 2 
 

 

 

 

 

 
//********************* 
 
// Make the score board 
 
// ********************** 
 

 

 

 
//********************* 
 
// launch the ball from the centre, left and right, on space bar 
 
// ********************** 
 

 

 
function render() { 
 
    requestAnimationFrame(render); 
 
    ctx.clearRect(0, 0, cWidth, cHeight); 
 
    ball.drawMe(); 
 
    ball.moveMe(); 
 
    player1.drawMe(); 
 
    player2.drawMe(); 
 
    checkKeys(); 
 

 
} 
 

 
render(); //set the animation and drawing on canvas going
#sCanvas { 
 
    width: 300px; 
 
    height: 200px; 
 
}
<canvas id="sCanvas" width="600" height="400" style="border: solid;"></canvas>