Compare commits
7 Commits
b292ad92ed
...
85f3c8ce5e
Author | SHA1 | Date |
---|---|---|
![]() |
85f3c8ce5e | 2 years ago |
![]() |
ebbc88dd2e | 2 years ago |
|
9bcf269c5b | 2 years ago |
![]() |
2951569cfd | 2 years ago |
![]() |
e8544b419a | 2 years ago |
![]() |
ba3b2aaf60 | 2 years ago |
![]() |
7b52be7c56 | 2 years ago |
@ -1,54 +1,70 @@
|
|||||||
package modele
|
package modele
|
||||||
|
|
||||||
import android.util.Log
|
import android.widget.TextView
|
||||||
import kotlinx.coroutines.GlobalScope
|
import kotlinx.coroutines.GlobalScope
|
||||||
import kotlinx.coroutines.cancel
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import views.ViewsGame
|
import views.ViewsGame
|
||||||
|
import kotlin.random.Random
|
||||||
|
|
||||||
class Game(private val width: Int,private val height: Int,private val viewGame:ViewsGame) {
|
class Game(private val width: Int,private val height: Int,private val viewGame:ViewsGame,private val points:TextView) {
|
||||||
val dashBoard: DashBoard = DashBoard(width,height,viewGame)
|
val dashBoard: DashBoard = DashBoard(width,height,viewGame)
|
||||||
lateinit var currentShape: Shape
|
lateinit var currentShape: Shape
|
||||||
var difficulty: Difficulty = Difficulty.EASY
|
private var difficulty: Difficulty = Difficulty.EASY
|
||||||
|
|
||||||
|
|
||||||
|
//To get the next shape
|
||||||
|
private fun getNextShape(): TypeShape {
|
||||||
|
return when(Random.nextInt(1,7)){
|
||||||
|
1 -> TypeShape(EnumTypeShape.IShape)
|
||||||
|
2 -> TypeShape(EnumTypeShape.SquareShape)
|
||||||
|
3 -> TypeShape(EnumTypeShape.JShape)
|
||||||
|
4 -> TypeShape(EnumTypeShape.LShape)
|
||||||
|
5 -> TypeShape(EnumTypeShape.SShape)
|
||||||
|
6 -> TypeShape(EnumTypeShape.TShape)
|
||||||
|
7 -> TypeShape(EnumTypeShape.ZShape)
|
||||||
|
else -> throw Exception("Problème de random getNextShape()")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// The start game function
|
// The start game function
|
||||||
|
|
||||||
fun startGame(){
|
fun startGame(){
|
||||||
//this.setCurrentShape(TypeShape.SquareShape)
|
|
||||||
currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1))
|
|
||||||
dashBoard.addShape(currentShape)
|
|
||||||
|
|
||||||
|
|
||||||
currentShape = Shape(TypeShape(EnumTypeShape.IShape),Position(2,5))
|
currentShape = Shape(getNextShape(),Position(width/2,0))
|
||||||
dashBoard.addShape(currentShape)
|
dashBoard.addShape(currentShape)
|
||||||
|
|
||||||
//currentShape = Shape(TypeShape(EnumTypeShape.SquareShape),Position(2,6))
|
|
||||||
//dashBoard.addShape(currentShape)
|
|
||||||
|
|
||||||
//currentShape = Shape(TypeShape(EnumTypeShape.TShape),Position(2,8))
|
|
||||||
//dashBoard.addShape(currentShape)
|
|
||||||
|
|
||||||
//currentShape = Shape(TypeShape(EnumTypeShape.ZShape),Position(2,10))
|
|
||||||
//dashBoard.addShape(currentShape)
|
|
||||||
|
|
||||||
//currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1))
|
|
||||||
//dashBoard.addShape(currentShape)
|
|
||||||
|
|
||||||
//currentShape = Shape(TypeShape(EnumTypeShape.JShape),Position(1,1))
|
|
||||||
//dashBoard.addShape(currentShape)
|
|
||||||
|
|
||||||
//dashBoard.gridOfGame[2][0] = 5
|
|
||||||
|
|
||||||
println("RUN !!")
|
println("RUN !!")
|
||||||
dashBoard.updateViewGame()
|
dashBoard.updateViewGame()
|
||||||
|
|
||||||
|
// Ne pas utiliser de global scope !!!! SA ENLEVE DES POINTS !!!!!!
|
||||||
GlobalScope.launch {
|
GlobalScope.launch {
|
||||||
while (true){
|
while(true){
|
||||||
dashBoard.fallingShape(currentShape)
|
if(dashBoard.fallingShape(currentShape,difficulty)){
|
||||||
|
|
||||||
|
// Clear line
|
||||||
|
val nbLine = dashBoard.clearLines()
|
||||||
|
|
||||||
|
// Score
|
||||||
|
if ( (nbLine > 0) and (nbLine<4)){
|
||||||
|
val tmpPoints:String = points.text as String
|
||||||
|
points.text = (tmpPoints.toInt()+(nbLine*100)).toString()
|
||||||
|
}
|
||||||
|
if ( nbLine == 4 ){
|
||||||
|
val tmpPoints:String = points.text as String
|
||||||
|
points.text = (tmpPoints.toInt()+1200).toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// New shape
|
||||||
|
currentShape = Shape(getNextShape(),Position(width/2,0))
|
||||||
|
if ( !dashBoard.addShape(currentShape)){
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
println("Game end !!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Binary file not shown.
Loading…
Reference in new issue