Back to project page SimpleGDX.
The source code is released under:
Apache License
If you think the Android project SimpleGDX listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.proglive.lesson1.alyoshin.simplegdx; /*from w w w . j av a2 s . co m*/ import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; 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.MathUtils; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.TimeUtils; import java.util.Iterator; public class MyGame extends Game { private Texture dropTexture; private Texture bucketTexture; private OrthographicCamera camera; private SpriteBatch batch; private Rectangle bucket; private Vector3 touchedPos; private Array<Rectangle> drops; private long lasDropTime; private int width = 800; //default value private int height = 600; //default value @Override public void resize(int width, int height) { // super.resize(width, height); camera.setToOrtho(false, width, height); this.width = width; this.height = height; } @Override public void dispose() { // super.dispose(); bucketTexture.dispose(); dropTexture.dispose(); batch.dispose(); } @Override public void render() { super.render(); Gdx.gl.glClearColor(0, 0, 0.2f, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); camera.update(); batch.setProjectionMatrix(camera.combined); batch.begin(); batch.draw(bucketTexture, bucket.x, bucket.y); for (Rectangle drop : drops) { batch.draw(dropTexture, drop.x, drop.y); } batch.end(); if (Gdx.input.isTouched()) { touchedPos.set(Gdx.input.getX(), Gdx.input.getY(), 0); camera.unproject(touchedPos); bucket.x = touchedPos.x - 64 / 2; } else { if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) { bucket.x -= 200 * Gdx.graphics.getRawDeltaTime(); } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) { bucket.x += 200 * Gdx.graphics.getRawDeltaTime(); } } if (bucket.x < 0) { bucket.x = 0; } if (bucket.x > width - 64) { bucket.x = width - 64; } if (TimeUtils.nanoTime() - lasDropTime > 1000000000) { spawnRainDrop(); } Iterator<Rectangle> iterator = drops.iterator(); while (iterator.hasNext()) { Rectangle drop = iterator.next(); drop.y -= 200 * Gdx.graphics.getRawDeltaTime(); if (drop.y + 64 < 0) { iterator.remove(); } if (drop.overlaps(bucket)) { iterator.remove(); } } } @Override public void create() { dropTexture = new Texture(Gdx.files.internal("droplet.png")); bucketTexture = new Texture(Gdx.files.internal("bucket.png")); camera = new OrthographicCamera(); batch = new SpriteBatch(); bucket = new Rectangle(width / 2 - 64 / 2, 20, 64, 64); touchedPos = new Vector3(); drops = new Array<Rectangle>(); spawnRainDrop(); } private void spawnRainDrop() { Rectangle drop = new Rectangle(MathUtils.random(0, width - 64), height - 50, 64, 64); drops.add(drop); lasDropTime = TimeUtils.nanoTime(); } }