我目前正在研究 book 中的示例。 “使用 LibGDX 进行 Java 游戏开发”,作者:Lee Stemkoski。我想尝试一下 Kotlin,并将第 2 章中的代码转换为 Kotlin。
我问以下代码是否是最佳实践,因为 Kotlin 代码似乎比 Java 代码更麻烦(而且没有“好”)。
package chapter2
import com.badlogic.gdx.Game
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.math.Rectangle
class StarfishCollectorAlpha : Game() {
private lateinit var batch: SpriteBatch
private var turtleTexture: Texture? = null
private var turtleX: Float? = null
private var turtleY: Float? = null
private lateinit var turtleRectangle: Rectangle
private var starfishTexture: Texture? = null
private var starfishX: Float? = null
private var starfishY: Float? = null
private lateinit var starfishRectangle: Rectangle
private var oceanTexture: Texture? = null
private var winMessageTexture: Texture? = null
private var win: Boolean? = null
override fun create() {
batch = SpriteBatch()
turtleTexture = Texture(Gdx.files.internal("assets/turtle-1.png"))
turtleX = 20f
turtleY = 20f
turtleRectangle = Rectangle(
turtleX!!,
turtleY!!,
turtleTexture!!.width.toFloat(),
turtleTexture!!.height.toFloat()
)
starfishTexture = Texture(Gdx.files.internal("assets/starfish.png"))
starfishX = 380f
starfishY = 380f
starfishRectangle = Rectangle(
starfishX!!,
starfishY!!,
starfishTexture!!.width.toFloat(),
starfishTexture!!.height.toFloat()
)
oceanTexture = Texture(Gdx.files.internal("assets/water.jpg"))
winMessageTexture = Texture(Gdx.files.internal("assets/you-win.png"))
win = false
}
override fun render() {
// check user input
if (Gdx.input.isKeyPressed(Input.Keys.LEFT))
turtleX = turtleX!! - 1
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT))
turtleX = turtleX!! + 1
if(Gdx.input.isKeyPressed(Input.Keys.UP))
turtleY = turtleY!! + 1
if(Gdx.input.isKeyPressed(Input.Keys.DOWN))
turtleY = turtleY!! - 1
// update turtle rectangle location
turtleRectangle.setPosition(turtleX!!, turtleY!!)
// checks win condition: Turtle must be overlapping starfish
if (turtleRectangle.overlaps(starfishRectangle))
win = true
// clear screen
Gdx.gl.glClearColor(0f, 0f, 0f, 1f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
// draw graphics
batch.begin()
batch.draw(oceanTexture, 0f, 0f)
if (!win!!)
batch.draw(starfishTexture, starfishX!!, starfishY!!)
batch.draw(turtleTexture, turtleX!!, turtleY!!)
if (win!!)
batch.draw(winMessageTexture, 180f, 180f)
batch.end()
}
}
首先:Java 版本仅用三个词定义了可空变量:
private Texture turtleTexture;
其次:您必须使用 Kotlin 显式地将整数转换为 float ,而 Java 会为您管理此操作。
第三:为了使用可为空的定义变量,您必须使用双感叹号!在最后,以及当您想要更改它们时。
我想使用 Kotlin 进行游戏开发 (LibGDX),但在我看来(也许我的知识有限)Java 是更好的语言。
请告诉我我的 Kotlin 代码很垃圾,有更好的方法来重构我的困惑。
最佳答案
对于 libgdx,通常的情况是要么所有变量都应该为 null,要么所有变量都应该被初始化,中间没有任何内容。因此,您可以在 Game 类中使用单个可为 null 的变量来委托(delegate)给没有可为 null 属性的上下文类。
这消除了大部分烦人的空检查。
至于需要在数字类型之间显式转换 - 对我来说这是一个功能:D。我知道这可能会让人恼火。不过我会尝试一下,看看它是否适合你。
这是我刚刚出于自己的目的而制作的:
class GolGui : ApplicationAdapter() {
companion object {
private val SCREEN_HEIGHT = 480
private val SCREEN_WIDTH = 800
private val SCREEN_CENTER_X = SCREEN_WIDTH / 2
private val SCREEN_CENTER_Y = SCREEN_HEIGHT / 2
}
private class Context : Disposable {
val batch = SpriteBatch();
val img = Texture("badlogic.jpg")
val gameSession = GameSession(GameBoard(12, 12))
val camera = OrthographicCamera(SCREEN_WIDTH.toFloat(), SCREEN_HEIGHT.toFloat())
val shapeRenderer = ShapeRenderer()
init {
shapeRenderer.projectionMatrix = camera.combined
}
fun render() {
Gdx.gl.glClearColor(0.2f, 0f, 0f, 1f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
shapeRenderer.apply {
begin(ShapeRenderer.ShapeType.Filled)
rect(200f, 200f, 200f, 200f)
end()
}
}
override fun dispose() {
batch.dispose()
img.dispose()
shapeRenderer.dispose()
}
}
private var context: Context? = null
override fun create() {
context = Context()
}
override fun render() {
context!!.render()
}
override fun dispose() {
context!!.dispose()
}
}
关于java - LibGDX:Kotlin 与 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53581936/