Advertisement
Veretelnikov_VO

Veretelnikov_lab7

Apr 19th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.95 KB | None | 0 0
  1. /////////Drop.java
  2.  
  3. package com.badlogic.drop;
  4.  
  5. import java.util.Iterator;
  6.  
  7. import com.badlogic.gdx.Game;
  8. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  9. import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  10.  
  11. public class Drop extends Game {
  12.  
  13.     public SpriteBatch batch;
  14.     public BitmapFont font;
  15.  
  16.     public void create() {
  17.  
  18.        batch = new SpriteBatch();
  19.        font = new BitmapFont(); // use libGDX's default Arial font
  20.        this.setScreen(new MainMenuScreen(this));
  21.        setScreen(new MyScene(this));
  22.  
  23.     }
  24.  
  25.     public void render() {
  26.        super.render(); // important!
  27.     }
  28.  
  29.     public void dispose() {
  30.        batch.dispose();
  31.        font.dispose();
  32.     }
  33.  
  34. }
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. ///////MyScene.java
  45.  
  46. package com.badlogic.drop;
  47.  
  48. import com.badlogic.gdx.Gdx;
  49. import com.badlogic.gdx.ScreenAdapter;
  50. import com.badlogic.gdx.graphics.GL20;
  51. import com.badlogic.gdx.graphics.PerspectiveCamera;
  52. import com.badlogic.gdx.graphics.VertexAttributes;
  53. import com.badlogic.gdx.graphics.g3d.Environment;
  54. import com.badlogic.gdx.graphics.g3d.ModelBatch;
  55. import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
  56. import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
  57. import com.badlogic.gdx.graphics.g3d.Material;
  58. import com.badlogic.gdx.graphics.g3d.ModelInstance;
  59. import com.badlogic.gdx.graphics.Color;
  60.  
  61. public class MyScene extends ScreenAdapter {
  62.     private final Drop game;
  63.     private PerspectiveCamera camera;
  64.     private ModelBatch modelBatch;
  65.     private Environment environment;
  66.  
  67.     public MyScene(final Drop game) {
  68.         this.game = game;
  69.         camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  70.         camera.position.set(10f, 10f, 10f);
  71.         camera.lookAt(0, 0, 0);
  72.         camera.near = 1f;
  73.         camera.far = 300f;
  74.  
  75.         modelBatch = new ModelBatch();
  76.         environment = new Environment();
  77.         environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
  78.     }
  79.  
  80.     @Override
  81.     public void render(float delta) {
  82.         Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  83.         Gdx.gl.glClearColor(0.1f, 0.2f, 0.3f, 1);
  84.         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
  85.  
  86.         camera.update();
  87.  
  88.         modelBatch.begin(camera);
  89.         // Создаем модель куба
  90.         ModelBuilder modelBuilder = new ModelBuilder();
  91.         com.badlogic.gdx.graphics.g3d.Model cubeModel = modelBuilder.createBox(5f, 5f, 5f,
  92.                 new Material(ColorAttribute.createDiffuse(Color.WHITE)),
  93.                 VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
  94.  
  95. // Отрисовываем куб
  96.         ModelInstance cubeInstance = new ModelInstance(cubeModel);
  97.         modelBatch.render(cubeInstance, environment);
  98.         modelBatch.end();
  99.  
  100.         if (Gdx.input.isTouched()) {
  101.             game.setScreen(new MainMenuScreen(game));
  102.             dispose();
  103.         }
  104.  
  105.     }
  106.  
  107.     @Override
  108.     public void resize(int width, int height) {
  109.         camera.viewportWidth = width;
  110.         camera.viewportHeight = height;
  111.         camera.update();
  112.     }
  113.  
  114.     @Override
  115.     public void dispose() {
  116.         modelBatch.dispose();
  117.     }
  118. }
  119.  
  120.  
  121.  
  122.  
  123.  
  124. ////////MainMenuScreen.java
  125.  
  126. package com.badlogic.drop;
  127.  
  128. import com.badlogic.gdx.Gdx;
  129. import com.badlogic.gdx.Screen;
  130. import com.badlogic.gdx.graphics.OrthographicCamera;
  131. import com.badlogic.gdx.utils.ScreenUtils;
  132.  
  133. public class MainMenuScreen implements Screen {
  134.  
  135.     final Drop game;
  136.     OrthographicCamera camera;
  137.  
  138.     public MainMenuScreen(final Drop game) {
  139.         this.game = game;
  140.  
  141.         camera = new OrthographicCamera();
  142.         camera.setToOrtho(false, 800, 480);
  143.     }
  144.  
  145.     @Override
  146.     public void render(float delta) {
  147.         ScreenUtils.clear(0, 0, 0.2f, 1);
  148.  
  149.         camera.update();
  150.         game.batch.setProjectionMatrix(camera.combined);
  151.  
  152.         game.batch.begin();
  153.         game.font.draw(game.batch, "Welcome to Drop!!! ", 100, 150);
  154.         game.font.draw(game.batch, "Tap anywhere to begin!", 100, 100);
  155.         game.batch.end();
  156.  
  157.         if (Gdx.input.isTouched()) {
  158.             game.setScreen(new GameScreen(game));
  159.             dispose();
  160.         }
  161.     }
  162.  
  163.     @Override
  164.     public void resize(int width, int height) {
  165.     }
  166.  
  167.     @Override
  168.     public void show() {
  169.     }
  170.  
  171.     @Override
  172.     public void hide() {
  173.     }
  174.  
  175.     @Override
  176.     public void pause() {
  177.     }
  178.  
  179.     @Override
  180.     public void resume() {
  181.     }
  182.  
  183.     @Override
  184.     public void dispose() {
  185.     }
  186.  
  187. }
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194. ///////////GameScreen.java
  195.  
  196. package com.badlogic.drop;
  197.  
  198. import com.badlogic.gdx.Gdx;
  199. import com.badlogic.gdx.Input;
  200. import com.badlogic.gdx.Screen;
  201. import com.badlogic.gdx.audio.Music;
  202. import com.badlogic.gdx.audio.Sound;
  203. import com.badlogic.gdx.graphics.OrthographicCamera;
  204. import com.badlogic.gdx.graphics.Texture;
  205. import com.badlogic.gdx.math.MathUtils;
  206. import com.badlogic.gdx.math.Rectangle;
  207. import com.badlogic.gdx.math.Vector3;
  208. import com.badlogic.gdx.utils.Array;
  209. import com.badlogic.gdx.utils.ScreenUtils;
  210. import com.badlogic.gdx.utils.TimeUtils;
  211.  
  212. import java.util.Iterator;
  213.  
  214. public class GameScreen implements Screen {
  215.     final Drop game;
  216.  
  217.     Texture dropImage;
  218.     Texture bucketImage;
  219.     Sound dropSound;
  220.     Music rainMusic;
  221.     OrthographicCamera camera;
  222.     Rectangle bucket;
  223.     Array<Rectangle> raindrops;
  224.     long lastDropTime;
  225.     int dropsGathered;
  226.  
  227.     public GameScreen(final Drop game) {
  228.         this.game = game;
  229.  
  230.         // load the images for the droplet and the bucket, 64x64 pixels each
  231.         dropImage = new Texture(Gdx.files.internal("droplet.png"));
  232.         bucketImage = new Texture(Gdx.files.internal("bucket.png"));
  233.  
  234.         // load the drop sound effect and the rain background "music"
  235.         dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
  236.         rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
  237.         rainMusic.setLooping(true);
  238.  
  239.         // create the camera and the SpriteBatch
  240.         camera = new OrthographicCamera();
  241.         camera.setToOrtho(false, 800, 480);
  242.  
  243.         // create a Rectangle to logically represent the bucket
  244.         bucket = new Rectangle();
  245.         bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
  246.         bucket.y = 20; // bottom left corner of the bucket is 20 pixels above
  247.         // the bottom screen edge
  248.         bucket.width = 64;
  249.         bucket.height = 64;
  250.  
  251.         // create the raindrops array and spawn the first raindrop
  252.         raindrops = new Array<Rectangle>();
  253.         spawnRaindrop();
  254.  
  255.     }
  256.  
  257.     private void spawnRaindrop() {
  258.         Rectangle raindrop = new Rectangle();
  259.         raindrop.x = MathUtils.random(0, 800 - 64);
  260.         raindrop.y = 480;
  261.         raindrop.width = 64;
  262.         raindrop.height = 64;
  263.         raindrops.add(raindrop);
  264.         lastDropTime = TimeUtils.nanoTime();
  265.     }
  266.  
  267.     @Override
  268.     public void render(float delta) {
  269.         // clear the screen with a dark blue color. The
  270.         // arguments to clear are the red, green
  271.         // blue and alpha component in the range [0,1]
  272.         // of the color to be used to clear the screen.
  273.         ScreenUtils.clear(0, 0, 0.2f, 1);
  274.  
  275.         // tell the camera to update its matrices.
  276.         camera.update();
  277.  
  278.         // tell the SpriteBatch to render in the
  279.         // coordinate system specified by the camera.
  280.         game.batch.setProjectionMatrix(camera.combined);
  281.  
  282.         // begin a new batch and draw the bucket and
  283.         // all drops
  284.         game.batch.begin();
  285.         game.font.draw(game.batch, "Drops Collected: " + dropsGathered, 0, 480);
  286.         game.batch.draw(bucketImage, bucket.x, bucket.y, bucket.width, bucket.height);
  287.         for (Rectangle raindrop : raindrops) {
  288.             game.batch.draw(dropImage, raindrop.x, raindrop.y);
  289.         }
  290.         game.batch.end();
  291.  
  292.         // process user input
  293.         if (Gdx.input.isTouched()) {
  294.             Vector3 touchPos = new Vector3();
  295.             touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
  296.             camera.unproject(touchPos);
  297.             bucket.x = touchPos.x - 64 / 2;
  298.         }
  299.         if (Gdx.input.isKeyPressed(Input.Keys.LEFT))
  300.             bucket.x -= 200 * Gdx.graphics.getDeltaTime();
  301.         if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))
  302.             bucket.x += 200 * Gdx.graphics.getDeltaTime();
  303.  
  304.         // make sure the bucket stays within the screen bounds
  305.         if (bucket.x < 0)
  306.             bucket.x = 0;
  307.         if (bucket.x > 800 - 64)
  308.             bucket.x = 800 - 64;
  309.  
  310.         // check if we need to create a new raindrop
  311.         if (TimeUtils.nanoTime() - lastDropTime > 1000000000)
  312.             spawnRaindrop();
  313.  
  314.         // move the raindrops, remove any that are beneath the bottom edge of
  315.         // the screen or that hit the bucket. In the later case we increase the
  316.         // value our drops counter and add a sound effect.
  317.         Iterator<Rectangle> iter = raindrops.iterator();
  318.         while (iter.hasNext()) {
  319.             Rectangle raindrop = iter.next();
  320.             raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
  321.             if (raindrop.y + 64 < 0)
  322.                 iter.remove();
  323.             if (raindrop.overlaps(bucket)) {
  324.                 dropsGathered++;
  325.                 dropSound.play();
  326.                 iter.remove();
  327.             }
  328.         }
  329.     }
  330.  
  331.     @Override
  332.     public void resize(int width, int height) {
  333.     }
  334.  
  335.     @Override
  336.     public void show() {
  337.         // start the playback of the background music
  338.         // when the screen is shown
  339.         rainMusic.play();
  340.     }
  341.  
  342.     @Override
  343.     public void hide() {
  344.     }
  345.  
  346.     @Override
  347.     public void pause() {
  348.     }
  349.  
  350.     @Override
  351.     public void resume() {
  352.     }
  353.  
  354.     @Override
  355.     public void dispose() {
  356.         dropImage.dispose();
  357.         bucketImage.dispose();
  358.         dropSound.dispose();
  359.         rainMusic.dispose();
  360.     }
  361.  
  362. }
  363.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement