Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////Drop.java
- package com.badlogic.drop;
- import java.util.Iterator;
- import com.badlogic.gdx.Game;
- import com.badlogic.gdx.graphics.g2d.BitmapFont;
- import com.badlogic.gdx.graphics.g2d.SpriteBatch;
- public class Drop extends Game {
- public SpriteBatch batch;
- public BitmapFont font;
- public void create() {
- batch = new SpriteBatch();
- font = new BitmapFont(); // use libGDX's default Arial font
- this.setScreen(new MainMenuScreen(this));
- setScreen(new MyScene(this));
- }
- public void render() {
- super.render(); // important!
- }
- public void dispose() {
- batch.dispose();
- font.dispose();
- }
- }
- ///////MyScene.java
- package com.badlogic.drop;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.ScreenAdapter;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.PerspectiveCamera;
- import com.badlogic.gdx.graphics.VertexAttributes;
- import com.badlogic.gdx.graphics.g3d.Environment;
- import com.badlogic.gdx.graphics.g3d.ModelBatch;
- import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
- import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
- import com.badlogic.gdx.graphics.g3d.Material;
- import com.badlogic.gdx.graphics.g3d.ModelInstance;
- import com.badlogic.gdx.graphics.Color;
- public class MyScene extends ScreenAdapter {
- private final Drop game;
- private PerspectiveCamera camera;
- private ModelBatch modelBatch;
- private Environment environment;
- public MyScene(final Drop game) {
- this.game = game;
- camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- camera.position.set(10f, 10f, 10f);
- camera.lookAt(0, 0, 0);
- camera.near = 1f;
- camera.far = 300f;
- modelBatch = new ModelBatch();
- environment = new Environment();
- environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
- }
- @Override
- public void render(float delta) {
- Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- Gdx.gl.glClearColor(0.1f, 0.2f, 0.3f, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
- camera.update();
- modelBatch.begin(camera);
- // Создаем модель куба
- ModelBuilder modelBuilder = new ModelBuilder();
- com.badlogic.gdx.graphics.g3d.Model cubeModel = modelBuilder.createBox(5f, 5f, 5f,
- new Material(ColorAttribute.createDiffuse(Color.WHITE)),
- VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
- // Отрисовываем куб
- ModelInstance cubeInstance = new ModelInstance(cubeModel);
- modelBatch.render(cubeInstance, environment);
- modelBatch.end();
- if (Gdx.input.isTouched()) {
- game.setScreen(new MainMenuScreen(game));
- dispose();
- }
- }
- @Override
- public void resize(int width, int height) {
- camera.viewportWidth = width;
- camera.viewportHeight = height;
- camera.update();
- }
- @Override
- public void dispose() {
- modelBatch.dispose();
- }
- }
- ////////MainMenuScreen.java
- package com.badlogic.drop;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Screen;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.utils.ScreenUtils;
- public class MainMenuScreen implements Screen {
- final Drop game;
- OrthographicCamera camera;
- public MainMenuScreen(final Drop game) {
- this.game = game;
- camera = new OrthographicCamera();
- camera.setToOrtho(false, 800, 480);
- }
- @Override
- public void render(float delta) {
- ScreenUtils.clear(0, 0, 0.2f, 1);
- camera.update();
- game.batch.setProjectionMatrix(camera.combined);
- game.batch.begin();
- game.font.draw(game.batch, "Welcome to Drop!!! ", 100, 150);
- game.font.draw(game.batch, "Tap anywhere to begin!", 100, 100);
- game.batch.end();
- if (Gdx.input.isTouched()) {
- game.setScreen(new GameScreen(game));
- dispose();
- }
- }
- @Override
- public void resize(int width, int height) {
- }
- @Override
- public void show() {
- }
- @Override
- public void hide() {
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- }
- @Override
- public void dispose() {
- }
- }
- ///////////GameScreen.java
- package com.badlogic.drop;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.Screen;
- import com.badlogic.gdx.audio.Music;
- import com.badlogic.gdx.audio.Sound;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.math.MathUtils;
- import com.badlogic.gdx.math.Rectangle;
- import com.badlogic.gdx.math.Vector3;
- import com.badlogic.gdx.utils.Array;
- import com.badlogic.gdx.utils.ScreenUtils;
- import com.badlogic.gdx.utils.TimeUtils;
- import java.util.Iterator;
- public class GameScreen implements Screen {
- final Drop game;
- Texture dropImage;
- Texture bucketImage;
- Sound dropSound;
- Music rainMusic;
- OrthographicCamera camera;
- Rectangle bucket;
- Array<Rectangle> raindrops;
- long lastDropTime;
- int dropsGathered;
- public GameScreen(final Drop game) {
- this.game = game;
- // load the images for the droplet and the bucket, 64x64 pixels each
- dropImage = new Texture(Gdx.files.internal("droplet.png"));
- bucketImage = new Texture(Gdx.files.internal("bucket.png"));
- // load the drop sound effect and the rain background "music"
- dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
- rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
- rainMusic.setLooping(true);
- // create the camera and the SpriteBatch
- camera = new OrthographicCamera();
- camera.setToOrtho(false, 800, 480);
- // create a Rectangle to logically represent the bucket
- bucket = new Rectangle();
- bucket.x = 800 / 2 - 64 / 2; // center the bucket horizontally
- bucket.y = 20; // bottom left corner of the bucket is 20 pixels above
- // the bottom screen edge
- bucket.width = 64;
- bucket.height = 64;
- // create the raindrops array and spawn the first raindrop
- raindrops = new Array<Rectangle>();
- spawnRaindrop();
- }
- private void spawnRaindrop() {
- Rectangle raindrop = new Rectangle();
- raindrop.x = MathUtils.random(0, 800 - 64);
- raindrop.y = 480;
- raindrop.width = 64;
- raindrop.height = 64;
- raindrops.add(raindrop);
- lastDropTime = TimeUtils.nanoTime();
- }
- @Override
- public void render(float delta) {
- // clear the screen with a dark blue color. The
- // arguments to clear are the red, green
- // blue and alpha component in the range [0,1]
- // of the color to be used to clear the screen.
- ScreenUtils.clear(0, 0, 0.2f, 1);
- // tell the camera to update its matrices.
- camera.update();
- // tell the SpriteBatch to render in the
- // coordinate system specified by the camera.
- game.batch.setProjectionMatrix(camera.combined);
- // begin a new batch and draw the bucket and
- // all drops
- game.batch.begin();
- game.font.draw(game.batch, "Drops Collected: " + dropsGathered, 0, 480);
- game.batch.draw(bucketImage, bucket.x, bucket.y, bucket.width, bucket.height);
- for (Rectangle raindrop : raindrops) {
- game.batch.draw(dropImage, raindrop.x, raindrop.y);
- }
- game.batch.end();
- // process user input
- if (Gdx.input.isTouched()) {
- Vector3 touchPos = new Vector3();
- touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
- camera.unproject(touchPos);
- bucket.x = touchPos.x - 64 / 2;
- }
- if (Gdx.input.isKeyPressed(Input.Keys.LEFT))
- bucket.x -= 200 * Gdx.graphics.getDeltaTime();
- if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))
- bucket.x += 200 * Gdx.graphics.getDeltaTime();
- // make sure the bucket stays within the screen bounds
- if (bucket.x < 0)
- bucket.x = 0;
- if (bucket.x > 800 - 64)
- bucket.x = 800 - 64;
- // check if we need to create a new raindrop
- if (TimeUtils.nanoTime() - lastDropTime > 1000000000)
- spawnRaindrop();
- // move the raindrops, remove any that are beneath the bottom edge of
- // the screen or that hit the bucket. In the later case we increase the
- // value our drops counter and add a sound effect.
- Iterator<Rectangle> iter = raindrops.iterator();
- while (iter.hasNext()) {
- Rectangle raindrop = iter.next();
- raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
- if (raindrop.y + 64 < 0)
- iter.remove();
- if (raindrop.overlaps(bucket)) {
- dropsGathered++;
- dropSound.play();
- iter.remove();
- }
- }
- }
- @Override
- public void resize(int width, int height) {
- }
- @Override
- public void show() {
- // start the playback of the background music
- // when the screen is shown
- rainMusic.play();
- }
- @Override
- public void hide() {
- }
- @Override
- public void pause() {
- }
- @Override
- public void resume() {
- }
- @Override
- public void dispose() {
- dropImage.dispose();
- bucketImage.dispose();
- dropSound.dispose();
- rainMusic.dispose();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement