Skip to content

Instantly share code, notes, and snippets.

@GRizzi91
Created October 13, 2021 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GRizzi91/09998ebc59343b7286e6b4f1e92b25a8 to your computer and use it in GitHub Desktop.
Save GRizzi91/09998ebc59343b7286e6b4f1e92b25a8 to your computer and use it in GitHub Desktop.
val state : State<Array<IntArray>> = gameViewModel.state.collectAsState()
Canvas(
modifier = Modifier
.fillMaxHeight(0.8f)
.aspectRatio(0.5f)
.border(1.dp, Color.Black)
) {
val squareDim = size.height / state.value.size
state.value.forEachIndexed { y, it ->
it.forEachIndexed { x, value ->
if (value > 0) {
drawGameElement(
squareColor = when (value) {
1 -> Color.Red
2 -> Color.Blue
3 -> Color.Yellow
4 -> Color.Green
else -> Color.Red
},
borderColor = Color.White,
offset = Offset(
x = (x) * squareDim,
y = (y) * squareDim
),
size = Size(
width = squareDim,
height = squareDim
)
)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment