Advertisement
Guest User

Untitled

a guest
May 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. package com.example.quizmatematyczny
  2.  
  3. import android.support.v7.app.AppCompatActivity
  4. import android.os.Bundle
  5. import CreateQuestion
  6. import android.content.Intent
  7. import android.widget.Button
  8. import android.widget.Chronometer
  9. import android.widget.ImageView
  10. import android.widget.TextView
  11. import kotlinx.android.synthetic.*
  12. import kotlinx.android.synthetic.main.activity_game.*
  13. import android.R.attr.name
  14.  
  15.  
  16.  
  17. class GameActivity : AppCompatActivity() {
  18.  
  19. var questionCreator: CreateQuestion? = null
  20. private lateinit var button1: Button
  21. private lateinit var button2: Button
  22. private lateinit var button3: Button
  23. private lateinit var button4: Button
  24. private lateinit var questionTextView: TextView
  25. private lateinit var chronometer: Chronometer
  26. var questionCount: Int = 6
  27. var correctAnswersCount = 0
  28. var incorrectAnswersCount = 0
  29. var countDownTime = 0
  30. // var myTimer: Timer = Timer()
  31.  
  32.  
  33. override fun onCreate(savedInstanceState: Bundle?) {
  34. super.onCreate(savedInstanceState)
  35. setContentView(R.layout.activity_game)
  36.  
  37.  
  38. button1 = findViewById<Button>(R.id.button1)
  39. button2 = findViewById<Button>(R.id.button2)
  40. button3 = findViewById<Button>(R.id.button3)
  41. button4 = findViewById<Button>(R.id.button4)
  42. questionTextView = findViewById<TextView>(R.id.questionTextView)
  43. chronometer = findViewById<Chronometer>(R.id.chronometer)
  44.  
  45. questionCreator = intent.getSerializableExtra("question") as CreateQuestion
  46.  
  47. this.generujPytanie()
  48. chronometer.start()
  49. }
  50.  
  51.  
  52. fun setButtonsText(wynik: Int) {
  53. if(questionCreator != null) {
  54. val answers = questionCreator!!.createAnswersForQuestion(wynik)
  55. val answer1StringCorrect = answers.poprawnyButton.toString()
  56. val answer2String = answers.button2.toString()
  57. val answer3String = answers.button3.toString()
  58. val answer4String = answers.button4.toString()
  59.  
  60. var buttonCorrectAnswer = (0 .. 3).random()
  61.  
  62. button1.setOnClickListener(null)
  63. button2.setOnClickListener(null)
  64. button3.setOnClickListener(null)
  65. button4.setOnClickListener(null)
  66.  
  67. when(buttonCorrectAnswer) {
  68. 0 -> {println("pierwszybutton\n")
  69. button1.setOnClickListener { correctAnswer() }
  70. button2.setOnClickListener { incorrectAnswer() }
  71. button3.setOnClickListener { incorrectAnswer() }
  72. button4.setOnClickListener { incorrectAnswer() }
  73. //
  74. button1.text = answer1StringCorrect
  75. button2.text = answer2String
  76. button3.text = answer3String
  77. button4.text = answer4String
  78. }
  79. 1 -> {print("2button\n")
  80. button1.setOnClickListener { incorrectAnswer() }
  81. button2.setOnClickListener { correctAnswer() }
  82. button3.setOnClickListener { incorrectAnswer() }
  83. button4.setOnClickListener { incorrectAnswer() }
  84. //
  85. button1.text = answer2String
  86. button2.text = answer1StringCorrect
  87. button3.text = answer3String
  88. button4.text = answer4String
  89. }
  90. 2 -> {print("trzecibutton\n")
  91. button1.setOnClickListener { incorrectAnswer() }
  92. button2.setOnClickListener { incorrectAnswer() }
  93. button3.setOnClickListener { correctAnswer() }
  94. button4.setOnClickListener { incorrectAnswer() }
  95. //
  96. button1.text = answer2String
  97. button2.text = answer3String
  98. button3.text = answer1StringCorrect
  99. button4.text = answer4String
  100. }
  101. 3 -> {print("4button\n")
  102.  
  103. button1.setOnClickListener { incorrectAnswer() }
  104. button2.setOnClickListener { incorrectAnswer() }
  105. button3.setOnClickListener { incorrectAnswer() }
  106. button4.setOnClickListener { correctAnswer() }
  107. //
  108. button1.text = answer2String
  109. button2.text = answer4String
  110. button3.text = answer3String
  111. button4.text = answer1StringCorrect
  112.  
  113.  
  114. }
  115. else -> {
  116. return
  117. }
  118. }
  119.  
  120.  
  121. }
  122. }
  123.  
  124. fun correctAnswer() {
  125. this.correctAnswersCount += 1
  126. if(this.correctAnswersCount < this.questionCount) {
  127. this.generujPytanie()
  128. } else {
  129. chronometer.stop()
  130. var intent = Intent(this, GameOverActivity::class.java)
  131. intent.putExtra("zmienna1", this.incorrectAnswersCount.toString())
  132. intent.putExtra("zmienna2", this.correctAnswersCount.toString())
  133. intent.putExtra("zmienna3", chronometer.text.toString())
  134. startActivity(intent)
  135. //timer invalidate
  136. //show view z wynikami: il. poprawnych odpowiedzi, blednych i czas
  137.  
  138. }
  139. }
  140. fun incorrectAnswer() {
  141. this.incorrectAnswersCount += 1
  142. when(this.incorrectAnswersCount) {
  143. 1 -> {
  144. print("Utrata serca")
  145. heart3.animate().alpha(0f).setDuration(0)
  146. generujPytanie()
  147. }
  148. 2 -> {
  149. print("Utrata 2 serca")
  150. heart2.animate().alpha(0f).setDuration(0)
  151. generujPytanie()
  152. }
  153. 3 -> {
  154. print("Utrata 3 serca")
  155. val intent = Intent(this, GameOverActivity::class.java)
  156. intent.putExtra("zmienna1", this.incorrectAnswersCount.toString())
  157. intent.putExtra("zmienna2", this.correctAnswersCount.toString())
  158. intent.putExtra("zmienna3", chronometer.text.toString())
  159. startActivity(intent)
  160. //heart1.animate().alpha(0f).setDuration(0) // Tbh tego nie trzeba bo od razu przeskakuje na okno wykniów.
  161. chronometer.stop()
  162. //Pokaz view z wynikami: il. poprawnych, blednych i czas
  163. }
  164. }
  165. }
  166.  
  167. public fun generujPytanie() {
  168. val tuple = questionCreator?.createQuestionForLevel()
  169. if (tuple != null) {
  170. questionTextView.text = tuple.second
  171. setButtonsText(tuple.first)
  172. }
  173. }
  174. }
  175.  
  176. //
  177. //
  178. //
  179. //
  180. //
  181. //
  182. // @objc func myUpdate() {
  183. // countDownTime += 1
  184. // DispatchQueue.main.async {
  185. // self.timerLabel.text = "Timer: \(self.countDownTime)"
  186. // }
  187. // }
  188. //
  189. // func startCountdown(){
  190. // self.myTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(myUpdate), userInfo: nil, repeats: true)
  191. // countDownTime = 0
  192. // }
  193. //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement