libGDX学习记录(三)接水滴

前期准备

1.配置好android环境的IDEA
2.下载libGDX项目构建工具—gdx-setup.jar

生成项目

1.双击上述gdx-setup.jar,
libGDX学习记录(三)接水滴
2.选择最下方左边的Advanced按钮,添加一个镜像地址:
https://maven.aliyun.com/nexus/content/groups/public/
点击save。
libGDX学习记录(三)接水滴
3.点击右侧Generate,点击弹出的选项是,再次点击弹出的选项是,开始创建项目。第一次可能会显示build failed,再执行一次Generate,即可。
libGDX学习记录(三)接水滴
4.IDEA打开项目
libGDX学习记录(三)接水滴
5.导入图片音频资源
libGDX学习记录(三)接水滴
6.修改配置
libGDX学习记录(三)接水滴
7.更改图片名libGDX学习记录(三)接水滴
8.Drop.java完整源码

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
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.TimeUtils;

import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import static com.badlogic.gdx.Gdx.gl;
import static com.badlogic.gdx.Gdx.input;

public class Drop extends ApplicationAdapter {

    private Texture dropImage;
    private Texture bucketImage;
    private Sound dropSound;
    private Music rainMusic;
    private OrthographicCamera camera;
    private SpriteBatch batch;
    private Rectangle bucket;
    private Array<Rectangle> raindrops;
    private long lastDropTime;          //上次生成雨滴的时间


    @Override
    public void create() {
        //加载水滴和水桶图片
        dropImage = new Texture(Gdx.files.internal("droplet.png"));
        bucketImage = new Texture(Gdx.files.internal("bucket.png"));
        //加载雨滴音效和背景音乐
        dropSound = Gdx.audio.newSound(Gdx.files.internal("drop.wav"));
        rainMusic = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));

        rainMusic.setLooping(true);     //循环播放
        rainMusic.play();               //播放

        camera = new OrthographicCamera();
        camera.setToOrtho(false,800,480);
        batch = new SpriteBatch();

        bucket = new Rectangle();
        bucket.x = 800/2-64/2;          //初始位置正中-->屏幕一半,自身一半
        bucket.y = 20;
        bucket.width = 64;
        bucket.height = 64;

        //创建雨滴(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() {
        gl.glClearColor(0,0,0.2f,1);
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(bucketImage,bucket.x,bucket.y);
        for (Rectangle raindrop : raindrops){
            batch.draw(dropImage,raindrop.x,raindrop.y);
        }
        batch.end();

        if (Gdx.input.isTouched()){
            Vector3 touchPos = new Vector3();
            touchPos.set(input.getX(),input.getY(),0);
            camera.unproject(touchPos);
            bucket.x = touchPos.x -64/2;        //根据鼠标点击的位置更新桶的位置
        }
        //键盘方向键
        if (input.isKeyPressed(Input.Keys.LEFT)) bucket.x -=200 * Gdx.graphics.getDeltaTime();
        if (input.isKeyPressed(Input.Keys.RIGHT)) bucket.x +=200 * Gdx.graphics.getDeltaTime();

        //限制移动在窗口内
        if (bucket.x<0)bucket.x = 0;
        if (bucket.x>800-64)bucket.x = 800-64;

        if (TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();
        for (Iterator<Rectangle> iter = raindrops.iterator();iter.hasNext();){
            Rectangle rainDrop = iter.next();
            rainDrop.y -= 200 * Gdx.graphics.getDeltaTime();
            if (rainDrop.y+64<0) iter.remove();

            //当水桶接住雨滴时
            if (rainDrop.overlaps(bucket)){
                //即时播放音效
                dropSound.play();
                iter.remove();
            }
        }
    }

    @Override
    public void dispose() {
        dropImage.dispose();
        bucketImage.dispose();
        dropSound.dispose();
        rainMusic.dispose();
        batch.dispose();
    }
}

9.修改启动类
libGDX学习记录(三)接水滴
10.运行桌面端
libGDX学习记录(三)接水滴
11.完整源码

上一篇:Error: The 'decorators' plugin requires a 'decoratorsBeforeExport' option


下一篇:如何使用Android罗盘方向对准opengl相机?