2 Libgdx游戏开发——接水滴游戏实现( 三 )

效果如下所示:

2 Libgdx游戏开发——接水滴游戏实现

文章插图
接下来,我们需要实现雨滴的下落功能,这里,我们可以采用时间作为变量,随着时间的变长来改rainDrop对象的y坐标数值
override fun render() {batch.begin()batch.draw(dropImage, rainDrop.x, rainDrop.y)batch.end()rainDrop.y -= 200 * Gdx.graphics.deltaTime}效果如下:
2 Libgdx游戏开发——接水滴游戏实现

文章插图
可以看到,下落效果实现了,但是似乎出现了重复的东西,其实就是我们在开始的没清除掉上次绘制的图像,加上清除的代码即可:
override fun render() {//清除并设置屏幕背景色ScreenUtils.clear(0f, 0f, 0.2f, 1f)batch.begin()batch.draw(dropImage, rainDrop.x, rainDrop.y)batch.end()//每帧的时间,高度减少200rainDrop.y -= 200 * Gdx.graphics.deltaTime}
2 Libgdx游戏开发——接水滴游戏实现

文章插图
8.判断雨滴是否掉落在桶里这里,就是需要判断边界了,上面也说到,使用Rectangle矩形,就是方便我们判断是否水滴和桶接触了
Rectangle对象中,有个overlaps()方法,就是专门来判断两个矩形是否重叠了
//判断两个矩形的接触面积有重叠,即水滴掉落在桶里if (rainDrop.overlaps(bucket)) {//播放音效dropSound.play()}9.键盘控制改变桶位置上面的功能已经基本完成了,那么还差通过键盘来控制桶的位置就能实现了
我们判断是否按下方向键左或右来改变桶对应矩形的x坐标,这样就能改变桶的绘制位置了
override fun render() {//清除设置屏幕背景色ScreenUtils.clear(0f, 0f, 0.2f, 1f)batch.projectionMatrix = camera.combinedbatch.begin()batch.draw(bucketImage, bucket.x, bucket.y)batch.draw(dropImage, rainDrop.x, rainDrop.y)batch.end()rainDrop.y -= 200 * Gdx.graphics.deltaTime//键盘输入判断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//判断两个矩形的接触面积有重叠,即水滴掉落在桶里if (rainDrop.overlaps(bucket)) {//播放音效dropSound.play()//模拟消失(让水滴图片消失在游戏屏幕内)rainDrop.y = -64f}}效果如下图:
2 Libgdx游戏开发——接水滴游戏实现

文章插图
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();}}

经验总结扩展阅读