Advertisement
Veretelnikov_VO

Veretelnikov_lab6.2

Apr 19th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.40 KB | None | 0 0
  1. //Реалізація екранів меню та гри
  2. /////////Drop.java
  3.  
  4. package com.badlogic.drop;
  5.  
  6. import java.util.Iterator;
  7.  
  8. import com.badlogic.gdx.Game;
  9. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  10. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  11.  
  12.  
  13. public class Drop extends Game {
  14.  
  15.     public SpriteBatch batch;
  16.     public BitmapFont font;
  17.  
  18.     public void create() {
  19.        batch = new SpriteBatch();
  20.        font = new BitmapFont(); // use libGDX's default Arial font
  21.        this.setScreen(new MainMenuScreen(this));
  22.     }
  23.  
  24.     public void render() {
  25.        super.render(); // important!
  26.     }
  27.  
  28.     public void dispose() {
  29.        batch.dispose();
  30.        font.dispose();
  31.     }
  32.  
  33. }
  34.  
  35.  
  36. ////////MainMenuScreen.java
  37.  
  38. package com.badlogic.drop;
  39.  
  40. import com.badlogic.gdx.Gdx;
  41. import com.badlogic.gdx.Screen;
  42. import com.badlogic.gdx.graphics.OrthographicCamera;
  43. import com.badlogic.gdx.utils.ScreenUtils;
  44.  
  45. public class MainMenuScreen implements Screen {
  46.  
  47.     final Drop game;
  48.     OrthographicCamera camera;
  49.  
  50.     public MainMenuScreen(final Drop game) {
  51.         this.game = game;
  52.  
  53.         camera = new OrthographicCamera();
  54.         camera.setToOrtho(false, 800, 480);
  55.     }
  56.  
  57.     @Override
  58.     public void render(float delta) {
  59.         ScreenUtils.clear(0, 0, 0.2f, 1);
  60.  
  61.         camera.update();
  62.         game.batch.setProjectionMatrix(camera.combined);
  63.  
  64.         game.batch.begin();
  65.         game.font.draw(game.batch, "Welcome to Drop!!! ", 100, 150);
  66.         game.font.draw(game.batch, "Tap anywhere to begin!", 100, 100);
  67.         game.batch.end();
  68.  
  69.         if (Gdx.input.isTouched()) {
  70.             game.setScreen(new GameScreen(game));
  71.             dispose();
  72.         }
  73.     }
  74.  
  75.     @Override
  76.     public void resize(int width, int height) {
  77.     }
  78.  
  79.     @Override
  80.     public void show() {
  81.     }
  82.  
  83.     @Override
  84.     public void hide() {
  85.     }
  86.  
  87.     @Override
  88.     public void pause() {
  89.     }
  90.  
  91.     @Override
  92.     public void resume() {
  93.     }
  94.  
  95.     @Override
  96.     public void dispose() {
  97.     }
  98.  
  99. }
  100.  
  101.  
  102.  
  103.  
  104.  
  105. //////GameScreen.java
  106.  
  107. package com.badlogic.drop;
  108.  
  109. import com.badlogic.gdx.Gdx;
  110. import com.badlogic.gdx.Input;
  111. import com.badlogic.gdx.Screen;
  112. import com.badlogic.gdx.audio.Music;
  113. import com.badlogic.gdx.audio.Sound;
  114. import com.badlogic.gdx.graphics.OrthographicCamera;
  115. import com.badlogic.gdx.graphics.Texture;
  116. import com.badlogic.gdx.math.MathUtils;
  117. import com.badlogic.gdx.math.Rectangle;
  118. import com.badlogic.gdx.math.Vector3;
  119. import com.badlogic.gdx.utils.Array;
  120. import com.badlogic.gdx.utils.ScreenUtils;
  121. import com.badlogic.gdx.utils.TimeUtils;
  122.  
  123. import java.util.Iterator;
  124.  
  125. class GameScreen implements Screen {
  126.     final Drop game;
  127.  
  128.     Texture dropImage;
  129.     Texture bucketImage;
  130.     Sound dropSound;
  131.     Music rainMusic;
  132.     OrthographicCamera camera;
  133.     Rectangle bucket;
  134.     Array<Rectangle> raindrops;
  135.     long lastDropTime;
  136.     int dropsGathered;
  137.  
  138.     public GameScreen(final Drop game) {
  139.         this.game = game;
  140.  
  141.         // load the images for the droplet and the bucket, 64x64 pixels each
  142.         dropImage = new Texture(Gdx.files.internal("droplet.png"));
  143.         bucketImage = new Texture(Gdx.files.internal("bucket.png"));
  144.  
  145.         // load the drop sound effect and the rain background "music"
  146.         dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
  147.         rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
  148.         rainMusic.setLooping(true);
  149.  
  150.         // create the camera and the SpriteBatch
  151.         camera = new OrthographicCamera();
  152.         camera.setToOrtho(false, 800, 480);
  153.  
  154.         // create a Rectangle to logically represent the bucket
  155.         bucket = new Rectangle();
  156.         bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
  157.         bucket.y = 20; // bottom left corner of the bucket is 20 pixels above
  158.         // the bottom screen edge
  159.         bucket.width = 64;
  160.         bucket.height = 64;
  161.  
  162.         // create the raindrops array and spawn the first raindrop
  163.         raindrops = new Array<Rectangle>();
  164.         spawnRaindrop();
  165.  
  166.     }
  167.  
  168.     private void spawnRaindrop() {
  169.         Rectangle raindrop = new Rectangle();
  170.         raindrop.x = MathUtils.random(0, 800 - 64);
  171.         raindrop.y = 480;
  172.         raindrop.width = 64;
  173.         raindrop.height = 64;
  174.         raindrops.add(raindrop);
  175.         lastDropTime = TimeUtils.nanoTime();
  176.     }
  177.  
  178.     @Override
  179.     public void render(float delta) {
  180.         // clear the screen with a dark blue color. The
  181.         // arguments to clear are the red, green
  182.         // blue and alpha component in the range [0,1]
  183.         // of the color to be used to clear the screen.
  184.         ScreenUtils.clear(0, 0, 0.2f, 1);
  185.  
  186.         // tell the camera to update its matrices.
  187.         camera.update();
  188.  
  189.         // tell the SpriteBatch to render in the
  190.         // coordinate system specified by the camera.
  191.         game.batch.setProjectionMatrix(camera.combined);
  192.  
  193.         // begin a new batch and draw the bucket and
  194.         // all drops
  195.         game.batch.begin();
  196.         game.font.draw(game.batch, "Drops Collected: " + dropsGathered, 0, 480);
  197.         game.batch.draw(bucketImage, bucket.x, bucket.y, bucket.width, bucket.height);
  198.         for (Rectangle raindrop : raindrops) {
  199.             game.batch.draw(dropImage, raindrop.x, raindrop.y);
  200.         }
  201.         game.batch.end();
  202.  
  203.         // process user input
  204.         if (Gdx.input.isTouched()) {
  205.             Vector3 touchPos = new Vector3();
  206.             touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
  207.             camera.unproject(touchPos);
  208.             bucket.x = touchPos.x - 64 / 2;
  209.         }
  210.         if (Gdx.input.isKeyPressed(Input.Keys.LEFT))
  211.             bucket.x -= 200 * Gdx.graphics.getDeltaTime();
  212.         if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))
  213.             bucket.x += 200 * Gdx.graphics.getDeltaTime();
  214.  
  215.         // make sure the bucket stays within the screen bounds
  216.         if (bucket.x < 0)
  217.             bucket.x = 0;
  218.         if (bucket.x > 800 - 64)
  219.             bucket.x = 800 - 64;
  220.  
  221.         // check if we need to create a new raindrop
  222.         if (TimeUtils.nanoTime() - lastDropTime > 1000000000)
  223.             spawnRaindrop();
  224.  
  225.         // move the raindrops, remove any that are beneath the bottom edge of
  226.         // the screen or that hit the bucket. In the later case we increase the
  227.         // value our drops counter and add a sound effect.
  228.         Iterator<Rectangle> iter = raindrops.iterator();
  229.         while (iter.hasNext()) {
  230.             Rectangle raindrop = iter.next();
  231.             raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
  232.             if (raindrop.y + 64 < 0)
  233.                 iter.remove();
  234.             if (raindrop.overlaps(bucket)) {
  235.                 dropsGathered++;
  236.                 dropSound.play();
  237.                 iter.remove();
  238.             }
  239.         }
  240.     }
  241.  
  242.     @Override
  243.     public void resize(int width, int height) {
  244.     }
  245.  
  246.     @Override
  247.     public void show() {
  248.         // start the playback of the background music
  249.         // when the screen is shown
  250.         rainMusic.play();
  251.     }
  252.  
  253.     @Override
  254.     public void hide() {
  255.     }
  256.  
  257.     @Override
  258.     public void pause() {
  259.     }
  260.  
  261.     @Override
  262.     public void resume() {
  263.     }
  264.  
  265.     @Override
  266.     public void dispose() {
  267.         dropImage.dispose();
  268.         bucketImage.dispose();
  269.         dropSound.dispose();
  270.         rainMusic.dispose();
  271.     }
  272.  
  273. }
  274.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement