Beat the high-score: build a game using libGDX and Kotlin

Beat the high-score: build a game using libGDX and Kotlin

Playing games is fun but making games is even better, especially with Kotlin and libGDX. Let's build together a breakout game and let's explore the gaming framework libGDX.

This session will show some libGDX's concepts: how to draw and animate elements of our game, how collision system works to destroy our bricks, what Kotlin brings to libGDX. Then we will dive into more advanced topics like shaders, in order to handle pixels from our images. Why are we doing all of this? To break the high score, of course!

Kyiv Kotlin Night (1th june 2019)

7843bb075c05be6886a97b77e36758ff?s=128

David

June 01, 2019
Tweet

Transcript

  1. 1.

    Build a game using libGDX and Kotlin Build a game

    using libGDX and Kotlin Build a game using libGDX and Kotlin Build a game using libGDX and Kotlin Beat the High score Beat the High score Build a game using libGDX and Kotlin
  2. 4.
  3. 6.
  4. 9.
  5. 13.
  6. 14.
  7. 16.
  8. 17.
  9. 18.
  10. 22.
  11. 23.

  12. 26.
  13. 27.
  14. 28.
  15. 30.

    dependencies { compile "com.badlogicgames.gdx:gdx:$gdxVersion" compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion" compile "com.badlogicgames.ashley:ashley:$ashleyVersion" compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

    compile "io.github.libktx:ktx-ashley:$ktxVersion" compile "io.github.libktx:ktx-graphics:$ktxVersion" compile "io.github.libktx:ktx-scene2d:$ktxVersion" compile "io.github.libktx:ktx-log:$ktxVersion" compile "com.github.dwursteisen.libgdx-addons:aseprite-addons:$libgdx_addons" compile "com.github.dwursteisen.libgdx-addons:ashley-addons:$libgdx_addons" compile "com.strongjoshua:libgdx-inGameConsole:$console" }
  16. 31.

    dependencies { compile "com.badlogicgames.gdx:gdx:$gdxVersion" compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion" compile "com.badlogicgames.ashley:ashley:$ashleyVersion" compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

    compile "io.github.libktx:ktx-ashley:$ktxVersion" compile "io.github.libktx:ktx-graphics:$ktxVersion" compile "io.github.libktx:ktx-scene2d:$ktxVersion" compile "io.github.libktx:ktx-log:$ktxVersion" compile "com.github.dwursteisen.libgdx-addons:aseprite-addons:$libgdx_addons" compile "com.github.dwursteisen.libgdx-addons:ashley-addons:$libgdx_addons" compile "com.strongjoshua:libgdx-inGameConsole:$console" }
  17. 32.

    dependencies { compile "com.badlogicgames.gdx:gdx:$gdxVersion" compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion" compile "com.badlogicgames.ashley:ashley:$ashleyVersion" compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

    compile "io.github.libktx:ktx-ashley:$ktxVersion" compile "io.github.libktx:ktx-graphics:$ktxVersion" compile "io.github.libktx:ktx-scene2d:$ktxVersion" compile "io.github.libktx:ktx-log:$ktxVersion" compile "com.github.dwursteisen.libgdx-addons:aseprite-addons:$libgdx_addons" compile "com.github.dwursteisen.libgdx-addons:ashley-addons:$libgdx_addons" compile "com.strongjoshua:libgdx-inGameConsole:$console" }
  18. 33.

    dependencies { compile "com.badlogicgames.gdx:gdx:$gdxVersion" compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion" compile "com.badlogicgames.ashley:ashley:$ashleyVersion" compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

    compile "io.github.libktx:ktx-ashley:$ktxVersion" compile "io.github.libktx:ktx-graphics:$ktxVersion" compile "io.github.libktx:ktx-scene2d:$ktxVersion" compile "io.github.libktx:ktx-log:$ktxVersion" compile "com.github.dwursteisen.libgdx-addons:aseprite-addons:$libgdx_addons" compile "com.github.dwursteisen.libgdx-addons:ashley-addons:$libgdx_addons" compile "com.strongjoshua:libgdx-inGameConsole:$console" }
  19. 34.

    dependencies { compile "com.badlogicgames.gdx:gdx:$gdxVersion" compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion" compile "com.badlogicgames.ashley:ashley:$ashleyVersion" compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

    compile "io.github.libktx:ktx-ashley:$ktxVersion" compile "io.github.libktx:ktx-graphics:$ktxVersion" compile "io.github.libktx:ktx-scene2d:$ktxVersion" compile "io.github.libktx:ktx-log:$ktxVersion" compile "com.github.dwursteisen.libgdx-addons:aseprite-addons:$libgdx_addons" compile "com.github.dwursteisen.libgdx-addons:ashley-addons:$libgdx_addons" compile "com.strongjoshua:libgdx-inGameConsole:$console" }
  20. 40.
  21. 41.

    class MyScreen : ScreenAdapter() { lateinit var batch: SpriteBatch override

    fun create() { batch = SpriteBatch() } override fun render() { batch.begin() batch.draw(image, x, y) batch.end() } }
  22. 42.

    class MyScreen : ScreenAdapter() { lateinit var batch: SpriteBatch override

    fun create() { batch = SpriteBatch() } override fun render() { batch.begin() batch.draw(image, x, y) batch.end() } }
  23. 43.

    class MyScreen : ScreenAdapter() { lateinit var batch: SpriteBatch override

    fun create() { batch = SpriteBatch() } override fun render() { batch.begin() batch.draw(image, x, y) batch.end() } }
  24. 44.

    class MyScreen : ScreenAdapter() { lateinit var batch: SpriteBatch override

    fun create() { batch = SpriteBatch() } override fun render() { batch.begin() batch.draw(image, x, y) batch.end() } }
  25. 45.

    class MyScreen : ScreenAdapter() { lateinit var batch: ShapeRenderer override

    fun create() { batch = ShapeRenderer() } override fun render() { batch.begin(ShapeRenderer.ShapeType.Filled) batch.color = Color.CHARTREUSE batch.circle(x, y, radius) batch.end() } }
  26. 46.

    class MyScreen : ScreenAdapter() { lateinit var batch: ShapeRenderer override

    fun create() { batch = ShapeRenderer() } override fun render() { batch.begin(ShapeRenderer.ShapeType.Filled) batch.color = Color.CHARTREUSE batch.circle(x, y, radius) batch.end() } }
  27. 47.

    class MyScreen : ScreenAdapter() { lateinit var batch: ShapeRenderer override

    fun create() { batch = ShapeRenderer() } override fun render() { batch.begin(ShapeRenderer.ShapeType.Filled) batch.color = Color.CHARTREUSE batch.circle(x, y, radius) batch.end() } }
  28. 48.

    class MyScreen : ScreenAdapter() { lateinit var batch: ShapeRenderer override

    fun create() { batch = ShapeRenderer() } override fun render() { batch.begin(ShapeRenderer.ShapeType.Filled) batch.color = Color.CHARTREUSE batch.circle(x, y, radius) batch.end() } }
  29. 49.

    class MyScreen : ScreenAdapter() { lateinit var batch: SpriteBatch override

    fun create() { batch = SpriteBatch() } override fun render() { batch.begin() batch.draw(image, x, y) batch.end() } }
  30. 50.

    class MyScreen : KtxScreen { val batch: SpriteBatch = SpriteBatch()

    override fun render() { batch.use { it.draw(sprite, x, y) } } } Inherit
  31. 51.
  32. 52.

    class MyScreen : ScreenAdapter() { lateinit var batch: ShapeRenderer private

    val position: Vector2 = Vector2(0f, 0f) override fun create() { batch = ShapeRenderer() } override fun render(delta: Float) { position.add(0f, -0.5f) batch.begin(ShapeRenderer.ShapeType.Filled) batch.color = Color.CHARTREUSE batch.circle(position.x, position.y, radius) batch.end() } }
  33. 53.

    class MyScreen : ScreenAdapter() { lateinit var batch: ShapeRenderer private

    val position: Vector2 = Vector2(0f, 0f) override fun create() { batch = ShapeRenderer() } override fun render(delta: Float) { position.add(0f, -0.5f) batch.begin(ShapeRenderer.ShapeType.Filled) batch.color = Color.CHARTREUSE batch.circle(position.x, position.y, radius) batch.end() } }
  34. 54.

    class MyScreen : ScreenAdapter() { lateinit var batch: ShapeRenderer private

    val position: Vector2 = Vector2(0f, 0f) override fun create() { batch = ShapeRenderer() } override fun render(delta: Float) { position.add(0f, -0.5f) batch.begin(ShapeRenderer.ShapeType.Filled) batch.color = Color.CHARTREUSE batch.circle(position.x, position.y, radius) batch.end() } }
  35. 55.

    class MyScreen : ScreenAdapter() { lateinit var batch: ShapeRenderer private

    val position: Vector2 = Vector2(0f, 0f) override fun create() { batch = ShapeRenderer() } override fun render(delta: Float) { position.add(0f, -0.5f) batch.begin(ShapeRenderer.ShapeType.Filled) batch.color = Color.CHARTREUSE batch.circle(position.x, position.y, radius) batch.end() } }
  36. 56.

    class MyScreen : ScreenAdapter() { lateinit var batch: ShapeRenderer private

    val position: Vector2 = Vector2(0f, 0f) private var time: Float = 0f override fun create() { batch = ShapeRenderer() } override fun render(delta: Float) { val (x, y) = position time += delta batch.begin(ShapeRenderer.ShapeType.Filled) batch.color = Color.CHARTREUSE batch.circle(x + MathUtils.cos(time), y, radius) batch.end() } }
  37. 57.

    class MyScreen : ScreenAdapter() { lateinit var batch: ShapeRenderer private

    val position: Vector2 = Vector2(0f, 0f) private var time: Float = 0f override fun create() { batch = ShapeRenderer() } override fun render(delta: Float) { val (x, y) = position time += delta batch.begin(ShapeRenderer.ShapeType.Filled) batch.color = Color.CHARTREUSE batch.circle(x + MathUtils.cos(time), y, radius) batch.end() } }
  38. 58.

    class MyScreen : ScreenAdapter() { lateinit var batch: ShapeRenderer private

    val position: Vector2 = Vector2(0f, 0f) private var time: Float = 0f override fun create() { batch = ShapeRenderer() } override fun render(delta: Float) { val (x, y) = position time += delta batch.begin(ShapeRenderer.ShapeType.Filled) batch.color = Color.CHARTREUSE batch.circle(x + MathUtils.cos(time), y, radius) batch.end() } }
  39. 59.

    class MyScreen : ScreenAdapter() { lateinit var batch: ShapeRenderer private

    val position: Vector2 = Vector2(0f, 0f) private var time: Float = 0f override fun create() { batch = ShapeRenderer() } override fun render(delta: Float) { val (x, y) = position time += delta batch.begin(ShapeRenderer.ShapeType.Filled) batch.color = Color.CHARTREUSE batch.circle(x + MathUtils.cos(time), y, radius) batch.end() } }
  40. 60.

    /** * Operator function that allows to deconstruct this vector.

    * @return X component. */ operator fun Vector2.component1(): Float = this.x /** * Operator function that allows to deconstruct this vector. * @return Y component. */ operator fun Vector2.component2(): Float = this.y val vector = Vector2(45f, 50f) val (x, y) = vector
  41. 61.

    /** * Operator function that allows to deconstruct this vector.

    * @return X component. */ operator fun Vector2.component1(): Float = this.x /** * Operator function that allows to deconstruct this vector. * @return Y component. */ operator fun Vector2.component2(): Float = this.y val (x, y) = Vector2(45f, 50f)
  42. 64.

    entity.add(Ball()) .add(Position(-100 v2 -100f)) .add(Size(8 v2 9)) .add(Rotation(4 v2 4))

    Create a 
 new Vector2 infix fun Number.v2(other: Number): Vector2 { return Vector2(this.toFloat(), other.toFloat()) }
  43. 66.
  44. 71.
  45. 72.

    val alpha = Math.abs(ball.x - raquet.x) val direction: Vector2 =

    Vector2(10f, 20f) direction.scl(1f, -1f) .scl(alpha) .nor() .rotate(306f) .add(10f, 20f)
  46. 73.

    val ball = Rectangle(68f, 64f, 5f, 5f) val player =

    Rectangle(64f, 64f, 100f, 10f) // the ball hit the player? player.overlaps(ball)
  47. 74.

    val ball = Rectangle(68f, 64f, 5f, 5f) val player =

    Rectangle(64f, 64f, 100f, 10f) // the ball hit the player? player.overlaps(ball)
  48. 75.

    val ball = Rectangle(68f, 64f, 5f, 5f) val player =

    Rectangle(64f, 64f, 100f, 10f) // the ball hit the player? player.overlaps(ball)
  49. 76.
  50. 77.
  51. 78.
  52. 79.
  53. 80.

    var world = World(Vector2(0, -10), true) // ... // Do

    in render method world.step(1/60f, 6, 2); val bodyDef = BodyDef() // Set its world position bodyDef.position.set(position.x, position.y) bodyDef.type = BodyDef.BodyType.DynamicBody bodyDef.angle = 0f val body = world.createBody(bodyDef) val shape = CircleShape() shape.radius = 8f // 2. Create a FixtureDef, as usual. val fd = FixtureDef() fd.density = 1f fd.friction = 2f fd.shape = shape body.createFixture(fd) body.userData = Any // attach sprite data shape.dispose()
  54. 81.

    var world = World(Vector2(0, -10), true) // ... // Do

    in render method world.step(1/60f, 6, 2); val bodyDef = BodyDef() // Set its world position bodyDef.position.set(position.x, position.y) bodyDef.type = BodyDef.BodyType.DynamicBody bodyDef.angle = 0f val body = world.createBody(bodyDef) val shape = CircleShape() shape.radius = 8f // 2. Create a FixtureDef, as usual. val fd = FixtureDef() fd.density = 1f fd.friction = 2f fd.shape = shape body.createFixture(fd) body.userData = Any // attach sprite data shape.dispose() Gravity
  55. 82.

    var world = World(Vector2(0, -10), true) // ... // Do

    in render method world.step(1/60f, 6, 2); val bodyDef = BodyDef() // Set its world position bodyDef.position.set(position.x, position.y) bodyDef.type = BodyDef.BodyType.DynamicBody bodyDef.angle = 0f val body = world.createBody(bodyDef) val shape = CircleShape() shape.radius = 8f // 2. Create a FixtureDef, as usual. val fd = FixtureDef() fd.density = 1f fd.friction = 2f fd.shape = shape body.createFixture(fd) body.userData = Any // attach sprite data shape.dispose()
  56. 83.

    var world = World(Vector2(0, -10), true) // ... // Do

    in render method world.step(1/60f, 6, 2); val bodyDef = BodyDef() // Set its world position bodyDef.position.set(position.x, position.y) bodyDef.type = BodyDef.BodyType.DynamicBody bodyDef.angle = 0f val body = world.createBody(bodyDef) val shape = CircleShape() shape.radius = 8f // 2. Create a FixtureDef, as usual. val fd = FixtureDef() fd.density = 1f fd.friction = 2f fd.shape = shape body.createFixture(fd) body.userData = Any // attach sprite data shape.dispose() Configuration of the « thing »
  57. 84.

    var world = World(Vector2(0, -10), true) // ... // Do

    in render method world.step(1/60f, 6, 2); val bodyDef = BodyDef() // Set its world position bodyDef.position.set(position.x, position.y) bodyDef.type = BodyDef.BodyType.DynamicBody bodyDef.angle = 0f val body = world.createBody(bodyDef) val shape = CircleShape() shape.radius = 8f // 2. Create a FixtureDef, as usual. val fd = FixtureDef() fd.density = 1f fd.friction = 2f fd.shape = shape body.createFixture(fd) body.userData = Any // attach sprite data shape.dispose()
  58. 85.

    var world = World(Vector2(0, -10), true) // ... // Do

    in render method world.step(1/60f, 6, 2); val bodyDef = BodyDef() // Set its world position bodyDef.position.set(position.x, position.y) bodyDef.type = BodyDef.BodyType.DynamicBody bodyDef.angle = 0f val body = world.createBody(bodyDef) val shape = CircleShape() shape.radius = 8f // 2. Create a FixtureDef, as usual. val fd = FixtureDef() fd.density = 1f fd.friction = 2f fd.shape = shape body.createFixture(fd) body.userData = Any // attach sprite data shape.dispose()
  59. 86.

    var world = World(Vector2(0, -10), true) // ... // Do

    in render method world.step(1/60f, 6, 2); val bodyDef = BodyDef() // Set its world position bodyDef.position.set(position.x, position.y) bodyDef.type = BodyDef.BodyType.DynamicBody bodyDef.angle = 0f val body = world.createBody(bodyDef) val shape = CircleShape() shape.radius = 8f // 2. Create a FixtureDef, as usual. val fd = FixtureDef() fd.density = 1f fd.friction = 2f fd.shape = shape body.createFixture(fd) body.userData = Any // attach sprite data shape.dispose()
  60. 89.
  61. 90.
  62. 91.
  63. 92.
  64. 95.

    open class MenuScreen(val game: Game) : ScreenAdapter() { override fun

    render(delta: Float) { // clear the screen Gdx.gl.glClearColor(0f, 0f, 0f, 1f) Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT) batch.begin() // ... batch.end() if (goToMenu) { game.setScreen(menuScreen) } } }
  65. 96.

    open class MenuScreen(val game: Game) : ScreenAdapter() { override fun

    render(delta: Float) { // clear the screen Gdx.gl.glClearColor(0f, 0f, 0f, 1f) Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT) batch.begin() // ... batch.end() if (goToMenu) { game.setScreen(menuScreen) } } } ⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠ game.setScreen(screen) 
 is not the same that
 game.screen = screen ⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠⚠
  66. 97.
  67. 98.
  68. 99.
  69. 100.

    class MyScreen : ScreenAdapter() { private lateinit var viewport: Viewport

    private lateinit var bath: SpriteBatch override fun show() { viewport = FitViewport(200f, 200f) bath = SpriteBath() } override fun render(delta: Float) { Gdx.gl.glClearColor(91f / 256f, 110f / 256f, 225f / 256f, 1f) Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT) batch.begin() batch.projectionMatrix = viewport.camera.combined // render batch.end() } override fun resize(width: Int, height: Int) { viewport.update(width, height) } }
  70. 101.

    class MyScreen : ScreenAdapter() { private lateinit var viewport: Viewport

    private lateinit var bath: SpriteBatch override fun show() { viewport = FitViewport(200f, 200f) bath = SpriteBath() } override fun render(delta: Float) { Gdx.gl.glClearColor(91f / 256f, 110f / 256f, 225f / 256f, 1f) Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT) batch.begin() batch.projectionMatrix = viewport.camera.combined // render batch.end() } override fun resize(width: Int, height: Int) { viewport.update(width, height) } } Size of the world you’ll display on the screen
  71. 102.

    class MyScreen : ScreenAdapter() { private lateinit var viewport: Viewport

    private lateinit var bath: SpriteBatch override fun show() { viewport = FitViewport(200f, 200f) bath = SpriteBath() } override fun render(delta: Float) { Gdx.gl.glClearColor(91f / 256f, 110f / 256f, 225f / 256f, 1f) Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT) batch.begin() batch.projectionMatrix = viewport.camera.combined // render batch.end() } override fun resize(width: Int, height: Int) { viewport.update(width, height) } }
  72. 103.
  73. 109.
  74. 110.
  75. 111.

    viewport.camera.position.add(0f, 40f, 0f) // Recalculates the projection and // view

    matrix of this camera // Use this after you've manipulated any of // the attributes of the camera. viewport.camera.update() viewport.camera.rotate(10f, 0f, 0f, 1f) viewport.camera.update()
  76. 112.

    viewport.camera.position.add(0f, 40f, 0f) // Recalculates the projection and // view

    matrix of this camera // Use this after you've manipulated any of // the attributes of the camera. viewport.camera.update() viewport.camera.rotate(10f, 0f, 0f, 1f) viewport.camera.update() Will move the camera up
  77. 113.

    viewport.camera.position.add(0f, 40f, 0f) // Recalculates the projection and // view

    matrix of this camera // Use this after you've manipulated any of // the attributes of the camera. viewport.camera.update() viewport.camera.rotate(10f, 0f, 0f, 1f) viewport.camera.update() Rotate the camera on the z Axis
  78. 115.
  79. 116.
  80. 117.

    // load texture val texture = Texture(Gdx.files.internal("texture.png")) val split =

    TextureRegion.split(texture, 128, 128) // select frames val frames = Array<TextureRegion>() frames.add(split[0][0]) frames.add(split[0][1]) frames.add(split[0][2]) frames.add(split[0][4]) // create animation val animation = Animation<TextureRegion>(100f, frames) // select frame to draw val keyToRender = animation.getKeyFrame(time)
  81. 118.

    // load texture val texture = Texture(Gdx.files.internal("texture.png")) val split =

    TextureRegion.split(texture, 128, 128) // select frames val frames = Array<TextureRegion>() frames.add(split[0][0]) frames.add(split[0][1]) frames.add(split[0][2]) frames.add(split[0][4]) // create animation val animation = Animation<TextureRegion>(100f, frames) // select frame to draw val keyToRender = animation.getKeyFrame(time)
  82. 119.

    // load texture val texture = Texture(Gdx.files.internal("texture.png")) val split =

    TextureRegion.split(texture, 128, 128) // select frames val frames = Array<TextureRegion>() frames.add(split[0][0]) frames.add(split[0][1]) frames.add(split[0][2]) frames.add(split[0][4]) // create animation val animation = Animation<TextureRegion>(100f, frames) // select frame to draw val keyToRender = animation.getKeyFrame(time)
  83. 120.

    // load texture val texture = Texture(Gdx.files.internal("texture.png")) val split =

    TextureRegion.split(texture, 128, 128) // select frames val frames = com.badlogic.gdx.utils.Array<TextureRegion>() frames.add(split[0][0]) frames.add(split[0][1]) frames.add(split[0][2]) frames.add(split[0][4]) // create animation val animation = Animation<TextureRegion>(100f, frames) // select frame to draw val keyToRender = animation.getKeyFrame(time) import com.badlogic.gdx.utils.Array as GdxArray
  84. 121.

    // load texture val texture = Texture(Gdx.files.internal("texture.png")) val split =

    TextureRegion.split(texture, 128, 128) // select frames val frames = GdxArray<TextureRegion>() frames.add(split[0][0]) frames.add(split[0][1]) frames.add(split[0][2]) frames.add(split[0][4]) // create animation val animation = Animation<TextureRegion>(100f, frames) // select frame to draw val keyToRender = animation.getKeyFrame(time)
  85. 122.

    // load texture val texture = Texture(Gdx.files.internal("texture.png")) val split =

    TextureRegion.split(texture, 128, 128) // select frames val frames = GdxArray<TextureRegion>() frames.add(split[0][0]) frames.add(split[0][1]) frames.add(split[0][2]) frames.add(split[0][4]) // create animation val animation = Animation<TextureRegion>(100f, frames) // select frame to draw val keyToRender = animation.getKeyFrame(time) Duration per frame
  86. 125.
  87. 126.

    val assetsManager = AssetManager() assetsManager.load("player.png", Texture::class.java) assetsManager.load("sfx/beat_intro.ogg", Music::class.java) assetsManager.load("krungthep2.fnt", BitmapFont::class.java)

    assetsManager.load("sheets/intro", Aseprite::class.java) // call it until it's loaded val isLoaded = assetsManager.update() val texture = assetsManager.get("player.png", Texture::class.java)
  88. 127.

    val assetsManager = AssetManager() assetsManager.load("player.png", Texture::class.java) assetsManager.load("sfx/beat_intro.ogg", Music::class.java) assetsManager.load("krungthep2.fnt", BitmapFont::class.java)

    assetsManager.load("sheets/intro", Aseprite::class.java) // call it until it's loaded val isLoaded = assetsManager.update() val texture = assetsManager.get("player.png", Texture::class.java)
  89. 128.

    val assetsManager = AssetManager() assetsManager.load("player.png", Texture::class.java) assetsManager.load("sfx/beat_intro.ogg", Music::class.java) assetsManager.load("krungthep2.fnt", BitmapFont::class.java)

    assetsManager.load("sheets/intro", Aseprite::class.java) // call it until it's loaded val isLoaded = assetsManager.update() val texture = assetsManager.get("player.png", Texture::class.java)
  90. 129.

    val assetsManager = AssetManager() assetsManager.load("player.png", Texture::class.java) assetsManager.load("sfx/beat_intro.ogg", Music::class.java) assetsManager.load("krungthep2.fnt", BitmapFont::class.java)

    assetsManager.load("sheets/intro", Aseprite::class.java) // call it until it's loaded val isLoaded = assetsManager.update() val texture = assetsManager.get("player.png", Texture::class.java)
  91. 130.

    val assetsManager = AssetManager() assetsManager.load("player.png", Texture::class.java) assetsManager.load("sfx/beat_intro.ogg", Music::class.java) assetsManager.load("krungthep2.fnt", BitmapFont::class.java)

    assetsManager.load("sheets/intro", Aseprite::class.java) // call it until it's loaded val isLoaded = assetsManager.update() val texture = assetsManager.get("player.png", Texture::class.java) Call it in the render method
  92. 131.

    val assetsManager = AssetManager() assetsManager.load("player.png", Texture::class.java) assetsManager.load("sfx/beat_intro.ogg", Music::class.java) assetsManager.load("krungthep2.fnt", BitmapFont::class.java)

    assetsManager.load("sheets/intro", Aseprite::class.java) // call it until it's loaded val isLoaded = assetsManager.update() val texture = assetsManager.get("player.png", Texture::class.java)
  93. 132.

    val assetsManager = AssetManager() assetsManager.load("player.png", Texture::class.java) assetsManager.load("sfx/beat_intro.ogg", Music::class.java) assetsManager.load("krungthep2.fnt", BitmapFont::class.java)

    assetsManager.load("sheets/intro", Aseprite::class.java) // call it until it's loaded val isLoaded = assetsManager.update() val texture: Texture = assetsManager["player.png"] inline operator fun <reified T> AssetManager.get(filename: String): T { return this.get(filename, T::class.java) }
  94. 134.

    val scene = verticalGroup { setFillParent(true) pad(10f) label("Hello Kotlin Night!")

    horizontalGroup { textButton("Click On Me") label("<-- here") } label("Another label...") selectBoxOf(GdxArray<String>().apply { add("value 1") add("value 2") add("value 3") }) } stage = Stage(FitViewport(screenWidth, screenHeight)) stage.addActor(scene) Hello Kotlin Night!
  95. 135.

    val scene = verticalGroup { setFillParent(true) pad(10f) label("Hello Kotlin Night!")

    horizontalGroup { textButton("Click On Me") label("<-- here") } label("Another label...") selectBoxOf(GdxArray<String>().apply { add("value 1") add("value 2") add("value 3") }) } stage = Stage(FitViewport(screenWidth, screenHeight)) stage.addActor(scene) Hello Kotlin Night!
  96. 136.

    val scene = verticalGroup { setFillParent(true) pad(10f) label("Hello Kotlin Night!")

    horizontalGroup { textButton("Click On Me") label("<-- here") } label("Another label...") selectBoxOf(GdxArray<String>().apply { add("value 1") add("value 2") add("value 3") }) } stage = Stage(FitViewport(screenWidth, screenHeight)) stage.addActor(scene) Hello Kotlin Night!
  97. 137.

    val scene = verticalGroup { setFillParent(true) pad(10f) label("Hello Kotlin Night!")

    horizontalGroup { textButton("Click On Me") label("<-- here") } label("Another label...") selectBoxOf(GdxArray<String>().apply { add("value 1") add("value 2") add("value 3") }) } stage = Stage(FitViewport(screenWidth, screenHeight)) stage.addActor(scene) Hello Kotlin Night!
  98. 141.

    ComponentMapper<Player> player = ComponentMapper.getFor(Player.class); ComponentMapper<Position> position = ComponentMapper.getFor(Position.class); ComponentMapper<Animation> animation

    = ComponentMapper.getFor(Animation.class); ComponentMapper<StateComponent> state = ComponentMapper.getFor(StateComponent.class); ... player.get(entity).life; position.get(entity).xy; animation.get(entity).getFrame(time); state.get(entity).time; Extractor Extraction
  99. 142.

    inline fun <reified T : Component> EntitySystem.get(): ComponentMapper<T> = 


    ComponentMapper.getFor(T::class.java) inline operator fun <reified T : Component> Entity.get(mapper: ComponentMapper<T>): T = 
 mapper.get(this)
  100. 143.

    val player: ComponentMapper<Player> = get() val position: ComponentMapper<Position> = get()

    val animation: ComponentMapper<Animation> = get() val state: ComponentMapper<StateComponent> = get() // access to components entity[player].life entity[position].xy entity[animation].frame(time) entity[state].time
  101. 144.

    val player = get<Player>() val position = get<Position>() val animation

    = get<Animation>() val state = get<StateComponent>() // access to components entity[player].life entity[position].xy entity[animation].frame(time) entity[state].time
  102. 145.

    val player = get<Player>() val position = get<Position>() val animation

    = get<Animation>() val state = get<StateComponent>() // access to components entity[player].life entity[position].xy entity[animation].frame(time) entity[state].time Accessing like a map
  103. 146.
  104. 147.

    fun on(event: Int, block: Transition): OnState { var currentTransitions =

    parent.transitions[state] ?: emptyMap() currentTransitions += event to block parent.transitions += state to currentTransitions return this } Lambda
  105. 148.

    startWith(WAIT) onState(WAIT).on(GameEvents.EVENT_COMPUTER_ALLOW_MOVE) { entity, event -> if (entity[dragon].currentTurn > 3)

    { entity[dragon].currentTurn = -1 go(FIRE, entity, event) } else { go(MOVE, entity, event) } } onState(FIRE).on(GameEvents.EVENT_DRAGON_FIRE_BALL) { entity, event -> go(MOVE, entity, event) } Lambda
  106. 149.

    startWith(WAIT) onState(WAIT).on(GameEvents.EVENT_COMPUTER_ALLOW_MOVE) { entity, event -> if (entity[dragon].currentTurn > 3)

    { entity[dragon].currentTurn = -1 go(FIRE, entity, event) } else { go(MOVE, entity, event) } } onState(FIRE).on(GameEvents.EVENT_DRAGON_FIRE_BALL) { entity, event -> go(MOVE, entity, event) } State Event Transition
  107. 150.
  108. 152.

    altLayers = map.layers .mapIndexed { index, layer -> index to

    layer } .filter { it.second.name.startsWith("alt_") } .map { it.first } .toIntArray() List 1 List 2 List 3 List 4 mutableList
  109. 153.

    val ball = Rectangle(68f, 64f, 5f, 5f) val player =

    Rectangle(64f, 64f, 100f, 10f) // the ball hit the player? player.overlaps(ball)
  110. 154.

    val pool = object : Pool<Rectangle>() { override fun newObject():

    Rectangle = Rectangle() } // ... val ball = pool.obtain().apply { set(68f, 64f, 5f, 5f) } val player = pool.obtain().apply { set(64f, 64f, 100f, 10f) } // the ball hit the player? player.overlaps(ball) pool.free(ball) pool.free(player)
  111. 155.

    val pool = object : Pool<Rectangle>() { override fun newObject():

    Rectangle = Rectangle() } // ... val ball = pool.obtain().apply { set(68f, 64f, 5f, 5f) } val player = pool.obtain().apply { set(64f, 64f, 100f, 10f) } // the ball hit the player? player.overlaps(ball) pool.free(ball) pool.free(player)
  112. 156.

    val pool = object : Pool<Rectangle>() { override fun newObject():

    Rectangle = Rectangle() } // ... val ball = pool.obtain().apply { set(68f, 64f, 5f, 5f) } val player = pool.obtain().apply { set(64f, 64f, 100f, 10f) } // the ball hit the player? player.overlaps(ball) pool.free(ball) pool.free(player)
  113. 157.

    val pool = object : Pool<Rectangle>() { override fun newObject():

    Rectangle = Rectangle() } // ... val ball = pool.obtain().apply { set(68f, 64f, 5f, 5f) } val player = pool.obtain().apply { set(64f, 64f, 100f, 10f) } // the ball hit the player? player.overlaps(ball) pool.free(ball) pool.free(player)
  114. 159.
  115. 160.
  116. 161.
  117. 162.
  118. 163.
  119. 164.

    // can be loaded using AssetManager too val tmxMap =

    TmxMapLoader().load(mapName) tmxMap.layers["bricks"]?.objects?.forEach { brick -> // create game entity } mapRenderer = OrthogonalTiledMapRenderer(tmxMap) // render method mapRenderer.setView(viewport.camera as OrthographicCamera) mapRenderer.render()
  120. 165.

    // can be loaded using AssetManager too val tmxMap =

    TmxMapLoader().load(mapName) tmxMap.layers["bricks"]?.objects?.forEach { brick -> // create game entity } mapRenderer = OrthogonalTiledMapRenderer(tmxMap) // render method mapRenderer.setView(viewport.camera as OrthographicCamera) mapRenderer.render()
  121. 166.

    // can be loaded using AssetManager too val tmxMap =

    TmxMapLoader().load(mapName) tmxMap.layers["bricks"]?.objects?.forEach { brick -> // create game entity } mapRenderer = OrthogonalTiledMapRenderer(tmxMap) // render method mapRenderer.setView(viewport.camera as OrthographicCamera) mapRenderer.render()
  122. 167.

    // can be loaded using AssetManager too val tmxMap =

    TmxMapLoader().load(mapName) tmxMap.layers["bricks"]?.objects?.forEach { brick -> // create game entity } mapRenderer = OrthogonalTiledMapRenderer(tmxMap) // render method mapRenderer.setView(viewport.camera as OrthographicCamera) mapRenderer.render()
  123. 168.
  124. 171.

    class BrickProperties(properties: MapProperties) { val x: Double by properties val

    y: Double by properties val hit: Int by properties val sound: String by properties } tmxMap.layers["bricks"].objects.forEach { brick -> val props = BrickProperties(brick.properties) val hit = props.hit }
  125. 172.

    class BrickProperties(properties: MapProperties) { val x: Double by properties val

    y: Double by properties val hit: Int by properties val sound: String by properties } tmxMap.layers["bricks"].objects.forEach { brick -> val props = BrickProperties(brick.properties) val hit = props.hit }
  126. 173.

    class BrickProperties(properties: MapProperties) { val x: Double by properties val

    y: Double by properties val hit: Int by properties val sound: String by properties } tmxMap.layers["bricks"].objects.forEach { brick -> val props = BrickProperties(brick.properties) val hit = props.hit } Delegates
  127. 174.

    inline operator fun <reified T> MapProperties.getValue(thisRef: Any?, property: KProperty<*>): T

    { val asStr = this[property.name].toString() return when (T::class) { Double::class -> asStr.toDouble() Int::class -> asStr.toInt() Boolean::class -> "true" == asStr String::class -> asStr else -> this[property.name] } as T }
  128. 177.

    open class AsepriteTask : DefaultTask() { // ... @TaskAction fun

    export() { val exec = getExecActionFactory().newExecAction() val exts = project.extensions.getByType(AsepritePluginExtentions::class.java) val aseprite = exts.exec ?: invalideAsepritePath() // ... } private fun invalideAsepritePath(): Nothing { TODO("""Missing aseprite executable path. Please configure it using aseprite.exec property (ie: in your ~/.gradle/gradle.properties) aseprite.exec=<path to exec> or using aseprite extension in your build.gradle aseprite { exec=<path to exec> } MacOS specific : point to aseprite located into <aseprite directory>/Aseprite.app/Contents/MacOS/aseprite""") } }
  129. 178.

    open class AsepriteTask : DefaultTask() { // ... @TaskAction fun

    export() { val exec = getExecActionFactory().newExecAction() val exts = project.extensions.getByType(AsepritePluginExtentions::class.java) val aseprite = exts.exec ?: invalideAsepritePath() // ... } private fun invalideAsepritePath(): Nothing { TODO("""Missing aseprite executable path. Please configure it using aseprite.exec property (ie: in your ~/.gradle/gradle.properties) aseprite.exec=<path to exec> or using aseprite extension in your build.gradle aseprite { exec=<path to exec> } MacOS specific : point to aseprite located into <aseprite directory>/Aseprite.app/Contents/MacOS/aseprite""") } } What happen if null? Encountered an error, lol!
  130. 179.

    open class AsepriteTask : DefaultTask() { // ... @TaskAction fun

    export() { val exec = getExecActionFactory().newExecAction() val exts = project.extensions.getByType(AsepritePluginExtentions::class.java) val aseprite = exts.exec ?: invalideAsepritePath() // ... } private fun invalideAsepritePath(): Nothing { TODO("""Missing aseprite executable path. Please configure it using aseprite.exec property (ie: in your ~/.gradle/gradle.properties) aseprite.exec=<path to exec> or using aseprite extension in your build.gradle aseprite { exec=<path to exec> } MacOS specific : point to aseprite located into <aseprite directory>/Aseprite.app/Contents/MacOS/aseprite""") } }
  131. 180.

    open class AsepriteTask : DefaultTask() { // ... @TaskAction fun

    export() { val exec = getExecActionFactory().newExecAction() val exts = project.extensions.getByType(AsepritePluginExtentions::class.java) val aseprite = exts.exec ?: invalideAsepritePath() // ... } private fun invalideAsepritePath(): Nothing { TODO("""Missing aseprite executable path. Please configure it using aseprite.exec property (ie: in your ~/.gradle/gradle.properties) aseprite.exec=<path to exec> or using aseprite extension in your build.gradle aseprite { exec=<path to exec> } MacOS specific : point to aseprite located into <aseprite directory>/Aseprite.app/Contents/MacOS/aseprite""") } }
  132. 182.

    void main() {
 vec4 sum = vec4(0);
 
 float step

    = iResolution.y;
 // y
 for(float i = -area ; i <= area ; i += 1.) {
 // x
 for(float j = -area ; j <= area ; j += 1.) {
 float x = v_texCoords.x + i / iResolution.x;
 float y = v_texCoords.y + j / iResolution.y;
 sum += texture2D(u_texture, vec2(x, y)) * 0.005;
 }
 }
 gl_FragColor = texture2D(u_texture, v_texCoords) + sum;
 } OpenGL Shading Language (GLSL)
  133. 183.
  134. 184.
  135. 185.
  136. 186.
  137. 187.
  138. 188.
  139. 189.
  140. 190.
  141. 191.
  142. 192.

    if (color_pixel < cutoff) { // light dark pixel =

    gl_FragColor = vec4(0.3, 0.2, 0.4., 1.0); } else { pixel = color_pixel; } 0 1 Cut off
  143. 193.

    if (color_pixel < cutoff) { // light dark pixel =

    gl_FragColor = vec4(0.3, 0.2, 0.4., 1.0); } else { pixel = color_pixel; }
  144. 194.
  145. 195.
  146. 196.
  147. 201.

    Beat The High Score Game https://github.com/dwursteisen/beat-the-high-score libGDX Lib https://libgdx.badlogicgames.com/ KTX

    
 (This is NOT Android KTX) Lib https://github.com/libktx/ktx/ libGDX addons Lib https://github.com/dwursteisen/libgdx-addons/ kTerminal Lib https://github.com/heatherhaks/kterminal Game Services Lib https://github.com/MrStahlfelge/gdx-gamesvcs In Game Console
 (A la quake) Lib https://github.com/StrongJoshua/libgdx-inGameConsole Gif Recorder Lib https://github.com/Anuken/GDXGifRecorder Tiled Editor Editor https://www.mapeditor.org Aseprite Editor https://www.aseprite.org