Repository navigation
Expand file tree
/
Copy pathMainActivity.kt
More file actions
89 lines (79 loc) · 2.73 KB
/
Copy pathMainActivity.kt
File metadata and controls
89 lines (79 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.example.tetris
import android.media.MediaPlayer
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.example.tetris.ui.theme.TetrisTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TetrisTheme {
Surface(
modifier = Modifier
.fillMaxSize()
.background(Color.Black)
) {
TetrisApp()
}
}
}
}
}
@Composable
fun TetrisApp() {
val context = LocalContext.current
val game = remember { TetrisGame() }
// 简单音效(把 drop.mp3 和 clear.mp3 放到 res/raw)
val dropSound = remember { MediaPlayer.create(context, R.raw.drop) }
val clearSound = remember { MediaPlayer.create(context, R.raw.clear) }
LaunchedEffect(game.isGameOver) {
if (game.isGameOver) {
dropSound.release()
clearSound.release()
}
}
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxSize().padding(top = 40.dp)
) {
TetrisBoard(
game = game,
onLineCleared = { clearSound.start() },
onDrop = { dropSound.start() }
)
Spacer(modifier = Modifier.height(32.dp))
Row(horizontalArrangement = Arrangement.spacedBy(20.dp)) {
ControlButton("←") { game.moveLeft() }
ControlButton("→") { game.moveRight() }
ControlButton("↷") { game.rotate() }
ControlButton("↓") { game.softDrop() }
ControlButton("💥") { game.hardDrop(); dropSound.start() }
}
Spacer(modifier = Modifier.height(20.dp))
androidx.compose.material3.Text(
text = "分数: ${game.score}",
color = Color.White,
style = MaterialTheme.typography.headlineMedium
)
}
}
@Composable
fun ControlButton(text: String, onClick: () -> Unit) {
androidx.compose.material3.Button(
onClick = onClick,
modifier = Modifier.size(64.dp)
) {
androidx.compose.material3.Text(text, style = MaterialTheme.typography.headlineMedium)
}
}