Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is the gist for the "Extended Simple App" LibGDX tutorial found at: https://github.com/libgdx/libgdx/wiki/Extending-the-simple-game
package com.badlogic.drop;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Drop extends Game {
SpriteBatch batch;
BitmapFont font;
public void create() {
batch = new SpriteBatch();
// Use LibGDX's default Arial font.
font = new BitmapFont();
this.setScreen(new MainMenuScreen(this));
}
public void render() {
super.render(); // important!
}
public void dispose() {
batch.dispose();
font.dispose();
}
}
package com.badlogic.drop;
import java.util.Iterator;
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.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
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;
public class GameScreen implements Screen {
final Drop game;
Texture dropImage;
Texture bucketImage;
Sound dropSound;
Music rainMusic;
OrthographicCamera camera;
Rectangle bucket;
Array<Rectangle> raindrops;
long lastDropTime;
int dropsGathered;
public GameScreen(final Drop gam) {
this.game = gam;
// load the images for the droplet and the bucket, 64x64 pixels each
dropImage = new Texture(Gdx.files.internal("droplet.png"));
bucketImage = new Texture(Gdx.files.internal("bucket.png"));
// load the drop sound effect and the rain background "music"
dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
rainMusic.setLooping(true);
// create the camera and the SpriteBatch
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
// create a Rectangle to logically represent the bucket
bucket = new Rectangle();
bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
bucket.y = 20; // bottom left corner of the bucket is 20 pixels above
// the bottom screen edge
bucket.width = 64;
bucket.height = 64;
// create the raindrops array and spawn the first raindrop
raindrops = new Array<Rectangle>();
spawnRaindrop();
}
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 render(float delta) {
// 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.
game.batch.setProjectionMatrix(camera.combined);
// begin a new batch and draw the bucket and
// all drops
game.batch.begin();
game.font.draw(game.batch, "Drops Collected: " + dropsGathered, 0, 480);
game.batch.draw(bucketImage, bucket.x, bucket.y);
for (Rectangle raindrop : raindrops) {
game.batch.draw(dropImage, raindrop.x, raindrop.y);
}
game.batch.end();
// process user input
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();
// make sure the bucket stays within the screen bounds
if (bucket.x < 0)
bucket.x = 0;
if (bucket.x > 800 - 64)
bucket.x = 800 - 64;
// check if we need to create a new raindrop
if (TimeUtils.nanoTime() - lastDropTime > 1000000000)
spawnRaindrop();
// 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<Rectangle> iter = raindrops.iterator();
while (iter.hasNext()) {
Rectangle raindrop = iter.next();
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
if (raindrop.y + 64 < 0)
iter.remove();
if (raindrop.overlaps(bucket)) {
dropsGathered++;
dropSound.play();
iter.remove();
}
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void show() {
// start the playback of the background music
// when the screen is shown
rainMusic.play();
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
dropImage.dispose();
bucketImage.dispose();
dropSound.dispose();
rainMusic.dispose();
}
}
package com.badlogic.drop;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
public class MainMenuScreen implements Screen {
final Drop game;
OrthographicCamera camera;
public MainMenuScreen(final Drop gam) {
game = gam;
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
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 show() {
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
}
@AgostinoSturaro

This comment has been minimized.

Show comment
Hide comment
@AgostinoSturaro

AgostinoSturaro Mar 15, 2014

MainMenuScreenScreen.java should be MainMenuScreen.java and, for some reason, I get some visibility problems for

SpriteBatch batch;
BitmapFont font;

Being in the same package, they should be visible to the other 2 classes, but Eclipse complains they are not. Lots of other errors when starting the app on a real device + big crash.

AgostinoSturaro commented Mar 15, 2014

MainMenuScreenScreen.java should be MainMenuScreen.java and, for some reason, I get some visibility problems for

SpriteBatch batch;
BitmapFont font;

Being in the same package, they should be visible to the other 2 classes, but Eclipse complains they are not. Lots of other errors when starting the app on a real device + big crash.

@sinistersnare

This comment has been minimized.

Show comment
Hide comment
@sinistersnare

sinistersnare Mar 26, 2014

@AgostinoSturaro sorry about that, I've fixed the errors not relating to visibilty, as I can not reproduce that, and changed the file name.

Please feel free to email me sinistersnare@gmail dot com or go on IRC, they can help there.

Owner

sinistersnare commented Mar 26, 2014

@AgostinoSturaro sorry about that, I've fixed the errors not relating to visibilty, as I can not reproduce that, and changed the file name.

Please feel free to email me sinistersnare@gmail dot com or go on IRC, they can help there.

@kevinlondon

This comment has been minimized.

Show comment
Hide comment
@kevinlondon

kevinlondon Apr 21, 2014

Thanks for the examples! Definitely helped me understand how to get started with the framework. Here's my repo with mostly the same code: https://github.com/kevinlondon/java-libgdx-drop-example

Would you mind if I submitted a pull request to use named constants instead of some of the values such as 800, 64, etc?

kevinlondon commented Apr 21, 2014

Thanks for the examples! Definitely helped me understand how to get started with the framework. Here's my repo with mostly the same code: https://github.com/kevinlondon/java-libgdx-drop-example

Would you mind if I submitted a pull request to use named constants instead of some of the values such as 800, 64, etc?

@supersam654

This comment has been minimized.

Show comment
Hide comment
@supersam654

supersam654 Jul 13, 2014

@sinistersnare I forked your gist and made a few minor changes to visibility and indentation. Feel free to merge it back into this one.

https://gist.github.com/supersam654/453a5cf5e19973b9fb26

supersam654 commented Jul 13, 2014

@sinistersnare I forked your gist and made a few minor changes to visibility and indentation. Feel free to merge it back into this one.

https://gist.github.com/supersam654/453a5cf5e19973b9fb26

@ysnkhll

This comment has been minimized.

Show comment
Hide comment
@ysnkhll

ysnkhll Sep 12, 2014

@sinistersnare Correct me if I'm wrong, but I believe you may have forgot to append super.dispose(); in the dispose method of class Drop. Doing so resolved issues I was having with your example.

ysnkhll commented Sep 12, 2014

@sinistersnare Correct me if I'm wrong, but I believe you may have forgot to append super.dispose(); in the dispose method of class Drop. Doing so resolved issues I was having with your example.

@tobloef

This comment has been minimized.

Show comment
Hide comment
@tobloef

tobloef Jul 10, 2015

@sinistersnare I forked the gist and fixed some minor bugs and readability issues. Please feel free to merge my fork into this one.

https://gist.github.com/TobLoef/42f1fd370426d9ebe511/

tobloef commented Jul 10, 2015

@sinistersnare I forked the gist and fixed some minor bugs and readability issues. Please feel free to merge my fork into this one.

https://gist.github.com/TobLoef/42f1fd370426d9ebe511/

@sachingorade

This comment has been minimized.

Show comment
Hide comment
@sachingorade

sachingorade Sep 12, 2015

GameScreen.dispose never gets called, so resources used by it will never be disposed??

sachingorade commented Sep 12, 2015

GameScreen.dispose never gets called, so resources used by it will never be disposed??

@denisk20

This comment has been minimized.

Show comment
Hide comment
@denisk20

denisk20 Oct 19, 2015

@sinistersnare GameScreen.dispose() wasn't called at all; calling super.dispose() in Drop.dispose() didn't help, I was getting these messages when closing desktop window:

AL lib: (WW) FreeDevice: (0x7f9185959a00) Deleting 4 Buffer(s)

Adding screen.dispose(); to Game.dispose() fixed this issue.

denisk20 commented Oct 19, 2015

@sinistersnare GameScreen.dispose() wasn't called at all; calling super.dispose() in Drop.dispose() didn't help, I was getting these messages when closing desktop window:

AL lib: (WW) FreeDevice: (0x7f9185959a00) Deleting 4 Buffer(s)

Adding screen.dispose(); to Game.dispose() fixed this issue.

@saurabhshirodkar

This comment has been minimized.

Show comment
Hide comment
@saurabhshirodkar

saurabhshirodkar Oct 30, 2015

Hey I am a little new to java, I just wanted to know where the setScreen function used in Drop class like:
this.setScreen(new MainMenuScreen(this));

I want to know where it is defined...because i thought 'this' keyword calls functions of the self class itself...but there seems to be no such function.

saurabhshirodkar commented Oct 30, 2015

Hey I am a little new to java, I just wanted to know where the setScreen function used in Drop class like:
this.setScreen(new MainMenuScreen(this));

I want to know where it is defined...because i thought 'this' keyword calls functions of the self class itself...but there seems to be no such function.

@rohansuri

This comment has been minimized.

Show comment
Hide comment
@rohansuri

rohansuri Jan 2, 2016

The above code needs to make a call to GameScreen's dispose method. Add

this.getScreen().dispose();
inside Drop class's dispose method. Hence the last screen's dispose method will be called through Game class' dispose method.

rohansuri commented Jan 2, 2016

The above code needs to make a call to GameScreen's dispose method. Add

this.getScreen().dispose();
inside Drop class's dispose method. Hence the last screen's dispose method will be called through Game class' dispose method.

@sgSumitGupta

This comment has been minimized.

Show comment
Hide comment
@sgSumitGupta

sgSumitGupta Jun 24, 2016

Hi All,

How can i add stop and start button in this game screen?
Please help me.

Thanks,
Sumit

sgSumitGupta commented Jun 24, 2016

Hi All,

How can i add stop and start button in this game screen?
Please help me.

Thanks,
Sumit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment