mirror of
https://github.com/tiennm99/libGDX-tutorial.git
synced 2026-06-08 16:16:03 +00:00
v2
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"platform": "windows64",
|
||||
"jdk": "C:\\Program Files\\Eclipse Foundation\\jdk-11.0.12.7-hotspot",
|
||||
"executable": "Drop",
|
||||
"classpath": [
|
||||
"desktop\\build\\libs\\desktop-1.0.jar"
|
||||
],
|
||||
"mainclass": "com.badlogic.drop.Drop",
|
||||
"output": "out-windows"
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
package com.badlogic.drop;
|
||||
|
||||
import com.badlogic.gdx.ApplicationAdapter;
|
||||
import com.badlogic.gdx.Game;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input.Keys;
|
||||
import com.badlogic.gdx.audio.Music;
|
||||
import com.badlogic.gdx.audio.Sound;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.graphics.Texture;
|
||||
import com.badlogic.gdx.graphics.g2d.BitmapFont;
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
|
||||
import com.badlogic.gdx.math.MathUtils;
|
||||
import com.badlogic.gdx.math.Rectangle;
|
||||
@@ -16,103 +18,22 @@ import com.badlogic.gdx.utils.ScreenUtils;
|
||||
import com.badlogic.gdx.utils.TimeUtils;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Drop extends ApplicationAdapter {
|
||||
private Texture dropImage;
|
||||
private Texture bucketImage;
|
||||
private Sound dropSound;
|
||||
private Music rainMusic;
|
||||
public class Drop extends Game {
|
||||
public SpriteBatch batch;
|
||||
public BitmapFont font;
|
||||
|
||||
private OrthographicCamera camera;
|
||||
private SpriteBatch batch;
|
||||
|
||||
private Rectangle bucket;
|
||||
|
||||
private Array<Rectangle> raindrops;
|
||||
|
||||
private long lastDropTime;
|
||||
|
||||
@Override
|
||||
public void create() {
|
||||
dropImage = new Texture(Gdx.files.internal("droplet.png"));
|
||||
bucketImage = new Texture(Gdx.files.internal("bucket.png"));
|
||||
|
||||
dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
|
||||
rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
|
||||
|
||||
rainMusic.setLooping(true);
|
||||
rainMusic.play();
|
||||
|
||||
camera = new OrthographicCamera();
|
||||
camera.setToOrtho(false, 800, 480);
|
||||
|
||||
batch = new SpriteBatch();
|
||||
|
||||
bucket = new Rectangle();
|
||||
bucket.x = 800 / 2 - 64 / 2;
|
||||
bucket.y = 20;
|
||||
bucket.width = 64;
|
||||
bucket.height = 64;
|
||||
|
||||
raindrops = new Array<Rectangle>();
|
||||
spawnRainDrop();
|
||||
font = new BitmapFont();
|
||||
this.setScreen(new MainMenuScreen(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
ScreenUtils.clear(0, 0, 0.2f, 1);
|
||||
|
||||
camera.update();
|
||||
|
||||
batch.setProjectionMatrix(camera.combined);
|
||||
batch.begin();
|
||||
batch.draw(bucketImage, bucket.x, bucket.y);
|
||||
for (Rectangle raindrop:raindrops) {
|
||||
batch.draw(dropImage, raindrop.x, raindrop.y);
|
||||
}
|
||||
batch.end();
|
||||
|
||||
if (Gdx.input.isTouched()) {
|
||||
Vector3 touchPos = new Vector3();
|
||||
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
|
||||
camera.unproject(touchPos);
|
||||
bucket.x = touchPos.x - 64 / 2;
|
||||
}
|
||||
|
||||
if (Gdx.input.isKeyPressed(Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
|
||||
if (Gdx.input.isKeyPressed(Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime();
|
||||
|
||||
if (bucket.x < 0) bucket.x = 0;
|
||||
if (bucket.x > 800 - 64) bucket.x = 800 - 64;
|
||||
|
||||
if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRainDrop();
|
||||
|
||||
for (Iterator<Rectangle> iter = raindrops.iterator(); iter.hasNext(); ) {
|
||||
Rectangle raindrop = iter.next();
|
||||
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
|
||||
if (raindrop.y + 64 < 0) iter.remove();
|
||||
if (raindrop.overlaps(bucket)) {
|
||||
dropSound.play();
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
super.render();
|
||||
}
|
||||
|
||||
private void spawnRainDrop() {
|
||||
Rectangle raindrop = new Rectangle();
|
||||
raindrop.x = MathUtils.random(0, 800 - 64);
|
||||
raindrop.y = 480;
|
||||
raindrop.width = 64;
|
||||
raindrop.height = 64;
|
||||
raindrops.add(raindrop);
|
||||
lastDropTime = TimeUtils.nanoTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
dropImage.dispose();
|
||||
bucketImage.dispose();
|
||||
dropSound.dispose();
|
||||
rainMusic.dispose();
|
||||
batch.dispose();
|
||||
font.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
package com.badlogic.drop;
|
||||
|
||||
import com.badlogic.gdx.Game;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input.Keys;
|
||||
import com.badlogic.gdx.Screen;
|
||||
import com.badlogic.gdx.audio.Music;
|
||||
import com.badlogic.gdx.audio.Sound;
|
||||
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.ScreenUtils;
|
||||
import com.badlogic.gdx.utils.TimeUtils;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class GameScreen implements Screen {
|
||||
final Drop game;
|
||||
|
||||
private Texture dropImage;
|
||||
private Texture bucketImage;
|
||||
private Sound dropSound;
|
||||
private Music rainMusic;
|
||||
|
||||
private OrthographicCamera camera;
|
||||
|
||||
private Rectangle bucket;
|
||||
|
||||
private Array<Rectangle> raindrops;
|
||||
|
||||
private long lastDropTime;
|
||||
int dropGathered;
|
||||
|
||||
public GameScreen(final Drop game) {
|
||||
this.game = game;
|
||||
|
||||
dropImage = new Texture(Gdx.files.internal("droplet.png"));
|
||||
bucketImage = new Texture(Gdx.files.internal("bucket.png"));
|
||||
|
||||
dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
|
||||
rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
|
||||
rainMusic.setLooping(true);
|
||||
|
||||
camera = new OrthographicCamera();
|
||||
camera.setToOrtho(false, 800, 480);
|
||||
|
||||
bucket = new Rectangle();
|
||||
bucket.x = 800 / 2 - 64 / 2;
|
||||
bucket.y = 20;
|
||||
bucket.width = 64;
|
||||
bucket.height = 64;
|
||||
|
||||
raindrops = new Array<Rectangle>();
|
||||
spawnRainDrop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(float delta) {
|
||||
ScreenUtils.clear(0, 0, 0.2f, 1);
|
||||
|
||||
camera.update();
|
||||
|
||||
game.batch.setProjectionMatrix(camera.combined);
|
||||
game.batch.begin();
|
||||
game.font.draw(game.batch, "Drops Collected: " + dropGathered, 0, 480 );
|
||||
game.batch.draw(bucketImage, bucket.x, bucket.y, bucket.width, bucket.height);
|
||||
for (Rectangle raindrop:raindrops) {
|
||||
game.batch.draw(dropImage, raindrop.x, raindrop.y);
|
||||
}
|
||||
game.batch.end();
|
||||
|
||||
if (Gdx.input.isTouched()) {
|
||||
Vector3 touchPos = new Vector3();
|
||||
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
|
||||
camera.unproject(touchPos);
|
||||
bucket.x = touchPos.x - 64 / 2;
|
||||
}
|
||||
|
||||
if (Gdx.input.isKeyPressed(Keys.LEFT))
|
||||
bucket.x -= 200 * Gdx.graphics.getDeltaTime();
|
||||
if (Gdx.input.isKeyPressed(Keys.RIGHT))
|
||||
bucket.x += 200 * Gdx.graphics.getDeltaTime();
|
||||
|
||||
if (bucket.x < 0) bucket.x = 0;
|
||||
if (bucket.x > 800 - 64) bucket.x = 800 - 64;
|
||||
|
||||
if (TimeUtils.nanoTime() - lastDropTime > 1000000000)
|
||||
spawnRainDrop();
|
||||
|
||||
for (Iterator<Rectangle> iter = raindrops.iterator(); iter.hasNext(); ) {
|
||||
Rectangle raindrop = iter.next();
|
||||
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
|
||||
if (raindrop.y + 64 < 0) iter.remove();
|
||||
if (raindrop.overlaps(bucket)) {
|
||||
dropGathered++;
|
||||
dropSound.play();
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void spawnRainDrop() {
|
||||
Rectangle raindrop = new Rectangle();
|
||||
raindrop.x = MathUtils.random(0, 800 - 64);
|
||||
raindrop.y = 480;
|
||||
raindrop.width = 64;
|
||||
raindrop.height = 64;
|
||||
raindrops.add(raindrop);
|
||||
lastDropTime = TimeUtils.nanoTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
dropImage.dispose();
|
||||
bucketImage.dispose();
|
||||
dropSound.dispose();
|
||||
rainMusic.dispose();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
rainMusic.play();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(int width, int height) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pause() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resume() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hide() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.badlogic.drop;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Screen;
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera;
|
||||
import com.badlogic.gdx.utils.ScreenUtils;
|
||||
|
||||
public class MainMenuScreen implements Screen {
|
||||
final Drop game;
|
||||
OrthographicCamera camera;
|
||||
|
||||
public MainMenuScreen(final Drop game) {
|
||||
this.game = game;
|
||||
camera = new OrthographicCamera();
|
||||
camera.setToOrtho(false, 800, 480);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {}
|
||||
|
||||
@Override
|
||||
public void render(float delta) {
|
||||
ScreenUtils.clear(0, 0, 0.2f, 1);
|
||||
|
||||
camera.update();
|
||||
game.batch.setProjectionMatrix(camera.combined);
|
||||
|
||||
game.batch.begin();
|
||||
game.font.draw(game.batch, "Welcome to Drop!!!", 100, 150);
|
||||
game.font.draw(game.batch, "Tap anywhere to begin!", 100, 100);
|
||||
game.batch.end();
|
||||
|
||||
if (Gdx.input.isTouched()) {
|
||||
game.setScreen(new GameScreen(game));
|
||||
dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(int width, int height) {}
|
||||
|
||||
@Override
|
||||
public void pause() {}
|
||||
|
||||
@Override
|
||||
public void resume() {}
|
||||
|
||||
@Override
|
||||
public void hide() {}
|
||||
|
||||
@Override
|
||||
public void dispose() {}
|
||||
}
|
||||
@@ -1,14 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit trunk//EN"
|
||||
"http://www.gwtproject.org/doctype/2.8.0/gwt-module.dtd">
|
||||
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit trunk//EN" "http://www.gwtproject.org/doctype/2.8.0/gwt-module.dtd">
|
||||
<module rename-to="html">
|
||||
<collapse-property name="user.agent" values="*"/>
|
||||
<entry-point class='com.badlogic.drop.client.HtmlLauncher'/>
|
||||
|
||||
<inherits name='Drop'/>
|
||||
<inherits name='com.badlogic.gdx.backends.gdx_backends_gwt'/>
|
||||
<inherits name='com.badlogic.gdx.physics.box2d.box2d-gwt'/>
|
||||
<set-configuration-property name="gdx.assetpath" value="../android/assets"/>
|
||||
<set-configuration-property name='xsiframe.failIfScriptTag' value='FALSE'/>
|
||||
<set-property name="user.agent" value="gecko1_8, safari"/>
|
||||
<collapse-property name="user.agent" values="*"/>
|
||||
</module>
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user