2015-10-04 81 views
4

我已經用Javascript編寫了一個簡單的pong遊戲。但我有兩個問題。如何在Javascript pong遊戲中恰當地使球偏轉?

  1. 我不知道爲什麼球沒有根據代碼偏離電腦的paddle,即「playerB」。

  2. 當球從槳偏轉時,我該如何創建一些隨機性?目前這款遊戲看起來非常重複。

我該如何解決這兩個問題?

Link to Jsfiddle

HTML

<!DOCTYPE html> 
<html> 
<head> 
    <title>Pong game</title> 
    <script type="text/javascript" src="jquery-2.1.4.min.js"></script> 
    <script type="text/javascript" src="js/pong.js"></script> 
    <link rel="stylesheet" href="css/pong.css"> 
</head> 
<body> 
    <header> 
    <p> 
    Computer:<span id="scoreB"></span> Human:<span id="scoreA"></span> 
    </p> 
    </header> 
    <div id="playground"> 
    <div id="ball"></div> 
    <div id="playerA" class="paddle right"></div> 
    <div id="playerB" class="paddle left"></div> 
    </div> 
</body> 
</html> 

CSS

html,body{ 
    margin: 0 atuo; 
    height:100%; 
} 
#playground{ 
    position: relative; 
    width:700px; 
    height:400px; 
    border: 1px solid grey; 
    overflow:hidden; 
} 
#ball{ 
    position: absolute; 
    background: #fbb; 
    width:20px; 
    height:20px; 
    left:200px; 
    top:100px; 
    border-radius:10px; 
} 
.paddle{ 
    position: absolute; 
    border:1px solid grey; 
    width:10px; 
    height:80px; 
} 
.left.paddle{ 
    left:0px; 
    background-color: green; 
} 
.right.paddle{ 
    right:0px; 
    background-color: blue; 
} 

的Javascript

$(document).ready(function(){ 
    var values = { 
    ball:{ 
     speed:5, 
     x:50, 
     y:50, 
     directionX: 1, 
     directionY: 1, 
     radius: 10 
    }, 
    playground:{ 
     width: parseInt($("#playground").width()), 
     height: parseInt($("#playground").height()) 
    }, 
    playerA:{ 
     y:0, 
     top: $("#playerA").offset().top, 
     height: $("#playerA").height(), 
     width: $("#playerA").width(), 
     score:0 
    }, 
    playerB:{ 
     y:0, 
     top: $("#playerB").offset().top, 
     height: $("#playerB").height(), 
     width: $("#playerB").width(), 
     score:0 
    } 
    }; 
    //collision detection 
    function hitsTopBottom(){ 
    var y = values.ball.y; 
    return y>values.playground.height-20 || y < 0; 
    } 
    function hitsRight(){ 
    return values.ball.x > values.playground.width-20; 
    } 
    function hitsLeft(){ 
    return values.ball.x < 0; 
    } 
    //ball hits playerA's paddle. Works properly but open to improvement 
    function hitsPlayerA(){ 
    var ballX = values.ball.x; 
    var ballY = values.ball.y; 
    var playerAY = values.playerA.y; 
    return ballY<=playerAY+values.playerA.height && ballY>playerAY && ballX>=700-values.playerA.width-values.ball.radius; 
    } 
    //ball hits playerB's paddle. Doesn't work at all 
    function hitsPlayerB(){ 
    var ballX = values.ball.x; 
    var ballY = values.ball.y; 
    var playerBY = values.playerB.y; 
    return ballY<=playerBY+values.playerB.height && ballY>=playerBY && ballX<=values.playerB.width-values.ball.radius; 
    } 
    //rendering the position of the ball 
    function renderball(){ 
    $("#ball").css("left",values.ball.x); 
    $("#ball").css("top",values.ball.y); 
    $("#scoreB").text(values.playerB.score); 
    $("#scoreA").text(values.playerA.score); 
    } 
    //moving ball 
    function moveball(){ 
    if(hitsTopBottom()){ 
     values.ball.directionY *= -1; 
    } 
    if(hitsPlayerA()){ 
     values.ball.directionX *= -1; 
    } 
    if(hitsPlayerB()){ 
     values.ball.directionX = 1; 
    } 
    if(hitsRight()){ 
     values.ball.x = 200; 
     values.ball.y = 200; 
     values.playerB.score += 1; 
    } 
    if(hitsLeft()){ 
     values.ball.x = 200; 
     values.ball.y = 200; 
     values.playerA.score += 1; 
    } 
    values.ball.x += values.ball.speed*values.ball.directionX; 
    values.ball.y += values.ball.speed*values.ball.directionY; 
    renderball(); 
    var playerB_pos = values.ball.y - values.playerB.height/2; 
    $("#playerB").css("top",playerB_pos); 
    } 
    //player movements 
    $("#playground").mousemove(function(e){ 
    values.playerA.y = e.pageY-values.playerA.top-parseInt($("#playerA").height()/2); 
    $("#playerA").css("top",values.playerA.y); 
    }); 
    setInterval(moveball,1000/30); 
}); 
+1

關於「隨意性」:我建議槳的速度應該改變球的垂直速度。目前我認爲你只能反轉水平速度,而垂直速度不會改變。 – Oriol

+0

你可能會考慮在http://codereview.stackexchange.com/ :)上發佈這個問題 – OddDev

+1

@OddDev這將被視爲代碼審查題外話,因爲這裏的代碼不按預期工作,我們被要求修復。 –

回答

1

你忘了編輯的實際playerB.y值(你只是改變了CSS) :

var playerB_pos = values.ball.y - values.playerB.height/2; 
values.playerB.y = playerB_pos; // Forgot this! 
$("#playerB").css("top",playerB_pos); 

工作的jsfiddle:http://jsfiddle.net/yb290ow9/6/

關於隨機性:你可以把槳「有彈性」:改變球的速度,不只是方向

2

球未偏轉因爲它的位置沒有被存儲,所以關閉了電腦槳。通過在循環中設置values.playerB.y = playerB_pos它現在會正確反彈。然而,因爲它與球的y位置相同,所以計算機永遠不會丟失。爲解決這個問題,並添加一些隨機性我用jQuery的動畫功能,調整點和假冒勢頭的電腦槳:

if(!isAnimating) { 
    isAnimating = true; 
    $("#playerB").animate({"top": playerB_pos}, { 
     duration: (values.ball.speed*16), 
     easing: "linear", 
     step: function(now, fx) { 
     values.playerB.y = parseInt($("#playerB").offset().top - parseInt($("#playerA").height())); 
     }, 
     complete: function() { 
     isAnimating = false; 
     } 
    }); 
    } 

你可以看到它在這裏的行動:http://jsfiddle.net/yb290ow9/5/