Advertisement
Guest User

GameScreen.java - Parent

a guest
Apr 26th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.75 KB | None | 0 0
  1. package com.badlogic.drop;
  2.  
  3. import java.util.Iterator;
  4.  
  5.  
  6. import com.badlogic.gdx.Gdx;
  7. import com.badlogic.gdx.Input.Keys;
  8. import com.badlogic.gdx.Screen;
  9. import com.badlogic.gdx.audio.Music;
  10. import com.badlogic.gdx.audio.Sound;
  11. import com.badlogic.gdx.graphics.GL20;
  12. import com.badlogic.gdx.graphics.OrthographicCamera;
  13. import com.badlogic.gdx.graphics.Texture;
  14. import com.badlogic.gdx.math.MathUtils;
  15. import com.badlogic.gdx.math.Rectangle;
  16. import com.badlogic.gdx.math.Vector3;
  17. import com.badlogic.gdx.utils.Array;
  18. import com.badlogic.gdx.utils.TimeUtils;
  19.  
  20. public class GameScreen implements Screen {
  21.   final Drop game;
  22.  
  23.     Texture dropImage;
  24.     Texture bucketImage;
  25.     Sound dropSound;
  26.     Music rainMusic;
  27.     OrthographicCamera camera;
  28.     Rectangle bucket;
  29.     Array<Rectangle> raindrops;
  30.     long lastDropTime;
  31.     int dropsGathered;
  32.  
  33.     public GameScreen(final Drop gam) {
  34.         this.game = gam;
  35.  
  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"));
  39.  
  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);
  44.  
  45.         // create the camera and the SpriteBatch
  46.         camera = new OrthographicCamera();
  47.         camera.setToOrtho(false, 800, 480);
  48.  
  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;
  56.  
  57.         // create the raindrops array and spawn the first raindrop
  58.         raindrops = new Array<Rectangle>();
  59.         spawnRaindrop();
  60.  
  61.     }
  62.  
  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();
  71.     }
  72.  
  73.     @Override
  74.     public void render(float delta) {
  75.         // clear the screen with a dark blue color. The
  76.         // arguments to glClearColor 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.         Gdx.gl.glClearColor(0, 0, 0.2f, 1);
  80.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  81.  
  82.         // tell the camera to update its matrices.
  83.         camera.update();
  84.  
  85.         // tell the SpriteBatch to render in the
  86.         // coordinate system specified by the camera.
  87.         game.batch.setProjectionMatrix(camera.combined);
  88.  
  89.         // begin a new batch and draw the bucket and
  90.         // all drops
  91.         game.batch.begin();
  92.         game.font.draw(game.batch, "Drops Collected: " + dropsGathered, 0, 480);
  93.         game.batch.draw(bucketImage, bucket.x, bucket.y);
  94.         for (Rectangle raindrop : raindrops) {
  95.             game.batch.draw(dropImage, raindrop.x, raindrop.y);
  96.         }
  97.         game.batch.end();
  98.  
  99.         // process user input
  100.         if (Gdx.input.isTouched()) {
  101.             Vector3 touchPos = new Vector3();
  102.             touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
  103.             camera.unproject(touchPos);
  104.             bucket.x = touchPos.x - 64 / 2;
  105.         }
  106.         if (Gdx.input.isKeyPressed(Keys.LEFT))
  107.             bucket.x -= 200 * Gdx.graphics.getDeltaTime();
  108.         if (Gdx.input.isKeyPressed(Keys.RIGHT))
  109.             bucket.x += 200 * Gdx.graphics.getDeltaTime();
  110.  
  111.         // make sure the bucket stays within the screen bounds
  112.         if (bucket.x < 0)
  113.             bucket.x = 0;
  114.         if (bucket.x > 800 - 64)
  115.             bucket.x = 800 - 64;
  116.  
  117.         // check if we need to create a new raindrop
  118.         if (TimeUtils.nanoTime() - lastDropTime > 300000000)
  119.             spawnRaindrop();
  120.  
  121.         // move the raindrops, remove any that are beneath the bottom edge of
  122.         // the screen or that hit the bucket. In the later case we play back
  123.         // a sound effect as well.
  124.         Iterator<Rectangle> iter = raindrops.iterator();
  125.         while (iter.hasNext()) {
  126.             Rectangle raindrop = iter.next();
  127.             raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
  128.             if (raindrop.y + 64 < 0)
  129.                 iter.remove();
  130.             if (raindrop.overlaps(bucket)) {
  131.                 dropsGathered++;
  132.                 dropSound.play();
  133.                 iter.remove();
  134.             }
  135.             if (raindrop.y + 64 < 0) {
  136.                 game.setScreen(new EndGameScreen(game));
  137.                 dispose();
  138.             }
  139.         }
  140.     }
  141.  
  142.     @Override
  143.     public void resize(int width, int height) {
  144.     }
  145.  
  146.     @Override
  147.     public void show() {
  148.         // start the playback of the background music
  149.         // when the screen is shown
  150.         rainMusic.play();
  151.     }
  152.  
  153.     @Override
  154.     public void hide() {
  155.     }
  156.  
  157.     @Override
  158.     public void pause() {
  159.     }
  160.  
  161.     @Override
  162.     public void resume() {
  163.     }
  164.  
  165.     @Override
  166.     public void dispose() {
  167.         dropImage.dispose();
  168.         bucketImage.dispose();
  169.         dropSound.dispose();
  170.         rainMusic.dispose();
  171.     }
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement