Post Detail

Post #93

Kotlin First App

Code for TimeFighter

 1package ch.ceff.android.timefighter
 2
 3import androidx.appcompat.app.AppCompatActivity
 4import android.os.Bundle
 5import android.os.CountDownTimer
 6import android.widget.Button
 7import android.widget.TextView
 8import android.widget.Toast
 9
10class MainActivity : AppCompatActivity() {
11    /// lateinit -> je m'en occuperai plus tard
12    private lateinit var gameScoreTextView: TextView
13    private lateinit var timeLeftTextView: TextView
14    private lateinit var tapMeButton: Button
15
16    private var score = 0
17
18    /// Timer
19    private var gameStarted = false /// Le jeu est démarré ?
20    private lateinit var countDownTimer: CountDownTimer
21    private var initialCountdown: Long = 60000 /// 60s
22    private var countDownInterval: Long = 1000 /// 1s
23    private var timeLeft = 60
24
25    override fun onCreate(savedInstanceState: Bundle?) {
26        super.onCreate(savedInstanceState)
27        setContentView(R.layout.activity_main) /// Inflate
28
29        /// Connexion aux variables
30        gameScoreTextView = findViewById(R.id.game_score_text_view)
31        timeLeftTextView = findViewById(R.id.time_left_text_view)
32        tapMeButton = findViewById(R.id.tap_me_button)
33
34        /// Ajout d'un gestionnaire sur le bouton
35        tapMeButton.setOnClickListener { incrementScore() }
36
37        resetGame()
38    }
39
40    private fun incrementScore() {
41        if (!gameStarted) {
42            startGame()
43        }
44
45        score++
46
47        /// val = value = constante dans cette partie de code
48        val newScore = /* value --> constante */ getString(R.string.game_score_text_view, score)
49        gameScoreTextView.text = newScore
50    }
51
52    private fun resetGame() {
53        score = 0
54
55        val initialScore = getString(R.string.game_score_text_view, score)
56        gameScoreTextView.text = initialScore
57
58        val initialTimeLeft = getString(R.string.time_left_text_view, 60)
59        timeLeftTextView.text = initialTimeLeft
60
61        /// Définition d'un objet anonyme : object : Constructeur()
62        /// CountDownTimer on doit redéfinir onTick() et onFinish()
63        countDownTimer = object : CountDownTimer(initialCountdown, countDownInterval) {
64
65            /// Lors de chaque interval du compteur
66            /// La méthode reçoit le temps résiduel
67            override fun onTick(millisUntilFinished: Long) {
68                timeLeft = millisUntilFinished.toInt() / 1000
69
70                val timeLeftString = getString(R.string.time_left_text_view, timeLeft)
71                timeLeftTextView.text = timeLeftString
72            }
73
74            /// Lorsque le timer arrive à 0
75            override fun onFinish() {
76                endGame()
77            }
78        }
79
80        gameStarted = false
81    }
82
83    private fun startGame() {
84        countDownTimer.start()
85        gameStarted = true
86    }
87
88    private fun endGame() {
89        Toast.makeText(this, getString(R.string.game_over_message, score), Toast.LENGTH_LONG).show()
90        resetGame()
91    }
92}