Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.freakout;
- import java.util.Iterator;
- import com.badlogic.gdx.*;
- import com.badlogic.gdx.Input.Keys;
- import com.badlogic.gdx.graphics.GL10;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- import com.badlogic.gdx.math.Rectangle;
- import com.badlogic.gdx.math.MathUtils;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.math.Vector3;
- import com.badlogic.gdx.physics.box2d.*;
- import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
- import com.badlogic.gdx.scenes.scene2d.Stage;
- import com.badlogic.gdx.scenes.scene2d.actions.*;
- import com.badlogic.gdx.utils.Array;
- import com.badlogic.gdx.utils.TimeUtils;
- public class Game implements ApplicationListener {
- OrthographicCamera camera;
- SpriteBatch batch;
- // scene2d resources
- Stage stage;
- // counters
- Long lastDropTime;
- // textures
- Texture paddleImage;
- Texture ballImage;
- Paddle paddle;
- Array<Ball> balls;
- public void create () {
- // create the scene2d Stage
- stage = new Stage();
- Gdx.input.setInputProcessor(stage);
- // load the images for the paddle and the ball
- paddleImage = new Texture(Gdx.files.internal("paddle.png"));
- ballImage = new Texture(Gdx.files.internal("ball.png"));
- // create the camera and the SpriteBatch
- camera = new OrthographicCamera();
- camera.setToOrtho(false, 800, 480);
- batch = new SpriteBatch();
- // create the paddle represented by a rectangle
- Paddle paddle = new Paddle();
- paddle.setX(800 / 2 - 200 / 2);
- paddle.setY(20);
- paddle.setWidth(200);
- paddle.setHeight(50);
- // create the ball list and store one ball
- balls = new Array<Ball>();
- spawnBall();
- }
- private void spawnBall(){
- Ball ball = new Ball();
- ball.setX(MathUtils.random(0, 800-50));
- ball.setY(480);
- ball.setWidth(50);
- ball.setHeight(50);
- balls.add(ball);
- lastDropTime = TimeUtils.nanoTime();
- }
- public void render () {
- // clear the screen with a dark blue color. The
- // arguments to glClearColor are the red, green
- // blue and alpha component in the range [0,1]
- // of the color to be used to clear the screen.
- Gdx.gl.glClearColor(0, 0, 0.2f, 1);
- Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
- // tell the camera to update its matrices.
- camera.update();
- // tell the SpriteBatch to render in the
- // coordinate system specified by the camera.
- batch.setProjectionMatrix(camera.combined);
- // process user input
- if(Gdx.input.isTouched()) {
- Vector3 touchPos = new Vector3();
- touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
- camera.unproject(touchPos);
- paddle.setX(touchPos.x - 200 / 2);
- }
- if(Gdx.input.isKeyPressed(Keys.LEFT)) paddle.setX(paddle.getX() - 200 * Gdx.graphics.getDeltaTime());
- if(Gdx.input.isKeyPressed(Keys.RIGHT)) paddle.setX(paddle.getX() + 200 * Gdx.graphics.getDeltaTime());
- // make sure the paddle stays within the bounds of the screen
- if(paddle.getX() < 0) paddle.setX(0);
- if(paddle.getX() > 800 - 200) paddle.setX(600);
- // check if we need to create a new raindrop
- if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnBall();
- // move the raindrops, remove any that are beneath the bottom edge of
- // the screen or that hit the bucket. In the later case we play back
- // a sound effect as well.
- Iterator<Ball> iter = balls.iterator();
- while(iter.hasNext()) {
- Ball ball = iter.next();
- ball.setY(ball.getY() - 200 * Gdx.graphics.getDeltaTime());
- if(ball.getY() + 48 < 0) iter.remove();
- if(ball.hit(paddle.getX() + paddle.getWidth() / 2,
- paddle.getY() + paddle.getHeight() / 2, true) != null) {
- iter.remove();
- }
- }
- // draw the batch via the scene
- // scene2d: iterate stage and "act" actors
- stage.act(Gdx.graphics.getDeltaTime());
- stage.draw();
- }
- public void resize (int width, int height) {
- stage.setViewport(width, height, true);
- }
- public void pause () {
- }
- public void resume () {
- }
- public void dispose () {
- paddleImage.dispose();
- ballImage.dispose();
- batch.dispose();
- stage.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement