Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //code inside $(function(){} will run after the DOM is loaded and ready
- var KEY = {
- UP: 38,
- DOWN: 40,
- W: 87,
- S: 83
- }
- var pingpong = {
- scoreA : 0, //score for player A
- scoreB : 0 //score for player B
- }
- pingpong.pressedKeys = [];
- pingpong.ball = {
- speed: 5,
- x: 150,
- y: 100,
- directionX: 1,
- directionY: 1
- }
- var WINNER = 3;
- $(function(){
- //set interval to call gameloop every 30 milliseconds
- pingpong.timer = setInterval(gameloop,30);
- //mark down what key is down and up into an array called "pressedKeys"
- $(document).keydown(function(e){
- pingpong.pressedKeys[e.which] = true;
- });
- $(document).keyup(function(e){
- pingpong.pressedKeys[e.which] = false;
- });
- });
- function gameloop() {
- if (pingpong.scoreA < WINNER && pingpong.scoreB < WINNER) {
- moveBall();
- movePaddles();
- }
- else {
- endGame();
- }
- }
- function moveBall() {
- //reference useful variables
- var playgroundHeight = parseInt($("#playground").height());
- var playgroundWidth = parseInt($("#playground").width());
- var ball = pingpong.ball;
- //check playground boundary
- //check bottom edge
- if (ball.y + ball.speed*ball.directionY > playgroundHeight) {
- ball.directionY = -1;
- }
- //check top edge
- if (ball.y + ball.speed*ball.directionY < 0) {
- ball.directionY = 1;
- }
- //check right edge
- if (ball.x + ball.speed*ball.directionX > playgroundWidth) {
- ball.directionX = -1;
- }
- //check left edge
- if (ball.x + ball.speed*ball.directionX < 0) {
- ball.directionX = 1;
- }
- ball.x += ball.speed*ball.directionX;
- ball.y += ball.speed*ball.directionY;
- //check moving paddle here
- //check left paddle
- var paddleAX = parseInt($("#paddleA").css("left"))+parseInt($("#paddleA").css("width"));
- var paddleAYBottom = parseInt($("#paddleA").css("top"))+parseInt($("#paddleA").css("height"));
- var paddleAYTop = parseInt($("#paddleA").css("top"));
- if (ball.x + ball.speed*ball.directionX < paddleAX) {
- if ((ball.y + ball.speed*ball.directionY <= paddleAYBottom) && (ball.y + ball.speed*ball.directionY >= paddleAYTop))
- {
- ball.directionX = 1;
- }
- }
- //check right paddle
- var paddleBX = parseInt($("#paddleB").css("left"))-parseInt($("#ball").css("width"));
- var paddleBYBottom = parseInt($("#paddleB").css("top"))+parseInt($("#paddleB").css("height"));
- var paddleBYTop = parseInt($("#paddleB").css("top"));
- if (ball.x + ball.speed*ball.directionX >= paddleBX) {
- if ((ball.y + ball.speed*ball.directionY <= paddleBYBottom) && (ball.y + ball.speed*ball.directionY >= paddleBYTop))
- {
- ball.directionX = -1;
- }
- }
- //check right edge
- if (ball.x + ball.speed*ball.directionX > playgroundWidth) {
- //player B lost; reset ball.
- pingpong.scoreA++;
- $("#scoreA").html(pingpong.scoreA);
- ball.x = 250;
- ball.y = 100;
- $("#ball").css({
- "left" : ball.x,
- "top" : ball.y
- });
- ball.directionX = -1;
- }
- //check left edge
- if (ball.x + ball.speed*ball.directionX < 0) {
- //player A lost; reset ball.
- pingpong.scoreB++;
- $("#scoreB").html(pingpong.scoreB);
- ball.x = 150;
- ball.y = 100;
- $("#ball").css({
- "left" : ball.x,
- "top" : ball.y
- });
- ball.directionX = 1;
- }
- //actually move ball with speed and direction
- $("#ball").css({
- "left" : ball.x,
- "top" : ball.y
- });
- }
- function movePaddles(){
- //use custom timer to continuously check if a key is pressed
- var playgroundHeight = parseInt($("#playground").height()); //added for edge check
- if (pingpong.pressedKeys[KEY.UP]) { //arrow-up
- //move paddle B up 5 pixels
- var top = parseInt($("#paddleB").css("top"));
- if (top > 0) { //edge check
- $("#paddleB").css("top",top-5);
- }
- }
- if (pingpong.pressedKeys[KEY.DOWN]) { //arrow-down
- //move paddle B down 5 pixels
- var top = parseInt($("#paddleB").css("top"));
- if (top < (playgroundHeight-70)) { //edge check
- $("#paddleB").css("top",top+5);
- }
- }
- if (pingpong.pressedKeys[KEY.W]) { //w
- //move paddle A up 5 pixels
- var top = parseInt($("#paddleA").css("top"));
- if (top > 0) { //edge check
- $("#paddleA").css("top",top-5);
- }
- }
- if (pingpong.pressedKeys[KEY.S]) { //s
- //move paddle A down 5 pixels
- var top = parseInt($("#paddleA").css("top"));
- if (top < (playgroundHeight-70)) { //edge check
- $("#paddleA").css("top",top+5);
- }
- }
- }
- function endGame() {
- if (pingpong.scoreA >= WINNER) {
- $("#winner").html("Player A has won the game!")
- }
- else {
- $("#winner").html("Player B has won the game!")
- }
- clearInterval(pingpong.timer);
- }
Add Comment
Please, Sign In to add comment