Tugas 11 - Pemrograman Perangkat Bergerak
Hesekiel Nainggolan
5025201054
PPB I
UNSCRAMBLE WORDS
Pada pertemuan kali ini, kita akan mencoba membuat aplikasi Unscramble Words. Aplikasi Unscramble adalah game pengacak ejaan kata untuk satu pemain. Aplikasi menampilkan kata acak, dan pemain harus menebak kata tersebut menggunakan semua huruf yang ditampilkan. Pemain akan mendapatkan poin jika kata tersebut benar. Jika tidak, pemain dapat mencoba menebak kata sebanyak-banyaknya. Aplikasi ini juga memiliki opsi untuk melewati kata saat ini. Di pojok kanan atas, aplikasi menampilkan jumlah kata, yaitu jumlah kata acak yang dimainkan dalam game saat ini. Ada 10 kata acak per game. Kita dapat menggunakan sumber code dari Github.
Panduan Kode
- WordsData.kt : File ini berisi daftar kata yang digunakan dalam game, konstanta untuk jumlah maksimum kata per game, dan jumlah poin skor pemain untuk setiap kata yang benar.
- MainActivity.kt : File ini sebagian besar berisi kode yang dihasilkan oleh template. Anda menampilkan composable GameScreen di blok setContent{}.
- GameScreen.kt : Semua composable UI ditentukan dalam file GameScreen.kt. Bagian berikut memberikan panduan tentang beberapa fungsi composable.
- GameStatus : adalah fungsi composable yang menampilkan skor game di bagian bawah layar. Fungsi composable berisi composable teks di Card. Untuk saat ini, skor di-hardcode menjadi 0.
- GameLayout : adalah fungsi composable yang menampilkan fungsi game utama, yang mencakup kata acak, petunjuk game, dan kolom teks yang menerima tebakan pengguna.
- GameScreen : Composable GameScreen berisi fungsi composable GameStatus dan GameLayout, judul game, jumlah kata, dan composable untuk tombol Submit dan Skip.
- FinalScoreDialog : Composable FinalScoreDialog menampilkan dialog, yaitu jendela kecil yang memberikan permintaan kepada pengguna, dengan opsi untuk Play Again atau Exit gameThis file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// No need to copy, this is included in the starter code. @Composable private fun FinalScoreDialog( score: Int, onPlayAgain: () -> Unit, modifier: Modifier = Modifier ) { val activity = (LocalContext.current as Activity) AlertDialog( onDismissRequest = { // Dismiss the dialog when the user clicks outside the dialog or on the back // button. If you want to disable that functionality, simply use an empty // onDismissRequest. }, title = { Text(text = stringResource(R.string.congratulations)) }, text = { Text(text = stringResource(R.string.you_scored, score)) }, modifier = modifier, dismissButton = { TextButton( onClick = { activity.finish() } ) { Text(text = stringResource(R.string.exit)) } }, confirmButton = { TextButton(onClick = onPlayAgain) { Text(text = stringResource(R.string.play_again)) } } ) }
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.unscramble.data | |
const val MAX_NO_OF_WORDS = 10 | |
const val SCORE_INCREASE = 20 | |
// Set with all the words for the Game | |
val allWords: Set<String> = | |
setOf( | |
"animal", | |
"auto", | |
"anecdote", | |
"alphabet", | |
"all", | |
"awesome", | |
"arise", | |
"balloon", | |
"basket", | |
"bench", | |
// ... | |
"zoology", | |
"zone", | |
"zeal" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// No need to copy, this is included in the starter code. | |
@Composable | |
fun GameStatus(score: Int, modifier: Modifier = Modifier) { | |
Card( | |
modifier = modifier | |
) { | |
Text( | |
text = stringResource(R.string.score, score), | |
style = typography.headlineMedium, | |
modifier = Modifier.padding(8.dp) | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// No need to copy, this is included in the starter code. | |
@Composable | |
fun GameLayout(modifier: Modifier = Modifier) { | |
val mediumPadding = dimensionResource(R.dimen.padding_medium) | |
Card( | |
modifier = modifier, | |
elevation = CardDefaults.cardElevation(defaultElevation = 5.dp) | |
) { | |
Column( | |
verticalArrangement = Arrangement.spacedBy(mediumPadding), | |
horizontalAlignment = Alignment.CenterHorizontally, | |
modifier = Modifier.padding(mediumPadding) | |
) { | |
Text( | |
modifier = Modifier | |
.clip(shapes.medium) | |
.background(colorScheme.surfaceTint) | |
.padding(horizontal = 10.dp, vertical = 4.dp) | |
.align(alignment = Alignment.End), | |
text = stringResource(R.string.word_count, 0), | |
style = typography.titleMedium, | |
color = colorScheme.onPrimary | |
) | |
Text( | |
text = "scrambleun", | |
style = typography.displayMedium | |
) | |
Text( | |
text = stringResource(R.string.instructions), | |
textAlign = TextAlign.Center, | |
style = typography.titleMedium | |
) | |
OutlinedTextField( | |
value = "", | |
singleLine = true, | |
shape = shapes.large, | |
modifier = Modifier.fillMaxWidth(), | |
colors = TextFieldDefaults.textFieldColors(containerColor = colorScheme.surface), | |
onValueChange = { }, | |
label = { Text(stringResource(R.string.enter_your_word)) }, | |
isError = false, | |
keyboardOptions = KeyboardOptions.Default.copy( | |
imeAction = ImeAction.Done | |
), | |
keyboardActions = KeyboardActions( | |
onDone = { } | |
) | |
) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun GameScreen() { | |
val mediumPadding = dimensionResource(R.dimen.padding_medium) | |
Column( | |
modifier = Modifier | |
.verticalScroll(rememberScrollState()) | |
.padding(mediumPadding), | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Text( | |
text = stringResource(R.string.app_name), | |
style = typography.titleLarge, | |
) | |
GameLayout( | |
modifier = Modifier | |
.fillMaxWidth() | |
.wrapContentHeight() | |
.padding(mediumPadding) | |
) | |
Column( | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding(mediumPadding), | |
verticalArrangement = Arrangement.spacedBy(mediumPadding), | |
horizontalAlignment = Alignment.CenterHorizontally | |
) { | |
Button( | |
modifier = Modifier.fillMaxWidth(), | |
onClick = { } | |
) { | |
Text( | |
text = stringResource(R.string.submit), | |
fontSize = 16.sp | |
) | |
} | |
OutlinedButton( | |
onClick = { }, | |
modifier = Modifier.fillMaxWidth() | |
) { | |
Text( | |
text = stringResource(R.string.skip), | |
fontSize = 16.sp | |
) | |
} | |
} | |
GameStatus(score = 0, modifier = Modifier.padding(20.dp)) | |
} | |
} |
Komentar
Posting Komentar