tronds

Pong in Java; using LibGDX

Nov 19th, 2013
278
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.12 KB | None | 0 0
  1. package com.humborstad.GDXTest;
  2.  
  3. import com.badlogic.gdx.ApplicationListener;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.Input.Keys;
  6. import com.badlogic.gdx.graphics.FPSLogger;
  7. import com.badlogic.gdx.graphics.GL10;
  8. import com.badlogic.gdx.graphics.OrthographicCamera;
  9. import com.badlogic.gdx.graphics.Texture;
  10. import com.badlogic.gdx.graphics.Texture.TextureFilter;
  11. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  12. import com.badlogic.gdx.graphics.g2d.Sprite;
  13. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  14. import com.badlogic.gdx.graphics.g2d.TextureRegion;
  15. import com.badlogic.gdx.math.Rectangle;
  16.  
  17. public class GDXTest implements ApplicationListener {
  18.     private OrthographicCamera camera;
  19.     private SpriteBatch batch;
  20.     private Texture ballTexture;
  21.     private Sprite ballSprite;
  22.     private Texture paddleTexture;
  23.     private Sprite[] paddleSprite = new Sprite[2];
  24.    
  25.     private Rectangle ballRectangle;
  26.     private Rectangle[] paddleRectangle = new Rectangle[2];
  27.    
  28.     //We're twoplayers
  29.     private int[] player = new int[2];
  30.    
  31.     private float ballXMoveSpeed = 300;
  32.     private float ballYMoveSpeed = 300;
  33.  
  34.     private float w;
  35.     private float h;
  36.    
  37.     private FPSLogger log = new FPSLogger();
  38.    
  39.     @Override
  40.     public void create() {
  41.         w = Gdx.graphics.getWidth();
  42.         h = Gdx.graphics.getHeight();
  43.        
  44.         camera = new OrthographicCamera();
  45.         camera.setToOrtho(false, w, h);
  46.        
  47.         batch = new SpriteBatch();
  48.        
  49.         ballTexture = new Texture(Gdx.files.internal("img/ball.png"));
  50.         ballTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
  51.        
  52.         paddleTexture = new Texture(Gdx.files.internal("img/paddle.png"));
  53.         paddleTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
  54.        
  55.         TextureRegion ballRegion = new TextureRegion(ballTexture, 0, 0, 16, 16);
  56.         TextureRegion paddleRegion = new TextureRegion(paddleTexture, 0, 0, 16, 64);
  57.        
  58.         ballSprite = new Sprite(ballRegion);
  59.         //ballSprite.setSize(0.9f, 0.9f * ballSprite.getHeight() / ballSprite.getWidth());
  60.        
  61.         // create a Rectangle to logically represent the bucket
  62.         ballRectangle = new Rectangle();
  63.         ballRectangle.x =  w/2; // center the bucket horizontally
  64.         ballRectangle.y = h/2; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
  65.         ballRectangle.width = 16;
  66.         ballRectangle.height = 16;
  67.        
  68.         ballSprite.setOrigin(ballSprite.getWidth()/2, ballSprite.getHeight()/2);
  69.         ballSprite.setPosition(ballRectangle.x, ballRectangle.y);
  70.        
  71.         paddleSprite[0] = new Sprite(paddleRegion);
  72.         paddleSprite[1] = new Sprite(paddleRegion);
  73.         //ballSprite.setSize(0.9f, 0.9f * ballSprite.getHeight() / ballSprite.getWidth());
  74.        
  75.         // create a Rectangle to logically represent the bucket
  76.         paddleRectangle[0] = new Rectangle();
  77.         paddleRectangle[0].width = 16;
  78.         paddleRectangle[0].height = 64;
  79.         paddleRectangle[0].x = 30; // center the bucket horizontally
  80.         paddleRectangle[0].y = h/2; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
  81.        
  82.         paddleRectangle[1] = new Rectangle();
  83.         paddleRectangle[1].width = 16;
  84.         paddleRectangle[1].height = 64;
  85.         paddleRectangle[1].x = w - 30 - paddleRectangle[1].width; // center the bucket horizontally
  86.         paddleRectangle[1].y = h/2; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
  87.        
  88.         paddleSprite[0].setOrigin(paddleSprite[0].getWidth()/2, paddleSprite[0].getHeight()/2);
  89.         paddleSprite[0].setPosition(paddleRectangle[0].x, paddleRectangle[0].y);
  90.        
  91.         for(int i = 0; i < player.length; i++){
  92.             player[i] = 0;
  93.         }
  94.     }
  95.  
  96.     @Override
  97.     public void dispose() {
  98.         batch.dispose();
  99.         ballTexture.dispose();
  100.         paddleTexture.dispose();
  101.     }
  102.  
  103.     @Override
  104.     public void render() {     
  105.         // clear the screen with a dark blue color. The
  106.         // arguments to glClearColor are the red, green
  107.         // blue and alpha component in the range [0,1]
  108.         // of the color to be used to clear the screen.
  109.         Gdx.gl.glClearColor(0, 0, 0.2f, 1);
  110.         Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  111.          
  112.         // tell the camera to update its matrices.
  113.         camera.update();
  114.         // tell the SpriteBatch to render in the
  115.         // coordinate system specified by the camera.
  116.         batch.setProjectionMatrix(camera.combined);
  117.        
  118.         if(Gdx.input.isKeyPressed(Keys.S)) paddleRectangle[0].y -= 400 * Gdx.graphics.getDeltaTime();
  119.         if(Gdx.input.isKeyPressed(Keys.W)) paddleRectangle[0].y += 400 * Gdx.graphics.getDeltaTime();
  120.         if(Gdx.input.isKeyPressed(Keys.DOWN)) paddleRectangle[1].y -= 400 * Gdx.graphics.getDeltaTime();
  121.         if(Gdx.input.isKeyPressed(Keys.UP)) paddleRectangle[1].y += 400 * Gdx.graphics.getDeltaTime();
  122.        
  123.         //Uh, yay for readability.
  124.         for(Rectangle i : paddleRectangle){
  125.             if(i.y < 0) i.y = 0;
  126.             if(i.y > h - i.height) i.y = h - i.height;
  127.         }
  128.        
  129.         if(ballRectangle.x + ballRectangle.width < 0 ){
  130.             player[1]++;
  131.             ballRectangle.x = w - w/4;
  132.             //ballXMoveSpeed =- ballXMoveSpeed;
  133.         } else if(ballRectangle.x + ballRectangle.width > w){
  134.             player[0]++;
  135.             ballRectangle.x = w/4;
  136.             //ballXMoveSpeed =- ballXMoveSpeed;
  137.         } else if (ballRectangle.y < 0 || ballRectangle.y + ballRectangle.height > h){
  138.             ballYMoveSpeed =- ballYMoveSpeed;
  139.         }
  140.        
  141.         for(Rectangle i : paddleRectangle){
  142.             if(ballRectangle.overlaps(i)){
  143.                 ballXMoveSpeed =- ballXMoveSpeed;
  144.             }
  145.         }
  146.        
  147.         batch.setProjectionMatrix(camera.combined);
  148.         batch.begin();
  149.             ballSprite.setPosition(ballRectangle.x, ballRectangle.y);
  150.             ballSprite.draw(batch);
  151.             //This is a bit sketchy. We don't certainly know if paddleSprite.length == paddleRectangle.length
  152.             for(int i = 0; i < paddleSprite.length; i ++){
  153.                 BitmapFont font = new BitmapFont();
  154.                 font.draw(batch, String.valueOf(player[1]), w - 50, h - 50);
  155.                 font.draw(batch, String.valueOf(player[0]), 50, h - 50);
  156.                 paddleSprite[i].setPosition(paddleRectangle[i].x, paddleRectangle[i].y);
  157.                 paddleSprite[i].draw(batch);
  158.             }
  159.             log.log();
  160.         batch.end();
  161.        
  162.         ballRectangle.x += ballXMoveSpeed * Gdx.graphics.getDeltaTime();
  163.         ballRectangle.y += ballYMoveSpeed * Gdx.graphics.getDeltaTime();
  164.     }
  165.  
  166.     @Override
  167.     public void resize(int width, int height) {
  168.     }
  169.  
  170.     @Override
  171.     public void pause() {
  172.     }
  173.  
  174.     @Override
  175.     public void resume() {
  176.     }
  177. }
Advertisement
Comments
  • Comment was deleted
Add Comment
Please, Sign In to add comment