Skip to content

Instantly share code, notes, and snippets.

@mukeshsolanki
Created May 27, 2022 06:04
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 mukeshsolanki/ecbf1ee38543602ef0a6e276da8b00b3 to your computer and use it in GitHub Desktop.
Save mukeshsolanki/ecbf1ee38543602ef0a6e276da8b00b3 to your computer and use it in GitHub Desktop.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SnakeTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) { SnakeGame() }
}
}
}
@Composable
override fun SnakeGame() {
scope = rememberCoroutineScope()
val state = gameEngine.state.collectAsState(initial = null)
Column {
state.value?.let { Board(it) }
Controller {
when (it) {
SnakeDirection.Up -> gameEngine.move = Pair(0, -1)
SnakeDirection.Left -> gameEngine.move = Pair(-1, 0)
SnakeDirection.Right -> gameEngine.move = Pair(1, 0)
SnakeDirection.Down -> gameEngine.move = Pair(0, 1)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment