Skip to content

Instantly share code, notes, and snippets.

@veiset veiset/MyGame.kt Secret
Last active May 26, 2020

Embed
What would you like to do?
Kotlin Advent Calender - Game
package org.veiset.game
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.backends.lwjgl.LwjglApplication
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
import ktx.app.KtxApplicationAdapter
import ktx.app.clearScreen
import ktx.graphics.use
fun main() {
val config = LwjglApplicationConfiguration().apply {
width = 1280
height = 720
}
LwjglApplication(MyGame(), config)
}
data class Santa(val position: Float)
data class ChristmasGift(
var height: Float = 720f,
val position: Float = (40..1200).random().toFloat()
)
class MyGame : KtxApplicationAdapter {
private lateinit var renderer: ShapeRenderer
private var player = Santa(40f)
private var gifts = emptyList<ChristmasGift>()
override fun create() {
renderer = ShapeRenderer()
}
override fun render() {
handleInput()
logic()
draw()
}
private fun handleInput() {
if (Gdx.input.isKeyPressed(Input.Keys.A)) {
player = Santa(player.position - 5f)
} else if (Gdx.input.isKeyPressed(Input.Keys.D)) {
player = Santa(player.position + 5f)
}
}
private fun logic() {
if (Math.random() > 0.95) {
gifts = gifts + ChristmasGift()
}
gifts.forEach {
it.height -= 1f
}
}
private fun draw() {
clearScreen(0f, 0f, 0f, 0f)
renderer.use(ShapeRenderer.ShapeType.Filled) {
renderer.color = Color.GREEN
gifts.forEach {
renderer.rect(it.position, it.height, 60f, 60f)
}
}
renderer.use(ShapeRenderer.ShapeType.Filled) {
renderer.color = Color.RED
renderer.rect(player.position, 80f, 80f, 80f)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.