Advertisement
Guest User

LibGDX Pong

a guest
May 3rd, 2014
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1. package com.connorkuehl.mygdxgame;
  2.  
  3. import com.badlogic.gdx.ApplicationListener;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.Input;
  6. import com.badlogic.gdx.graphics.Color;
  7. import com.badlogic.gdx.graphics.GL10;
  8. import com.badlogic.gdx.graphics.OrthographicCamera;
  9. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  10. import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
  11. import com.badlogic.gdx.math.Rectangle;
  12.  
  13. public class PongGame implements ApplicationListener {
  14.    
  15.     // game's width
  16.     public static int WIDTH;
  17.     // game's height
  18.     public static int HEIGHT;
  19.     // camera to render things
  20.     OrthographicCamera cam;
  21.     // we need this shape renderer to render our shapes.
  22.     ShapeRenderer sr;
  23.     // player 1's rectangle
  24.     Rectangle p1;
  25.     // player 2's rectangle
  26.     Rectangle p2;
  27.     // player's width
  28.     int playerWidth = 15;
  29.     // player's height
  30.     int playerHeight = 100;
  31.     // speed at which players can move
  32.     int playerSpeed = 125;
  33.     // speed at which the pong ball moves
  34.     int pongSpeed = 300;
  35.     // default scores
  36.     int playerOneScore, playerTwoScore;
  37.     // the ball
  38.     Rectangle pong;
  39.     // chance that the trajectory will change
  40.     int pongYspeed = 50;
  41.     // randomly generated number to generate probability of pong's trajectory
  42.     double changePongY;
  43.     // the direction that the pong will move
  44.     double pongDirection;
  45.     // whether or not the pong will move up
  46.     boolean pongUp = false;
  47.     // whether or not the pong will move down
  48.     boolean pongDown = false;
  49.     // whether or not the pong is moving left
  50.     boolean pongLeft = false;
  51.     // whether or not the pong is moving right
  52.     boolean pongRight = false;
  53.     // left boundary
  54.     Rectangle leftBound;
  55.     // right boundary
  56.     Rectangle rightBound;
  57.    
  58.    
  59.     @Override
  60.     public void create() { 
  61.         // initialize variables and objects
  62.         WIDTH = Gdx.graphics.getWidth();
  63.         HEIGHT = Gdx.graphics.getWidth();
  64.        
  65.         cam = new OrthographicCamera(WIDTH, HEIGHT);
  66.         cam.setToOrtho(false);
  67.         sr = new ShapeRenderer();
  68.  
  69.         p1 = new Rectangle(30, HEIGHT / 4, playerWidth, playerHeight);
  70.         p2 = new Rectangle(WIDTH - 45, HEIGHT / 4, playerWidth, playerHeight);
  71.         pong = new Rectangle(WIDTH / 2, HEIGHT / 3, 5, 5);
  72.         leftBound = new Rectangle(0, 0, 3, HEIGHT);
  73.         rightBound = new Rectangle(WIDTH, 0, 3, HEIGHT);
  74.        
  75.         playerOneScore = 0;
  76.         playerTwoScore = 0;
  77.         pongDirection = Math.random();
  78.        
  79.         if(pongDirection > 0.5)
  80.         {
  81.             pongLeft = true;
  82.         } else {
  83.             pongRight = true;
  84.         }
  85.  
  86.     }
  87.  
  88.     @Override
  89.     public void dispose() {
  90.         sr.dispose();
  91.     }
  92.  
  93.     @Override
  94.     public void render() {     
  95.         Gdx.gl.glClearColor(0, 0, 0, 1);
  96.         Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  97.        
  98.         cam.update();
  99.        
  100.         sr.setProjectionMatrix(cam.combined);
  101.         sr.begin(ShapeType.Filled);
  102.         sr.setColor(Color.WHITE);
  103.         sr.circle(pong.x, pong.y, 5);
  104.         sr.rect(p1.x, p1.y, p1.width, p1.height);
  105.         sr.rect(p2.x, p2.y, p2.width, p2.height);
  106.         sr.end();
  107.        
  108.        
  109.        
  110.         // player 1 controls
  111.         if(Gdx.input.isKeyPressed(Input.Keys.W)) { p1.y += playerSpeed * Gdx.graphics.getDeltaTime(); }
  112.         if(Gdx.input.isKeyPressed(Input.Keys.S)) { p1.y -= playerSpeed * Gdx.graphics.getDeltaTime(); }
  113.         // player 2 controls
  114.         if(Gdx.input.isKeyPressed(Input.Keys.UP)) { p2.y += playerSpeed * Gdx.graphics.getDeltaTime(); }
  115.         if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) { p2.y -= playerSpeed * Gdx.graphics.getDeltaTime(); }
  116.        
  117.         changePongY = Math.random();
  118.        
  119.         // determine pong's direction and change movement appropriately
  120.         if(pongRight && !pongLeft)
  121.         {
  122.             pong.x += pongSpeed * Gdx.graphics.getDeltaTime();
  123.         }
  124.         else if(pongLeft && !pongRight)
  125.         {
  126.             pong.x -= pongSpeed * Gdx.graphics.getDeltaTime();
  127.         }
  128.        
  129.         // change the up/down trajectory of the pong if necessary
  130.         if(pongUp && !pongDown)
  131.         {
  132.             pong.y += pongYspeed * Gdx.graphics.getDeltaTime();
  133.         }
  134.         else if(pongDown && !pongUp)
  135.         {
  136.             pong.y -= pongYspeed * Gdx.graphics.getDeltaTime();
  137.         }
  138.        
  139.         // collision
  140.         if(pong.overlaps(p1))
  141.         {
  142.             pongRight = true;
  143.             pongLeft = false;
  144.            
  145.             checkPongTrajectory();
  146.         }
  147.         else if(pong.overlaps(p2))
  148.         {
  149.             pongRight = false;
  150.             pongLeft = true;
  151.            
  152.             changePongY = Math.random();
  153.         }
  154.        
  155.         // reset gameboard if the pong goes out of the boundaries
  156.         else if(pong.overlaps(leftBound))
  157.         {
  158.             resetBoard();
  159.             playerTwoScore += 1;
  160.         }
  161.        
  162.         else if(pong.overlaps(rightBound))
  163.         {
  164.             resetBoard();
  165.             playerOneScore += 1;
  166.         }
  167.        
  168.     }
  169.  
  170.     private void resetBoard() {
  171.         pong.x = WIDTH / 2;
  172.         pong.y = HEIGHT / 3;
  173.         p1.x = 30;
  174.         p1.y = HEIGHT / 4;
  175.         p2.x = WIDTH - 45;
  176.         p2.y = HEIGHT / 4;
  177.         pongUp = false;
  178.         pongDown  = false;
  179.     }
  180.    
  181.     // determine whether or not the pong will go up or down.
  182.     private void checkPongTrajectory() {
  183.         changePongY = Math.random();
  184.         // if the pong is in the top half of the board and the trajectory shifts, send it downwards
  185.         if((changePongY > 0.3) && (pong.y > (HEIGHT / 2))) {
  186.             pongUp = false;
  187.             pongDown = true;
  188.         }
  189.         // if the pong is the bottom half of the board and the trajectory shifts, send it upwards
  190.         else if((changePongY < 0.7) && (pong.y < (HEIGHT / 2))) {
  191.             pongUp = true;
  192.             pongDown = false;
  193.         }
  194.         // if the trajectory doesn't shift at all, don't change the direction of the pong.
  195.         else {
  196.             pongUp = false;
  197.             pongDown = false;
  198.         }
  199.     }
  200.  
  201.     @Override
  202.     public void resize(int width, int height) {
  203.     }
  204.  
  205.     @Override
  206.     public void pause() {
  207.     }
  208.  
  209.     @Override
  210.     public void resume() {
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement