效果如下图:

文章插图
10.随机雨滴最后,上述完成的只是一个雨滴,那么,我们需要生成多个雨滴,可以使用一个List来存储雨滴的位置
- 当雨滴掉落在地上,将雨滴从列表中移除;
- 当雨滴被桶接到,雨滴也从列表中移除;
class CatchWater : ApplicationAdapter() {lateinit var dropImage: Texturelateinit var bucketImage: Texturelateinit var dropSound: Soundlateinit var rainMusic: Musiclateinit var camera: OrthographicCameralateinit var batch: SpriteBatch//最后雨滴下落时间var lastDropTime = TimeUtils.millis()val bucket = Rectangle().apply {//桶放中间x = (800 / 2 - 64 / 2).toFloat()y = 20.toFloat()width = 64.toFloat()height = 64.toFloat()}val rainDropList = arrayListOf<Rectangle>()override fun create() {// load the images for the droplet and the bucket, 64x64 pixels eachdropImage = Texture(Gdx.files.internal("drop.png"))bucketImage = 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"))// start the playback of the background music immediatelyrainMusic.setLooping(true)rainMusic.play()// create the camera and the SpriteBatchcamera = OrthographicCamera()camera.setToOrtho(false, 800f, 480f)batch = SpriteBatch()//开始先默认生成一个雨滴generateRainDrop()}private fun generateRainDrop() {val rainDrop = Rectangle().apply {//x坐标随机x = MathUtils.random(0, 800 - 64).toFloat()y = (480 - 64).toFloat()width = 64.toFloat()height = 64.toFloat()}rainDropList.add(rainDrop)}override fun render() {//清除设置屏幕背景色ScreenUtils.clear(0f, 0f, 0.2f, 1f)// tell the camera to update its matrices.camera.update()// tell the SpriteBatch to render in the// coordinate system specified by the camera.batch.projectionMatrix = camera.combinedbatch.begin()batch.draw(bucketImage, bucket.x, bucket.y)//绘制雨滴列表rainDropList.forEach {batch.draw(dropImage, it.x, it.y)}batch.end()// 触摸(手机端的操作和鼠标操作)if (Gdx.input.isTouched) {val touchPos = Vector3()touchPos[Gdx.input.x.toFloat(), Gdx.input.y.toFloat()] = 0fbucket.x = touchPos.x - 64 / 2camera.unproject(touchPos)}//键盘操作if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) bucket.x -= 200 * Gdx.graphics.deltaTimeif (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) bucket.x += 200 * Gdx.graphics.deltaTime//500ms生成一个雨滴if (TimeUtils.millis() - lastDropTime > 500) {generateRainDrop()lastDropTime = TimeUtils.millis()}val iter: MutableIterator<Rectangle> = rainDropList.iterator()while (iter.hasNext()) {val raindrop = iter.next()raindrop.y -= 200 * Gdx.graphics.deltaTime//如果雨滴掉落在地上if (raindrop.y + 64 < 0) iter.remove()//判断两个矩形的接触面积有重叠,即水滴掉落在桶里if (raindrop.overlaps(bucket)) {//播放音效dropSound.play()//将此雨滴移除列表iter.remove()}}}override fun dispose() {//资源释放dropImage.dispose();bucketImage.dispose();dropSound.dispose();rainMusic.dispose();batch.dispose();}}
上面有个涉及到手机和鼠标的操作,因为和Camera对象一起使用,还没过于研究,之后看看再深入一下吧打包关于打包的操作,可以通过Gradle的Task来进行操作
Android Studio4.2之后版本,把task给隐藏掉了,所以需要通过设置开启出来

文章插图
之后Gradle重构当前项目,右侧的Gradle就会出现Task列表了

文章插图
打包Android的和Android项目开发打包步骤一样的,这里不再赘述
如果是要打包的jar包,则可以点击右侧的Task任务,如下图所示:

文章插图
生成的jar文件,在
desktop\build\libs
目录下至于打包成exe,暂时还没研究,各位可以参考下这篇文章libGDX游戏开发之打包游戏(十二)_漫浅的博客-CSDN博客_libgdx开发的游戏
参考
- A Simple Game - libGDX
- Quillraven/SimpleKtxGame: The LibGDX simple game with Kotlin and LibKTX using an assetmanager, pool and viewport
- libGDX学习记录(三)接水滴_JS O_O的博客-CSDN博客
推荐阅读
- 五 Qt+ECharts开发笔记:ECharts的动态排序柱状图介绍、基础使用和Qt封装Demo
- Asp-Net-Core开发笔记:集成Hangfire实现异步任务队列和定时任务
- 我的世界网易游戏版龙蛋怎么孵化(我的世界网易版本孵龙蛋)
- 自定义View6 -塔防小游戏:第三篇防御塔随意放置+多组野怪
- 【疫情动态条形图】用Python开发全球疫情排名动态条形图bar_chart_race
- 全民飞行棋如何玩(飞行棋游戏怎么玩)
- 飞行棋玩法教程(飞行棋游戏情调版图片)
- 1 Libgdx游戏学习——环境配置及demo运行
- 军棋这个游戏怎么玩(军棋属于什么游戏)
- 驱动开发:通过Async反向与内核通信