Advertisement
Guest User

Untitled

a guest
Nov 16th, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.13 KB | None | 0 0
  1. package com.freakout;
  2. import java.util.Iterator;
  3.  
  4. import com.badlogic.gdx.*;
  5. import com.badlogic.gdx.Input.Keys;
  6. import com.badlogic.gdx.graphics.GL10;
  7. import com.badlogic.gdx.graphics.OrthographicCamera;
  8. import com.badlogic.gdx.graphics.Texture;
  9. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  10. import com.badlogic.gdx.math.Rectangle;
  11. import com.badlogic.gdx.math.MathUtils;
  12. import com.badlogic.gdx.math.Vector2;
  13. import com.badlogic.gdx.math.Vector3;
  14. import com.badlogic.gdx.physics.box2d.*;
  15. import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
  16. import com.badlogic.gdx.scenes.scene2d.Stage;
  17. import com.badlogic.gdx.scenes.scene2d.actions.*;
  18. import com.badlogic.gdx.utils.Array;
  19. import com.badlogic.gdx.utils.TimeUtils;
  20.  
  21. public class Game implements ApplicationListener {
  22.         OrthographicCamera camera;
  23.         SpriteBatch batch;
  24.        
  25.         // scene2d resources
  26.         Stage stage;
  27.  
  28.         // counters
  29.         Long lastDropTime;
  30.    
  31.         // textures
  32.         Texture paddleImage;
  33.         Texture ballImage;
  34.        
  35.         Paddle paddle;
  36.        
  37.         Array<Ball> balls;
  38.        
  39.         public void create () {
  40.             // create the scene2d Stage
  41.             stage = new Stage();
  42.             Gdx.input.setInputProcessor(stage);
  43.            
  44.            
  45.             // load the images for the paddle and the ball
  46.             paddleImage = new Texture(Gdx.files.internal("paddle.png"));
  47.             ballImage = new Texture(Gdx.files.internal("ball.png"));
  48.            
  49.             // create the camera and the SpriteBatch
  50.             camera = new OrthographicCamera();
  51.             camera.setToOrtho(false, 800, 480);
  52.             batch = new SpriteBatch();
  53.            
  54.             // create the paddle represented by a rectangle
  55.             Paddle paddle = new Paddle();
  56.             paddle.setX(800 / 2 - 200 / 2);
  57.             paddle.setY(20);
  58.             paddle.setWidth(200);
  59.             paddle.setHeight(50);
  60.            
  61.             // create the ball list and store one ball
  62.             balls = new Array<Ball>();
  63.             spawnBall();
  64.            
  65.            
  66.         }
  67.         private void spawnBall(){
  68.             Ball ball = new Ball();
  69.             ball.setX(MathUtils.random(0, 800-50));
  70.             ball.setY(480);
  71.             ball.setWidth(50);
  72.             ball.setHeight(50);
  73.             balls.add(ball);
  74.             lastDropTime = TimeUtils.nanoTime();
  75.         }
  76.  
  77.         public void render () {
  78.             // clear the screen with a dark blue color. The
  79.             // arguments to glClearColor are the red, green
  80.             // blue and alpha component in the range [0,1]
  81.             // of the color to be used to clear the screen.
  82.             Gdx.gl.glClearColor(0, 0, 0.2f, 1);
  83.             Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  84.            
  85.             // tell the camera to update its matrices.
  86.             camera.update();
  87.            
  88.             // tell the SpriteBatch to render in the
  89.             // coordinate system specified by the camera.
  90.             batch.setProjectionMatrix(camera.combined);
  91.            
  92.            
  93.             // process user input
  94.             if(Gdx.input.isTouched()) {
  95.                Vector3 touchPos = new Vector3();
  96.                touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
  97.                camera.unproject(touchPos);
  98.                paddle.setX(touchPos.x - 200 / 2);
  99.             }
  100.             if(Gdx.input.isKeyPressed(Keys.LEFT)) paddle.setX(paddle.getX() - 200 * Gdx.graphics.getDeltaTime());
  101.             if(Gdx.input.isKeyPressed(Keys.RIGHT)) paddle.setX(paddle.getX() + 200 * Gdx.graphics.getDeltaTime());
  102.            
  103.             // make sure the paddle stays within the bounds of the screen
  104.             if(paddle.getX() < 0) paddle.setX(0);
  105.             if(paddle.getX() > 800 - 200) paddle.setX(600);
  106.            
  107.             // check if we need to create a new raindrop
  108.             if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnBall();
  109.            
  110.             // move the raindrops, remove any that are beneath the bottom edge of
  111.             // the screen or that hit the bucket. In the later case we play back
  112.             // a sound effect as well.
  113.             Iterator<Ball> iter = balls.iterator();
  114.             while(iter.hasNext()) {
  115.                Ball ball = iter.next();
  116.                ball.setY(ball.getY() - 200 * Gdx.graphics.getDeltaTime());
  117.                if(ball.getY() + 48 < 0) iter.remove();
  118.                if(ball.hit(paddle.getX() + paddle.getWidth() / 2,
  119.                        paddle.getY() + paddle.getHeight() / 2, true) != null) {
  120.                   iter.remove();
  121.                }
  122.             }
  123.  
  124.            
  125.             // draw the batch via the scene
  126.             // scene2d: iterate stage and "act" actors
  127.             stage.act(Gdx.graphics.getDeltaTime());
  128.             stage.draw();
  129.            
  130.  
  131.            
  132.         }
  133.  
  134.         public void resize (int width, int height) {
  135.             stage.setViewport(width, height, true);
  136.         }
  137.  
  138.         public void pause () {
  139.         }
  140.  
  141.         public void resume () {
  142.         }
  143.  
  144.         public void dispose () {
  145.             paddleImage.dispose();
  146.             ballImage.dispose();
  147.             batch.dispose();
  148.             stage.dispose();
  149.         }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement