27
loading...
This website collects cookies to deliver better user experience
line
, draw
, or plot
commands. Others didn't. As the video chip just displayed some area of the computers' memory, they needed to manipulate bytes in that area. Which bits and bytes related to which pixel on screen varied from model to model. As the computing power was quite limited, doing so in BASIC took quite some time. This is illustrated in the following clip:const val COLUMNS = 40
const val ROWS = 14
createLevelData()
(see source code below) returns a list of characters. This is quite similar to the succession of bytes in the memory of home computers, which are accessed by the video chip to display something in a particular cell on screen. Whenever a particular byte changes, the new symbol is immediately visible. The list being returned by createLevelData()
represents a grid of 14 rows and 40 columns. Elements 0 to 39 belong to the first row, 40 to 79 to the second, and so on.private fun createLevelData(): SnapshotStateList<Char> {
val data = mutableStateListOf<Char>()
var rows = 0
level.split("\n").forEach {
if (it.length != COLUMNS)
throw RuntimeException("length of row $rows is not $COLUMNS")
data.addAll(it.toList())
rows += 1
}
if (rows != ROWS)
throw RuntimeException("number of rows is not $ROWS")
return data
}
createLevelData()
returns a SnapshotStateList
.LazyVerticalGrid(
modifier = Modifier.fillMaxSize(),
cells = GridCells.Fixed(COLUMNS)
) {
itemsIndexed(levelData, itemContent = { index, item ->
var background = Color.Transparent
val symbol = when (item) {
'#' -> BRICK
CHAR_GEM -> GEM
'O' -> ROCK
'.' -> {
background = Color(0xffc2b280)
SAND
}
'@' -> PLAYER
else -> 32
}
Text(
modifier = Modifier
.background(background)
.clickable {
movePlayerTo(levelData, index, gemsCollected)
},
text = symbol.unicodeToString()
)
})
}
LazyVerticalGrid()
. It receives the SnapshotStateList
through levelData
and iterates over it using itemsIndexed()
. Its elements are converted to a Unicode symbol and then passed to Text()
. When an element of the list changes (because of movement or game physics) the corresponding composable will be recomposed automatically.Char
s and convert its elements to Unicode strings. Using Unicode symbols and passing them to Text()
is perfect for a prototype version (as you can see in the screenshot above). But for a real game we might want to use our own images instead. As the movements of the player, gems and rocks are done through changing the list, changing the drawing algorithm is piece of cake.