-
-
Save veiset/4f4e4dd59a95d6d12bc1a828b64955a1 to your computer and use it in GitHub Desktop.
Kotlin Advent Calender - Game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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