JavaScript小游戏

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="utf-8">  
    <title>Ping Pong</title>  
    <script src="jquery-1.11.0.min.js"></script>  
    <style>
    	* {  
  box-sizing: border-box;  
  margin: 0px;  
  padding: 0px;  
}  
  
#playground{  
  background: url(images/bg1.png);  
  width: 400px;  
  height: 200px;  
  position: relative;  
}  
  
.paddle{  
  background: #bbf;  
  position: absolute;  
  left: 50px;  
  top: 70px;  
  width: 10px;  
  height: 70px;  
}  
  
#paddleB{  
  left:320px;  
}  
  
#ball{  
  background: #fbb;  
  position: absolute;  
  left: 150px;  
  top: 100px;  
  width: 10px;  
  height: 10px;  
  border-radius: 5px;  
}  
  
.score{  
  font-size: 24px;  
  position: relative;  
  left: 70px;  
  top: 50px;  
}  
  
#scoreboard{  
  position: relative;  
  left: 50px;  
  width: 250px;  
  height: 140px;  
  background: url(images/bg1.png) no-repeat center blue;  
}  
  
#game{  
  position: relative;  
  left: 500px;  
} 
    </style>
</head>  
<body>  
    <header>  
        <h1>Ping Pong</h1>  
    </header>  
  
    <div id="game">  
  
        <div id="scoreboard">  
            <div class="score">player A : <span id="scoreA">0</span></div>  
            <div class="score">player B : <span id="scoreB">0</span></div>  
        </div>  
  
        <div id="playground">  
            <div id="paddleA" class="paddle"></div>  
            <div id="paddleB" class="paddle"></div>  
            <div id="ball"></div>  
        </div>  
          
    </div>  
  
  
    <footer>  

    </footer>  
    <script>
    	window.οnlοad=function(){
    		var KEY = {  
    UP: 38,  
    Down: 40,  
    W: 87,  
    S: 83  
};  
  
var pingpong = {};  
pingpong.pressedKey = [];  
pingpong.ball = {  
    speed: 3,  
    x: 150,  
    y: 100,  
    radius: 5,  
    directionX: 1,  
    directionY: 1  
};  
pingpong.scoreA = 0;  
pingpong.scoreB = 0;  
  
$(function() {  
    pingpong.timer = setInterval(gameloop, 30);  
  
    $(document).keydown(function(e) {  
        pingpong.pressedKey[e.which] = true;  
    });  
    $(document).keyup(function(e) {  
        pingpong.pressedKey[e.which] = false;  
    });  
  
});  
  
function gameloop() {  
    moveBall();  
    movePaddles();  
}  
  
function moveBall() {  
    var playgroundHeight = parseInt($("#playground").height());  
    var playgroundWidth  = parseInt($("#playground").width());  
    var ball = pingpong.ball;  
  
    //check the directions for walls  
    if(ball.y + ball.speed*ball.directionY + 2*ball.radius > playgroundHeight)  
        ball.directionY = -1;  
    if(ball.y + ball.speed*ball.directionY < 0)  
        ball.directionY = 1;  
    if(ball.x + ball.speed*ball.directionX + 2*ball.radius > playgroundWidth)  
        ball.directionX = -1;  
    if(ball.x + ball.speed*ball.directionX < 0)  
        ball.directionX = 1;  
  
    //check the directions for paddles  
    var paddleAX = parseInt($("#paddleA").css("left")) +   
                    parseInt($("#paddleA").css("width"));  
    var paddleABottom = parseInt($("#paddleA").css("top")) +   
                        parseInt($("#paddleA").css("height"));  
    var paddleAUp = parseInt($("#paddleA").css("top"));  
      
    if(ball.x + ball.speed * ball.directionX <= paddleAX)  
        if(ball.y + ball.speed * ball.directionY <= paddleABottom &&   
            ball.y + ball.speed * ball.directionY >= paddleAUp)        
            ball.directionX = 1;          
  
    var paddleBX = parseInt($("#paddleB").css("left")) - 2 * ball.radius;  
    var paddleBBottom = parseInt($("#paddleB").css("top")) +   
                        parseInt($("#paddleB").css("height"));  
    var paddleBUp = parseInt($("#paddleB").css("top"));  
      
    if(ball.x + ball.speed * ball.directionX >= paddleBX)  
        if(ball.y + ball.speed * ball.directionY <= paddleBBottom &&   
            ball.y + ball.speed * ball.directionY >= paddleBUp)        
            ball.directionX = -1;     
  
    //update the position of ball  
    ball.x += ball.speed * ball.directionX;  
    ball.y += ball.speed * ball.directionY;  
  
    setBall(ball.x, ball.y);  
  
    if(ball.x > paddleBX) {    
        ball.x = 250;  
        ball.y = 100;  
        setBall(ball.x, ball.y);  
        ball.directionX = -1;  
        pingpong.scoreA++;  
        $("#scoreA").html(pingpong.scoreA);  
          
        pingpong.pressedKey = [];  
        alert("play A goal, press to continue");  
        for (var i = 100000000 - 1; i >= 0; i--);  
    }  
    if(ball.x < paddleAX) {    
        ball.x = 250;  
        ball.y = 100;  
        setBall(ball.x, ball.y);  
        ball.directionX = 1;  
        pingpong.scoreB++;  
        $("#scoreB").html(pingpong.scoreB);  
          
        pingpong.pressedKey = [];  
        alert("play B goal, press to continue");  
        for (var i = 100000000 - 1; i >= 0; i--);  
    }  
  
}  
  
function setBall(x, y) {  
    $("#ball").css({  
        "left": x,  
        "top":  y     
    });  
}  
  
function movePaddles() {  
    if(pingpong.pressedKey[KEY.UP]) {  
        var top = parseInt($("#paddleB").css("top"));  
        $("#paddleB").css("top", top-5);  
    }  
    if(pingpong.pressedKey[KEY.Down]) {  
        var top = parseInt($("#paddleB").css("top"));  
        $("#paddleB").css("top", top+5);  
    }   
    if(pingpong.pressedKey[KEY.W]) {  
        var top = parseInt($("#paddleA").css("top"));  
        $("#paddleA").css("top", top-5);  
    }  
    if(pingpong.pressedKey[KEY.S]) {  
        var top = parseInt($("#paddleA").css("top"));  
        $("#paddleA").css("top", top+5);  
    }  
} 
}//控制左边上下的是w,s,控制右边上下的是上下箭头
    </script>  
</body>  
</html> 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值