Android开发使用 libGDX接水滴功能
程序员文章站
2022-06-24 19:30:08
前期准备1.配置好android环境的IDEA2.下载libGDX项目构建工具—gdx-setup.jar生成项目1.双击上述gdx-setup.jar,2.选择最下方左边的Advanced按钮,添加一个镜像地址:https://maven.aliyun.com/nexus/content/groups/public/点击save。3.点击右侧Generate,点击弹出的选项是,再次点击弹出的选项是,开始创建项目。第一次可能会显示build failed,再执行一次Generate,即可...
前期准备
1.配置好android环境的IDEA
2.下载libGDX项目构建工具—gdx-setup.jar
生成项目
1.双击上述gdx-setup.jar,
2.选择最下方左边的Advanced按钮,添加一个镜像地址:
https://maven.aliyun.com/nexus/content/groups/public/
点击save。
3.点击右侧Generate,点击弹出的选项是,再次点击弹出的选项是,开始创建项目。第一次可能会显示build failed,再执行一次Generate,即可。
4.IDEA打开项目
5.导入图片音频资源
6.修改配置
7.更改图片名
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.修改启动类
10.运行桌面端
11.完整源码
改良版(有欢迎界面和计分器)
1.将原先的Drop.java注释掉,换成以下代码:
public class Drop extends Game{
public SpriteBatch batch;
public BitmapFont font;
public void create(){
batch = new SpriteBatch();
font = new BitmapFont();
this.setScreen(new MainMenuScreen(this));
}
public void render(){
super.render();
}
public void dispose() {
batch.dispose();
font.dispose();
}
}
2.另外创建MainMenuScreen.java,代码如下:
package com.mygdx.game;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import static com.badlogic.gdx.Gdx.gl;
import static com.badlogic.gdx.Gdx.input;
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 show() {
}
@Override
public void render(float delta) {
gl.glClearColor(0,0,0.2f,1);
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
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 (input.isTouched()){
game.setScreen(new GameScreen(game));
dispose();
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
3.再创建一个GameScreen.java,代码如下:
package com.mygdx.game;
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.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 static com.badlogic.gdx.Gdx.gl;
public class GameScreen implements Screen {
final Drop game;
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; //上次生成雨滴的时间
int dropsGathered; //收集的水滴数量
public GameScreen(final Drop game) {
this.game = game;
//加载水滴和水桶图片
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 show() {
//当前屏幕界面出现时,播放背景音乐
rainMusic.play();
}
@Override
public void render(float delta) {
gl.glClearColor(0,0,0.2f,1);
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.font.draw(game.batch,"Drops Collected: " + dropsGathered,0,480);
game.batch.draw(bucketImage,bucket.x,bucket.y);
for (Rectangle raindrop : raindrops){
game.batch.draw(dropImage,raindrop.x,raindrop.y);
}
game.batch.end();
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();
//限制移动在窗口内
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)){
dropsGathered++;
//即时播放音效
dropSound.play();
iter.remove();
}
}
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
dropImage.dispose();
bucketImage.dispose();
dropSound.dispose();
rainMusic.dispose();
batch.dispose();
}
}
4.效果如图:
5.完整源码
本文地址:https://blog.csdn.net/weixin_47450795/article/details/110228330
推荐阅读
-
Android开发使用Handler实现图片轮播功能示例
-
Android开发使用Handler实现图片轮播功能示例
-
Android开发使用Drawable绘制圆角与圆形图案功能示例
-
Android开发使用自定义view实现ListView下拉的视差特效功能
-
Android开发使用json实现服务器与客户端数据的交互功能示例
-
Android开发使用自定义view实现ListView下拉的视差特效功能
-
Android开发使用Drawable绘制圆角与圆形图案功能示例
-
Android开发之CheckBox的简单使用与监听功能示例
-
Android开发使用json实现服务器与客户端数据的交互功能示例
-
Android开发之TextView使用intent传递信息,实现注册界面功能示例