Kotlin First App

Post #93 written by Khodok in Code

Content

Code for TimeFighter

Kotlin
 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
90
91
92
package ch.ceff.android.timefighter

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

class MainActivity : AppCompatActivity() {
    /// lateinit -> je m'en occuperai plus tard
    private lateinit var gameScoreTextView: TextView
    private lateinit var timeLeftTextView: TextView
    private lateinit var tapMeButton: Button

    private var score = 0

    /// Timer
    private var gameStarted = false /// Le jeu est démarré ?
    private lateinit var countDownTimer: CountDownTimer
    private var initialCountdown: Long = 60000 /// 60s
    private var countDownInterval: Long = 1000 /// 1s
    private var timeLeft = 60

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main) /// Inflate

        /// Connexion aux variables
        gameScoreTextView = findViewById(R.id.game_score_text_view)
        timeLeftTextView = findViewById(R.id.time_left_text_view)
        tapMeButton = findViewById(R.id.tap_me_button)

        /// Ajout d'un gestionnaire sur le bouton
        tapMeButton.setOnClickListener { incrementScore() }

        resetGame()
    }

    private fun incrementScore() {
        if (!gameStarted) {
            startGame()
        }

        score++

        /// val = value = constante dans cette partie de code
        val newScore = /* value --> constante */ getString(R.string.game_score_text_view, score)
        gameScoreTextView.text = newScore
    }

    private fun resetGame() {
        score = 0

        val initialScore = getString(R.string.game_score_text_view, score)
        gameScoreTextView.text = initialScore

        val initialTimeLeft = getString(R.string.time_left_text_view, 60)
        timeLeftTextView.text = initialTimeLeft

        /// Définition d'un objet anonyme : object : Constructeur()
        /// CountDownTimer on doit redéfinir onTick() et onFinish()
        countDownTimer = object : CountDownTimer(initialCountdown, countDownInterval) {

            /// Lors de chaque interval du compteur
            /// La méthode reçoit le temps résiduel
            override fun onTick(millisUntilFinished: Long) {
                timeLeft = millisUntilFinished.toInt() / 1000

                val timeLeftString = getString(R.string.time_left_text_view, timeLeft)
                timeLeftTextView.text = timeLeftString
            }

            /// Lorsque le timer arrive à 0
            override fun onFinish() {
                endGame()
            }
        }

        gameStarted = false
    }

    private fun startGame() {
        countDownTimer.start()
        gameStarted = true
    }

    private fun endGame() {
        Toast.makeText(this, getString(R.string.game_over_message, score), Toast.LENGTH_LONG).show()
        resetGame()
    }
}
Comments

Please Log in to leave a comment.

No comments yet.