I’ve been working on a basic snake game wirh 3 level. First 2 of them are working completely fine, but the 3rd one is causing trouble. Even though all of the code except for one extra feature (viewModel’s extraWalls variable) is same, idk why this error is occuring only in this one.
Full project is at: https://github.com/tauqirnizami/SnakeGame
My viewModel for 3rd (difficult) level is:
class DifficultSnakeViewModel : ViewModel() {
/*Pair(length/y-coordinate, width/x-coordinate)*/
var coordinates = mutableStateListOf(Pair(14, 14), Pair(15, 14), Pair(16, 14))
private set
private var gameGoing by mutableStateOf(true)
private val eachExtraWallWidthPlusOne = 4
val extraWalls: List<Pair<Int, Int>> = listOf(Pair(2,3), Pair(3,4), Pair(4,5)) //xtra walls added to snake game's grid to increase difficulty. This is the one causing problem.
init {
foodCoordinates = Pair(10, 9)
score = 0L
directions =
mutableStateListOf(0) //Using a list instead of a single int to keep track when user changes directions too quickly while the viewModel is on delay
giantFoodCoordinates = null
giantFoodCounter = 1
viewModelScope.launch(Dispatchers.Main) {
while (gameGoing) {
delay(if (score < 333) 500 - (score * 1.5).toLong() else 0) //This controls the snake speed
coordinatesUpdation()
}
}
}
private suspend fun coordinatesUpdation() {
// This code was working fine with other 2 levels
}
private fun food(otherFood: Pair<Int, Int>?): Pair<Int, Int> {
//code to generate food coordinates. Working fine (at least in other 2 levels)
}
private fun walls(): List<Pair<Int, Int>> {
val walls = mutableListOf<Pair<Int, Int>>()
val uniquePositions = mutableSetOf<Pair<Int, Int>>()
while (uniquePositions.size < 9) {
var position: Pair<Int, Int>
do {
position = Pair((2 until gridLength).random(), (2 until gridWidth - eachExtraWallWidthPlusOne).random())
} while (coordinates.contains(position) || position == foodCoordinates || position == Pair(coordinates[0].first + 1, coordinates[0].second))
walls.add(position)
uniquePositions.add(position)
repeat(2) {
position = Pair(position.first, position.second + 1)
walls.add(position)
uniquePositions.add(position)
}
}
return uniquePositions.toList()
}
}
And my composable file is:
@Composable
fun DifficultDisplayGrid(
modifier: Modifier,
snakeViewModel: DifficultSnakeViewModel
) {
val colors = difficultGenerateColorGrid(coordinates = snakeViewModel.coordinates, extraWalls = snakeViewModel.extraWalls)
Column {
ColorGrid(colors = colors, modifier = modifier)
Text(text = "Score = $score")
}
}
@Composable
fun DifficultScreen(
modifier: Modifier = Modifier,
snakeViewModel: DifficultSnakeViewModel = DifficultSnakeViewModel()
) {
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = modifier
) {
DifficultDisplayGrid(modifier, snakeViewModel = snakeViewModel)
Row(
modifier = Modifier,
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Button(onClick = { directions.add(2) }) {
Icon(imageVector = Icons.Filled.KeyboardArrowLeft, contentDescription = "Left")
}
//other buttons
}
//other code
The problem which is occuring is that, in this (difficult) level, when I run the program, the walls (extraWalls variable) are the ones who keeps changing, whereas the snake’s coordinates remain the same. The coordinates updation function is exactly the same as in medium and easy level, which are both working completely fine.
Ps: I know the walls function isn’t perfect, and the walls might sometomes overlap the snake or food, but that’s a secondary concern.
I thiught the error might be because maybe the walls() is being called everytine when the variable is accessed, so i tried to store the value in a separate data class, assign the data class’s variable’s value to another variable and use that variable’s value to be retrieved by the composable. As expected, this too didn’t worked at all!