Skip to content

Commit

Permalink
Update extended game wiki and fix link in shapes wiki (#233)
Browse files Browse the repository at this point in the history
* Fix downloading the assets for Simple game

* Fix downloading the assets for Simple game

* Fix link to Polygon code

* Update extended game wiki and java code

* Minor fixes

* Update Drop.java

* Update simple-game-extended.md

---------

Co-authored-by: damios <[email protected]>
  • Loading branch information
jaca1119 and crykn authored Oct 22, 2024
1 parent ed5c95a commit 056bf97
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 247 deletions.
17 changes: 14 additions & 3 deletions assets/downloads/tutorials/extended-game-java/Drop.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
package com.badlogic.drop;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.viewport.FitViewport;


public class Drop extends Game {

SpriteBatch batch;
BitmapFont font;
public SpriteBatch batch;
public BitmapFont font;
public FitViewport viewport;


public void create() {
batch = new SpriteBatch();
// Use LibGDX's default Arial font.
// use libGDX's default font
font = new BitmapFont();
viewport = new FitViewport(8, 5);

//font has 15pt, but we need to scale it to our viewport by ratio of viewport height to screen height
font.setUseIntegerPositions(false);
font.getData().setScale(viewport.getWorldHeight() / Gdx.graphics.getHeight());

this.setScreen(new MainMenuScreen(this));
}

Expand Down
222 changes: 118 additions & 104 deletions assets/downloads/tutorials/extended-game-java/GameScreen.java
Original file line number Diff line number Diff line change
@@ -1,148 +1,162 @@
package com.badlogic.drop;

import java.util.Iterator;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Input;
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.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.TimeUtils;

public class GameScreen implements Screen {

final Drop game;

Texture dropImage;
Texture bucketImage;
Texture backgroundTexture;
Texture bucketTexture;
Texture dropTexture;
Sound dropSound;
Music rainMusic;
OrthographicCamera camera;
Rectangle bucket;
Array<Rectangle> raindrops;
long lastDropTime;
Music music;
Sprite bucketSprite;
Vector2 touchPos;
Array<Sprite> dropSprites;
float dropTimer;
Rectangle bucketRectangle;
Rectangle dropRectangle;
int dropsGathered;

public GameScreen(final Drop gam) {
this.game = gam;
public GameScreen(final Drop game) {
this.game = game;

// 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 images for the background, bucket and droplet
backgroundTexture = new Texture("background.png");
bucketTexture = new Texture("bucket.png");
dropTexture = new Texture("drop.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);
// load the drop sound effect and background music
dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.mp3"));
music = Gdx.audio.newMusic(Gdx.files.internal("music.mp3"));
music.setLooping(true);
music.setVolume(0.5F);

// create the camera and the SpriteBatch
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
bucketSprite = new Sprite(bucketTexture);
bucketSprite.setSize(1, 1);

// 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;
touchPos = new Vector2();

// create the raindrops array and spawn the first raindrop
raindrops = new Array<Rectangle>();
spawnRaindrop();
bucketRectangle = new Rectangle();
dropRectangle = new Rectangle();

dropSprites = new Array<>();
}

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 show() {
// start the playback of the background music
// when the screen is shown
music.play();
}

@Override
public void render(float delta) {
// clear the screen with a dark blue color. The
// arguments to clear are the red, green
// blue and alpha component in the range [0,1]
// of the color to be used to clear the screen.
ScreenUtils.clear(0, 0, 0.2f, 1);

// tell the camera to update its matrices.
camera.update();
input();
logic();
draw();
}

// tell the SpriteBatch to render in the
// coordinate system specified by the camera.
game.batch.setProjectionMatrix(camera.combined);
private void input() {
float speed = 4f;
float delta = Gdx.graphics.getDeltaTime();

// 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);
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
bucketSprite.translateX(speed * delta);
}
else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
bucketSprite.translateX(-speed * delta);
}
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;
touchPos.set(Gdx.input.getX(), Gdx.input.getY());
game.viewport.unproject(touchPos);
bucketSprite.setCenterX(touchPos.x);
}
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)) {
}

private void logic() {
float worldWidth = game.viewport.getWorldWidth();
float worldHeight = game.viewport.getWorldHeight();
float bucketWidth = bucketSprite.getWidth();
float bucketHeight = bucketSprite.getHeight();
float delta = Gdx.graphics.getDeltaTime();

bucketSprite.setX(MathUtils.clamp(bucketSprite.getX(), 0, worldWidth - bucketWidth));
bucketRectangle.set(bucketSprite.getX(), bucketSprite.getY(), bucketWidth, bucketHeight);

for (int i = dropSprites.size - 1; i >= 0; i--) {
Sprite dropSprite = dropSprites.get(i);
float dropWidth = dropSprite.getWidth();
float dropHeight = dropSprite.getHeight();

dropSprite.translateY(-2f * delta);
dropRectangle.set(dropSprite.getX(), dropSprite.getY(), dropWidth, dropHeight);

if (dropSprite.getY() < -dropHeight) dropSprites.removeIndex(i);
else if (bucketRectangle.overlaps(dropRectangle)) {
dropsGathered++;
dropSprites.removeIndex(i);
dropSound.play();
iter.remove();
}
}

dropTimer += delta;
if (dropTimer > 1f) {
dropTimer = 0;
createDroplet();
}
}

@Override
public void resize(int width, int height) {
private void draw() {
ScreenUtils.clear(Color.BLACK);
game.viewport.apply();
game.batch.setProjectionMatrix(game.viewport.getCamera().combined);
game.batch.begin();

float worldWidth = game.viewport.getWorldWidth();
float worldHeight = game.viewport.getWorldHeight();

game.batch.draw(backgroundTexture, 0, 0, worldWidth, worldHeight);
bucketSprite.draw(game.batch);

game.font.draw(game.batch, "Drops collected: " + dropsGathered, 0, worldHeight);

for (Sprite dropSprite : dropSprites) {
dropSprite.draw(game.batch);
}

game.batch.end();
}

private void createDroplet() {
float dropWidth = 1;
float dropHeight = 1;
float worldWidth = game.viewport.getWorldWidth();
float worldHeight = game.viewport.getWorldHeight();

Sprite dropSprite = new Sprite(dropTexture);
dropSprite.setSize(dropWidth, dropHeight);
dropSprite.setX(MathUtils.random(0F, worldWidth - dropWidth));
dropSprite.setY(worldHeight);
dropSprites.add(dropSprite);
}

@Override
public void show() {
// start the playback of the background music
// when the screen is shown
rainMusic.play();
public void resize(int width, int height) {
game.viewport.update(width, height, true);
}

@Override
Expand All @@ -159,10 +173,10 @@ public void resume() {

@Override
public void dispose() {
dropImage.dispose();
bucketImage.dispose();
backgroundTexture.dispose();
dropSound.dispose();
rainMusic.dispose();
music.dispose();
dropTexture.dispose();
bucketTexture.dispose();
}

}
24 changes: 11 additions & 13 deletions assets/downloads/tutorials/extended-game-java/MainMenuScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,28 @@

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.utils.ScreenUtils;

public class MainMenuScreen implements Screen {

final Drop game;
OrthographicCamera camera;
final Drop game;

public MainMenuScreen(final Drop gam) {
game = gam;

camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
public MainMenuScreen(final Drop game) {
this.game = game;
}

@Override
public void render(float delta) {
ScreenUtils.clear(0, 0, 0.2f, 1);
ScreenUtils.clear(Color.BLACK);

camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.viewport.apply();
game.batch.setProjectionMatrix(game.viewport.getCamera().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);
//draw text. Remember that x and y are in meters
game.font.draw(game.batch, "Welcome to Drop!!! ", 1, 1.5f);
game.font.draw(game.batch, "Tap anywhere to begin!", 1, 1);
game.batch.end();

if (Gdx.input.isTouched()) {
Expand All @@ -37,6 +34,7 @@ public void render(float delta) {

@Override
public void resize(int width, int height) {
game.viewport.update(width, height, true);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion wiki/math-utils/circles-planes-rays-etc.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ A plane is an infinite two-dimensional surface in three-dimensional space descri

----

# [Polygons](https://javadoc.io/doc/com.badlogicgames.gdx/gdx/latest/com/badlogic/gdx/math/Polygon.html)[(code)] (https://github.com/libgdx/libgdx/tree/master/gdx/src/com/badlogic/gdx/math/Polygon.java)
# [Polygons](https://javadoc.io/doc/com.badlogicgames.gdx/gdx/latest/com/badlogic/gdx/math/Polygon.html) [(code)](https://github.com/libgdx/libgdx/tree/master/gdx/src/com/badlogic/gdx/math/Polygon.java)

A simple class defining a two-dimensional polygon from a list of points. It can be easily translated, rotated, and scaled. It also provides the ability to test a point for containment within the polygon. Polygon to polygon intersection can be tested by using the [Intersector](https://javadoc.io/doc/com.badlogicgames.gdx/gdx/latest/com/badlogic/gdx/math/Intersector.html) [(code)](https://github.com/libgdx/libgdx/tree/master/gdx/src/com/badlogic/gdx/math/Intersector.java) class, assuming the polygons are convex.

Expand Down
10 changes: 5 additions & 5 deletions wiki/start/a-simple-game.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ private static Lwjgl3ApplicationConfiguration getDefaultConfiguration() {
## Loading Assets
2D games made in libGDX need assets: images, audio, and other resources that comprise the project. In this case, we'll need a bucket, a raindrop, a background, a water drop sound effect, and music. If you're pretty resourceful, you can make these on your own. For simplicity's sake, you can download these examples which are optimized for this tutorial.

<a href="/assets/downloads/tutorials/simple-game/bucket.png?nomagnify" download="bucket">bucket.png</a><br>
<a href="/assets/downloads/tutorials/simple-game/drop.png?nomagnify" download="drop">drop.png</a><br>
<a href="/assets/downloads/tutorials/simple-game/background.png?nomagnify" download="background">background.png</a><br>
<a href="/assets/downloads/tutorials/simple-game/drop.mp3?nomagnify" download="drop">drop.mp3</a><br>
<a href="/assets/downloads/tutorials/simple-game/music.mp3?nomagnify" download="music">music.mp3</a>
<a href="/assets/downloads/tutorials/simple-game/bucket.png?nomagnify" download="bucket.png">bucket.png</a><br>
<a href="/assets/downloads/tutorials/simple-game/drop.png?nomagnify" download="drop.png">drop.png</a><br>
<a href="/assets/downloads/tutorials/simple-game/background.png?nomagnify" download="background.png">background.png</a><br>
<a href="/assets/downloads/tutorials/simple-game/drop.mp3?nomagnify" download="drop.mp3">drop.mp3</a><br>
<a href="/assets/downloads/tutorials/simple-game/music.mp3?nomagnify" download="music.mp3">music.mp3</a>

Just having these saved on your computer is not enough. These files need to be placed in the assets folder of your project. Look inside the project folder:

Expand Down
Loading

0 comments on commit 056bf97

Please sign in to comment.