Skip to content

Commit 056bf97

Browse files
jaca1119crykn
andauthored
Update extended game wiki and fix link in shapes wiki (#233)
* 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]>
1 parent ed5c95a commit 056bf97

File tree

6 files changed

+301
-247
lines changed

6 files changed

+301
-247
lines changed

assets/downloads/tutorials/extended-game-java/Drop.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
package com.badlogic.drop;
22

33
import com.badlogic.gdx.Game;
4+
import com.badlogic.gdx.Gdx;
45
import com.badlogic.gdx.graphics.g2d.BitmapFont;
56
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
7+
import com.badlogic.gdx.utils.viewport.FitViewport;
8+
69

710
public class Drop extends Game {
811

9-
SpriteBatch batch;
10-
BitmapFont font;
12+
public SpriteBatch batch;
13+
public BitmapFont font;
14+
public FitViewport viewport;
15+
1116

1217
public void create() {
1318
batch = new SpriteBatch();
14-
// Use LibGDX's default Arial font.
19+
// use libGDX's default font
1520
font = new BitmapFont();
21+
viewport = new FitViewport(8, 5);
22+
23+
//font has 15pt, but we need to scale it to our viewport by ratio of viewport height to screen height
24+
font.setUseIntegerPositions(false);
25+
font.getData().setScale(viewport.getWorldHeight() / Gdx.graphics.getHeight());
26+
1627
this.setScreen(new MainMenuScreen(this));
1728
}
1829

Lines changed: 118 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,162 @@
11
package com.badlogic.drop;
22

3-
import java.util.Iterator;
4-
53
import com.badlogic.gdx.Gdx;
6-
import com.badlogic.gdx.Input.Keys;
4+
import com.badlogic.gdx.Input;
75
import com.badlogic.gdx.Screen;
86
import com.badlogic.gdx.audio.Music;
97
import com.badlogic.gdx.audio.Sound;
10-
import com.badlogic.gdx.graphics.OrthographicCamera;
8+
import com.badlogic.gdx.graphics.Color;
119
import com.badlogic.gdx.graphics.Texture;
10+
import com.badlogic.gdx.graphics.g2d.Sprite;
1211
import com.badlogic.gdx.math.MathUtils;
1312
import com.badlogic.gdx.math.Rectangle;
14-
import com.badlogic.gdx.math.Vector3;
13+
import com.badlogic.gdx.math.Vector2;
1514
import com.badlogic.gdx.utils.Array;
1615
import com.badlogic.gdx.utils.ScreenUtils;
17-
import com.badlogic.gdx.utils.TimeUtils;
1816

1917
public class GameScreen implements Screen {
20-
2118
final Drop game;
2219

23-
Texture dropImage;
24-
Texture bucketImage;
20+
Texture backgroundTexture;
21+
Texture bucketTexture;
22+
Texture dropTexture;
2523
Sound dropSound;
26-
Music rainMusic;
27-
OrthographicCamera camera;
28-
Rectangle bucket;
29-
Array<Rectangle> raindrops;
30-
long lastDropTime;
24+
Music music;
25+
Sprite bucketSprite;
26+
Vector2 touchPos;
27+
Array<Sprite> dropSprites;
28+
float dropTimer;
29+
Rectangle bucketRectangle;
30+
Rectangle dropRectangle;
3131
int dropsGathered;
3232

33-
public GameScreen(final Drop gam) {
34-
this.game = gam;
33+
public GameScreen(final Drop game) {
34+
this.game = game;
3535

36-
// load the images for the droplet and the bucket, 64x64 pixels each
37-
dropImage = new Texture(Gdx.files.internal("droplet.png"));
38-
bucketImage = new Texture(Gdx.files.internal("bucket.png"));
36+
// load the images for the background, bucket and droplet
37+
backgroundTexture = new Texture("background.png");
38+
bucketTexture = new Texture("bucket.png");
39+
dropTexture = new Texture("drop.png");
3940

40-
// load the drop sound effect and the rain background "music"
41-
dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
42-
rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
43-
rainMusic.setLooping(true);
41+
// load the drop sound effect and background music
42+
dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.mp3"));
43+
music = Gdx.audio.newMusic(Gdx.files.internal("music.mp3"));
44+
music.setLooping(true);
45+
music.setVolume(0.5F);
4446

45-
// create the camera and the SpriteBatch
46-
camera = new OrthographicCamera();
47-
camera.setToOrtho(false, 800, 480);
47+
bucketSprite = new Sprite(bucketTexture);
48+
bucketSprite.setSize(1, 1);
4849

49-
// create a Rectangle to logically represent the bucket
50-
bucket = new Rectangle();
51-
bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
52-
bucket.y = 20; // bottom left corner of the bucket is 20 pixels above
53-
// the bottom screen edge
54-
bucket.width = 64;
55-
bucket.height = 64;
50+
touchPos = new Vector2();
5651

57-
// create the raindrops array and spawn the first raindrop
58-
raindrops = new Array<Rectangle>();
59-
spawnRaindrop();
52+
bucketRectangle = new Rectangle();
53+
dropRectangle = new Rectangle();
6054

55+
dropSprites = new Array<>();
6156
}
6257

63-
private void spawnRaindrop() {
64-
Rectangle raindrop = new Rectangle();
65-
raindrop.x = MathUtils.random(0, 800 - 64);
66-
raindrop.y = 480;
67-
raindrop.width = 64;
68-
raindrop.height = 64;
69-
raindrops.add(raindrop);
70-
lastDropTime = TimeUtils.nanoTime();
58+
@Override
59+
public void show() {
60+
// start the playback of the background music
61+
// when the screen is shown
62+
music.play();
7163
}
7264

7365
@Override
7466
public void render(float delta) {
75-
// clear the screen with a dark blue color. The
76-
// arguments to clear are the red, green
77-
// blue and alpha component in the range [0,1]
78-
// of the color to be used to clear the screen.
79-
ScreenUtils.clear(0, 0, 0.2f, 1);
80-
81-
// tell the camera to update its matrices.
82-
camera.update();
67+
input();
68+
logic();
69+
draw();
70+
}
8371

84-
// tell the SpriteBatch to render in the
85-
// coordinate system specified by the camera.
86-
game.batch.setProjectionMatrix(camera.combined);
72+
private void input() {
73+
float speed = 4f;
74+
float delta = Gdx.graphics.getDeltaTime();
8775

88-
// begin a new batch and draw the bucket and
89-
// all drops
90-
game.batch.begin();
91-
game.font.draw(game.batch, "Drops Collected: " + dropsGathered, 0, 480);
92-
game.batch.draw(bucketImage, bucket.x, bucket.y);
93-
for (Rectangle raindrop : raindrops) {
94-
game.batch.draw(dropImage, raindrop.x, raindrop.y);
76+
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
77+
bucketSprite.translateX(speed * delta);
78+
}
79+
else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
80+
bucketSprite.translateX(-speed * delta);
9581
}
96-
game.batch.end();
9782

98-
// process user input
9983
if (Gdx.input.isTouched()) {
100-
Vector3 touchPos = new Vector3();
101-
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
102-
camera.unproject(touchPos);
103-
bucket.x = touchPos.x - 64 / 2;
84+
touchPos.set(Gdx.input.getX(), Gdx.input.getY());
85+
game.viewport.unproject(touchPos);
86+
bucketSprite.setCenterX(touchPos.x);
10487
}
105-
if (Gdx.input.isKeyPressed(Keys.LEFT))
106-
bucket.x -= 200 * Gdx.graphics.getDeltaTime();
107-
if (Gdx.input.isKeyPressed(Keys.RIGHT))
108-
bucket.x += 200 * Gdx.graphics.getDeltaTime();
109-
110-
// make sure the bucket stays within the screen bounds
111-
if (bucket.x < 0)
112-
bucket.x = 0;
113-
if (bucket.x > 800 - 64)
114-
bucket.x = 800 - 64;
115-
116-
// check if we need to create a new raindrop
117-
if (TimeUtils.nanoTime() - lastDropTime > 1000000000)
118-
spawnRaindrop();
119-
120-
// move the raindrops, remove any that are beneath the bottom edge of
121-
// the screen or that hit the bucket. In the later case we play back
122-
// a sound effect as well.
123-
Iterator<Rectangle> iter = raindrops.iterator();
124-
while (iter.hasNext()) {
125-
Rectangle raindrop = iter.next();
126-
raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
127-
if (raindrop.y + 64 < 0)
128-
iter.remove();
129-
if (raindrop.overlaps(bucket)) {
88+
}
89+
90+
private void logic() {
91+
float worldWidth = game.viewport.getWorldWidth();
92+
float worldHeight = game.viewport.getWorldHeight();
93+
float bucketWidth = bucketSprite.getWidth();
94+
float bucketHeight = bucketSprite.getHeight();
95+
float delta = Gdx.graphics.getDeltaTime();
96+
97+
bucketSprite.setX(MathUtils.clamp(bucketSprite.getX(), 0, worldWidth - bucketWidth));
98+
bucketRectangle.set(bucketSprite.getX(), bucketSprite.getY(), bucketWidth, bucketHeight);
99+
100+
for (int i = dropSprites.size - 1; i >= 0; i--) {
101+
Sprite dropSprite = dropSprites.get(i);
102+
float dropWidth = dropSprite.getWidth();
103+
float dropHeight = dropSprite.getHeight();
104+
105+
dropSprite.translateY(-2f * delta);
106+
dropRectangle.set(dropSprite.getX(), dropSprite.getY(), dropWidth, dropHeight);
107+
108+
if (dropSprite.getY() < -dropHeight) dropSprites.removeIndex(i);
109+
else if (bucketRectangle.overlaps(dropRectangle)) {
130110
dropsGathered++;
111+
dropSprites.removeIndex(i);
131112
dropSound.play();
132-
iter.remove();
133113
}
134114
}
115+
116+
dropTimer += delta;
117+
if (dropTimer > 1f) {
118+
dropTimer = 0;
119+
createDroplet();
120+
}
135121
}
136122

137-
@Override
138-
public void resize(int width, int height) {
123+
private void draw() {
124+
ScreenUtils.clear(Color.BLACK);
125+
game.viewport.apply();
126+
game.batch.setProjectionMatrix(game.viewport.getCamera().combined);
127+
game.batch.begin();
128+
129+
float worldWidth = game.viewport.getWorldWidth();
130+
float worldHeight = game.viewport.getWorldHeight();
131+
132+
game.batch.draw(backgroundTexture, 0, 0, worldWidth, worldHeight);
133+
bucketSprite.draw(game.batch);
134+
135+
game.font.draw(game.batch, "Drops collected: " + dropsGathered, 0, worldHeight);
136+
137+
for (Sprite dropSprite : dropSprites) {
138+
dropSprite.draw(game.batch);
139+
}
140+
141+
game.batch.end();
142+
}
143+
144+
private void createDroplet() {
145+
float dropWidth = 1;
146+
float dropHeight = 1;
147+
float worldWidth = game.viewport.getWorldWidth();
148+
float worldHeight = game.viewport.getWorldHeight();
149+
150+
Sprite dropSprite = new Sprite(dropTexture);
151+
dropSprite.setSize(dropWidth, dropHeight);
152+
dropSprite.setX(MathUtils.random(0F, worldWidth - dropWidth));
153+
dropSprite.setY(worldHeight);
154+
dropSprites.add(dropSprite);
139155
}
140156

141157
@Override
142-
public void show() {
143-
// start the playback of the background music
144-
// when the screen is shown
145-
rainMusic.play();
158+
public void resize(int width, int height) {
159+
game.viewport.update(width, height, true);
146160
}
147161

148162
@Override
@@ -159,10 +173,10 @@ public void resume() {
159173

160174
@Override
161175
public void dispose() {
162-
dropImage.dispose();
163-
bucketImage.dispose();
176+
backgroundTexture.dispose();
164177
dropSound.dispose();
165-
rainMusic.dispose();
178+
music.dispose();
179+
dropTexture.dispose();
180+
bucketTexture.dispose();
166181
}
167-
168182
}

assets/downloads/tutorials/extended-game-java/MainMenuScreen.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,28 @@
22

33
import com.badlogic.gdx.Gdx;
44
import com.badlogic.gdx.Screen;
5-
import com.badlogic.gdx.graphics.OrthographicCamera;
5+
import com.badlogic.gdx.graphics.Color;
66
import com.badlogic.gdx.utils.ScreenUtils;
77

88
public class MainMenuScreen implements Screen {
99

10-
final Drop game;
11-
OrthographicCamera camera;
10+
final Drop game;
1211

13-
public MainMenuScreen(final Drop gam) {
14-
game = gam;
15-
16-
camera = new OrthographicCamera();
17-
camera.setToOrtho(false, 800, 480);
12+
public MainMenuScreen(final Drop game) {
13+
this.game = game;
1814
}
1915

2016
@Override
2117
public void render(float delta) {
22-
ScreenUtils.clear(0, 0, 0.2f, 1);
18+
ScreenUtils.clear(Color.BLACK);
2319

24-
camera.update();
25-
game.batch.setProjectionMatrix(camera.combined);
20+
game.viewport.apply();
21+
game.batch.setProjectionMatrix(game.viewport.getCamera().combined);
2622

2723
game.batch.begin();
28-
game.font.draw(game.batch, "Welcome to Drop!!! ", 100, 150);
29-
game.font.draw(game.batch, "Tap anywhere to begin!", 100, 100);
24+
//draw text. Remember that x and y are in meters
25+
game.font.draw(game.batch, "Welcome to Drop!!! ", 1, 1.5f);
26+
game.font.draw(game.batch, "Tap anywhere to begin!", 1, 1);
3027
game.batch.end();
3128

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

3835
@Override
3936
public void resize(int width, int height) {
37+
game.viewport.update(width, height, true);
4038
}
4139

4240
@Override

wiki/math-utils/circles-planes-rays-etc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ A plane is an infinite two-dimensional surface in three-dimensional space descri
4343

4444
----
4545

46-
# [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)
46+
# [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)
4747

4848
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.
4949

wiki/start/a-simple-game.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ private static Lwjgl3ApplicationConfiguration getDefaultConfiguration() {
8989
## Loading Assets
9090
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.
9191

92-
<a href="/assets/downloads/tutorials/simple-game/bucket.png?nomagnify" download="bucket">bucket.png</a><br>
93-
<a href="/assets/downloads/tutorials/simple-game/drop.png?nomagnify" download="drop">drop.png</a><br>
94-
<a href="/assets/downloads/tutorials/simple-game/background.png?nomagnify" download="background">background.png</a><br>
95-
<a href="/assets/downloads/tutorials/simple-game/drop.mp3?nomagnify" download="drop">drop.mp3</a><br>
96-
<a href="/assets/downloads/tutorials/simple-game/music.mp3?nomagnify" download="music">music.mp3</a>
92+
<a href="/assets/downloads/tutorials/simple-game/bucket.png?nomagnify" download="bucket.png">bucket.png</a><br>
93+
<a href="/assets/downloads/tutorials/simple-game/drop.png?nomagnify" download="drop.png">drop.png</a><br>
94+
<a href="/assets/downloads/tutorials/simple-game/background.png?nomagnify" download="background.png">background.png</a><br>
95+
<a href="/assets/downloads/tutorials/simple-game/drop.mp3?nomagnify" download="drop.mp3">drop.mp3</a><br>
96+
<a href="/assets/downloads/tutorials/simple-game/music.mp3?nomagnify" download="music.mp3">music.mp3</a>
9797

9898
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:
9999

0 commit comments

Comments
 (0)