Advertisement
Veretelnikov_VO

Veretelnikov_lab6.1

Apr 19th, 2024
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.88 KB | None | 0 0
  1. //Проста гра, без головного екрану
  2. package com.badlogic.drop;
  3.  
  4. import java.util.Iterator;
  5.  
  6. import com.badlogic.gdx.ApplicationAdapter;
  7. import com.badlogic.gdx.Gdx;
  8. import com.badlogic.gdx.Input;
  9. import com.badlogic.gdx.audio.Music;
  10. import com.badlogic.gdx.audio.Sound;
  11. import com.badlogic.gdx.graphics.OrthographicCamera;
  12. import com.badlogic.gdx.graphics.Texture;
  13. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  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.ScreenUtils;
  19. import com.badlogic.gdx.utils.TimeUtils;
  20.  
  21. public class Drop extends ApplicationAdapter {
  22.     private Texture dropImage;
  23.     private Texture bucketImage;
  24.     private Sound dropSound;
  25.     private Music rainMusic;
  26.     private SpriteBatch batch;
  27.     private OrthographicCamera camera;
  28.     private Rectangle bucket;
  29.     private Array<Rectangle> raindrops;
  30.     private long lastDropTime;
  31.  
  32.     @Override
  33.     public void create() {
  34.        // load the images for the droplet and the bucket, 64x64 pixels each
  35.        dropImage = new Texture(Gdx.files.internal("droplet.png"));
  36.        bucketImage = new Texture(Gdx.files.internal("bucket.png"));
  37.  
  38.        // load the drop sound effect and the rain background "music"
  39.        dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
  40.        rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
  41.  
  42.        // start the playback of the background music immediately
  43.        rainMusic.setLooping(true);
  44.        rainMusic.play();
  45.  
  46.        // create the camera and the SpriteBatch
  47.        camera = new OrthographicCamera();
  48.        camera.setToOrtho(false, 800, 480);
  49.        batch = new SpriteBatch();
  50.  
  51.        // create a Rectangle to logically represent the bucket
  52.        bucket = new Rectangle();
  53.        bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
  54.        bucket.y = 20; // bottom left corner of the bucket is 20 pixels above the bottom screen edge
  55.        bucket.width = 64;
  56.        bucket.height = 64;
  57.  
  58.        // create the raindrops array and spawn the first raindrop
  59.        raindrops = new Array<Rectangle>();
  60.        spawnRaindrop();
  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() {
  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();
  83.  
  84.        // tell the SpriteBatch to render in the
  85.        // coordinate system specified by the camera.
  86.        batch.setProjectionMatrix(camera.combined);
  87.  
  88.        // begin a new batch and draw the bucket and
  89.        // all drops
  90.        batch.begin();
  91.        batch.draw(bucketImage, bucket.x, bucket.y);
  92.        for(Rectangle raindrop: raindrops) {
  93.           batch.draw(dropImage, raindrop.x, raindrop.y);
  94.        }
  95.        batch.end();
  96.  
  97.        // process user input
  98.        if(Gdx.input.isTouched()) {
  99.           Vector3 touchPos = new Vector3();
  100.           touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
  101.           camera.unproject(touchPos);
  102.           bucket.x = touchPos.x - 64 / 2;
  103.        }
  104.        if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.getDeltaTime();
  105.        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.getDeltaTime();
  106.  
  107.        // make sure the bucket stays within the screen bounds
  108.        if(bucket.x < 0) bucket.x = 0;
  109.        if(bucket.x > 800 - 64) bucket.x = 800 - 64;
  110.  
  111.        // check if we need to create a new raindrop
  112.        if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();
  113.  
  114.        // move the raindrops, remove any that are beneath the bottom edge of
  115.        // the screen or that hit the bucket. In the latter case we play back
  116.        // a sound effect as well.
  117.        for (Iterator<Rectangle> iter = raindrops.iterator(); iter.hasNext(); ) {
  118.           Rectangle raindrop = iter.next();
  119.           raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
  120.           if(raindrop.y + 64 < 0) iter.remove();
  121.           if(raindrop.overlaps(bucket)) {
  122.              dropSound.play();
  123.              iter.remove();
  124.           }
  125.        }
  126.     }
  127.  
  128.     @Override
  129.     public void dispose() {
  130.        // dispose of all the native resources
  131.        dropImage.dispose();
  132.        bucketImage.dispose();
  133.        dropSound.dispose();
  134.        rainMusic.dispose();
  135.        batch.dispose();
  136.     }
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement