11package com .badlogic .drop ;
22
3- import java .util .Iterator ;
4-
53import com .badlogic .gdx .Gdx ;
6- import com .badlogic .gdx .Input . Keys ;
4+ import com .badlogic .gdx .Input ;
75import com .badlogic .gdx .Screen ;
86import com .badlogic .gdx .audio .Music ;
97import com .badlogic .gdx .audio .Sound ;
10- import com .badlogic .gdx .graphics .OrthographicCamera ;
8+ import com .badlogic .gdx .graphics .Color ;
119import com .badlogic .gdx .graphics .Texture ;
10+ import com .badlogic .gdx .graphics .g2d .Sprite ;
1211import com .badlogic .gdx .math .MathUtils ;
1312import com .badlogic .gdx .math .Rectangle ;
14- import com .badlogic .gdx .math .Vector3 ;
13+ import com .badlogic .gdx .math .Vector2 ;
1514import com .badlogic .gdx .utils .Array ;
1615import com .badlogic .gdx .utils .ScreenUtils ;
17- import com .badlogic .gdx .utils .TimeUtils ;
1816
1917public 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}
0 commit comments