Skip to content

Commit

Permalink
Update Kotlin and Compose versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ellenspertus committed Aug 14, 2022
1 parent 80ffa4f commit bb9afd0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
17 changes: 12 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.4.31"
id("org.jetbrains.compose") version "0.3.2"
kotlin("jvm") version "1.6.10"
id("org.jetbrains.compose") version "1.1.0"
}

group = "me.aivanov"
Expand All @@ -20,11 +20,18 @@ dependencies {
}

tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
kotlinOptions.jvmTarget = "17"
}

kotlin.sourceSets.all {
languageSettings.useExperimentalAnnotation("kotlin.RequiresOptIn")
subprojects {
val compilerArgs = listOf("-Xopt-in=kotlin.RequiresOptIn")
tasks.withType<KotlinCompile> {
kotlinOptions.freeCompilerArgs += compilerArgs
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompileCommon> {
kotlinOptions.freeCompilerArgs = compilerArgs
}
}

compose.desktop {
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/com/arkivanov/composnake/DefaultGame.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf

class DefaultGame : Game {
val WIDTH = 16
val HEIGHT = WIDTH

private val _board: MutableState<Board>
private val randomPointGenerator: RandomPointGenerator

init {
val width = 16
val height = 16

val cy = height / 2
val cy = HEIGHT / 2

val snake =
setOf(
Expand All @@ -25,8 +24,8 @@ class DefaultGame : Game {
)

val grid =
List(height) { y ->
List(width) { x ->
List(HEIGHT) { y ->
List(WIDTH) { x ->
Point(x = x, y = y)
}
}
Expand Down
44 changes: 32 additions & 12 deletions src/main/kotlin/com/arkivanov/composnake/Main.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.arkivanov.composnake

import androidx.compose.desktop.Window
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material.Checkbox
import androidx.compose.material.CheckboxDefaults
import androidx.compose.material.MaterialTheme
Expand All @@ -15,38 +12,52 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusModifier
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.focusTarget
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.WindowState
import androidx.compose.ui.window.application
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive

fun main() {
val rowWidth = 400.dp
val rowHeight = rowWidth

@OptIn(ExperimentalComposeUiApi::class)
fun main() = application {
val game: Game = DefaultGame()
val focusRequester = FocusRequester()
val delay = 2000L

Window(
size = IntSize(width = 500, height = 500),
state = WindowState(size = DpSize.Unspecified),
onCloseRequest = ::exitApplication,
title = "CompoSnake"
) {
MaterialTheme {
LaunchedEffect(Unit) {
focusRequester.requestFocus()
while (isActive) {
delay(200L)
delay(delay)
game.step()
}
}

Box(
modifier = Modifier
.focusRequester(focusRequester)
.focusModifier()
.focusTarget()
.onKeyEvent {
when (it.key) {
Key.DirectionLeft -> game.setDirection(Direction.LEFT).let { true }
Expand All @@ -58,8 +69,12 @@ fun main() {
}
)

Box(
modifier = Modifier.fillMaxSize(),
Box(modifier = Modifier
.width(rowWidth)
.height(rowHeight)
.focusRequester(focusRequester)
.focusTarget(),
// modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Board(game.board.value)
Expand All @@ -81,7 +96,7 @@ private fun Board(board: Board) {

Column {
board.grid.forEachFast { row ->
Row {
Row(Modifier.width(rowWidth).padding(horizontal = 8.dp)) {
row.forEachFast { cell ->
when (cell) {
board.food -> FoodCell()
Expand All @@ -94,9 +109,12 @@ private fun Board(board: Board) {
}
}

val cellModifier = Modifier.width(24.dp).height(24.dp).padding(10.dp)

@Composable
private fun FoodCell() {
RadioButton(
modifier = cellModifier,
selected = true,
onClick = {},
colors = RadioButtonDefaults.colors(selectedColor = MaterialTheme.colors.primary)
Expand All @@ -106,6 +124,7 @@ private fun FoodCell() {
@Composable
private fun EmptyCell() {
Checkbox(
modifier = cellModifier,
checked = false,
onCheckedChange = {}
)
Expand All @@ -115,6 +134,7 @@ private fun EmptyCell() {
private fun SnakeCell(isHead: Boolean) {
Checkbox(
checked = true,
modifier = cellModifier,
colors = CheckboxDefaults.colors(
checkedColor = if (isHead) MaterialTheme.colors.primary else MaterialTheme.colors.secondary
),
Expand Down

0 comments on commit bb9afd0

Please sign in to comment.