日々是好日~every day is a good day~

日常の中の非日常の備忘録

【Android Studio】Tach the number 3回目

今日は『カウントダウンして数字をタップできるようにする』です

import android.graphics.Color
import android.os.Bundle
import android.os.CountDownTimer
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        gameStart()
    }
    fun gameStart() {
        val btn1: Button = findViewById(R.id.btn1)
        val btn2: Button = findViewById(R.id.btn2)
        val btn3: Button = findViewById(R.id.btn3)
        val btn4: Button = findViewById(R.id.btn4)
        val btn5: Button = findViewById(R.id.btn5)
        val btn6: Button = findViewById(R.id.btn6)
        val btn7: Button = findViewById(R.id.btn7)
        val btn8: Button = findViewById(R.id.btn8)
        val btn9: Button = findViewById(R.id.btn9)
        val btn10: Button = findViewById(R.id.btn10)
        val btn11: Button = findViewById(R.id.btn11)
        val btn12: Button = findViewById(R.id.btn12)
        val btn13: Button = findViewById(R.id.btn13)
        val btn14: Button = findViewById(R.id.btn14)
        val btn15: Button = findViewById(R.id.btn15)
        val btn16: Button = findViewById(R.id.btn16)
        val btn17: Button = findViewById(R.id.btn17)
        val btn18: Button = findViewById(R.id.btn18)
        val btn19: Button = findViewById(R.id.btn19)
        val btn20: Button = findViewById(R.id.btn20)
        val btn21: Button = findViewById(R.id.btn21)
        val btn22: Button = findViewById(R.id.btn22)
        val btn23: Button = findViewById(R.id.btn23)
        val btn24: Button = findViewById(R.id.btn24)
        val btn25: Button = findViewById(R.id.btn25)
        val tvCD: TextView = findViewById(R.id.tvCD)

        var count = 0

        val btn = arrayOf(
            btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn10,
            btn11, btn12, btn13, btn14, btn15, btn16, btn17, btn18, btn19, btn20,
            btn21, btn22, btn23, btn24, btn25
        )
        val btnData = arrayOf(
            "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"
        )
        val list = listOf(
            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
            16, 17, 18, 19, 20, 21, 22, 23, 24
        )
        val num = list.shuffled()

        val startTime = 3000L

        tvCD.textSize = 48f
        tvCD.setTextColor(Color.BLACK)
        tvCD.text = "${startTime / 1000}"
        for (i in 0..24) {
            btn[i].text = btnData[num[i]]
            btn[i].isEnabled = false
        }
        //カウントダウンタイマーのオブジェクトを用意
        val timer = (object : CountDownTimer(startTime, 1000) {
            //途中経過・残り時間
            override fun onTick(p0: Long) {
                //残り時間を表示
                tvCD.text = "${(p0 + 1000) / 1000}" //秒単位
            }
            //ゲームスタート
            override fun onFinish() {
                tvCD.textSize = 24f
                tvCD.setTextColor(Color.RED)
                tvCD.text = "START"
                for (i in 0..24) {
                    btn[i].isEnabled = true
                }
            }
        })
        timer.start()

        for (i in 0..24) {
            btn[i].setOnClickListener {
                if (btn[i].text == btnData[count]) {
                    count++
                    btn[i].isEnabled = false
                    if (count == 25) {   //ゲームクリア
                        tvCD.text = "STOP"
                    }
                } else {
                    tvCD.text = "STOP"
                }
            }
        }
    }
}

カウントダウンタイマーのオブジェクトを用意して1秒ごとに表示
0になったらボタンをすべて有効にする
数字が順番に押されているかチェック
順番を間違えたか25までいったらSTOP

(再生できなくなっていたので2024.2.5に修正しました)
数字を押せるようになりました