diff --git a/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt b/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt index 2be892e..b401d4f 100644 --- a/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt +++ b/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt @@ -10,6 +10,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Button +import android.widget.TextView import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -46,12 +47,14 @@ class GameFragment : Fragment(){ viewGame.nbCaseHauteur = heightGame viewGame.nbCaseLargeur = withGame - modeleGame = Game(height = heightGame, width = withGame, viewGame = viewGame) - val buttonRight:Button = view.findViewById(R.id.Button_Right) val buttonLeft:Button = view.findViewById(R.id.Button_Left) val buttonRotateRight:Button = view.findViewById(R.id.Button_Right_Rotation) val buttonRotateLeft:Button = view.findViewById(R.id.Button_Left_Rotation) + val points:TextView = view.findViewById(R.id.Id_Points) + + modeleGame = Game(height = heightGame, width = withGame, viewGame = viewGame, points = points) + buttonRight.setOnClickListener { modeleGame.dashBoard.moveRight(modeleGame.currentShape) } @@ -62,7 +65,8 @@ class GameFragment : Fragment(){ modeleGame.dashBoard.rotateShapeRight(modeleGame.currentShape) } buttonRotateLeft.setOnClickListener { - modeleGame.dashBoard.rotateShapeLeft(modeleGame.currentShape) + //modeleGame.dashBoard.rotateShapeLeft(modeleGame.currentShape) + modeleGame.dashBoard.moveDown(modeleGame.currentShape) } diff --git a/Tetris/app/src/main/java/modele/DashBoard.kt b/Tetris/app/src/main/java/modele/DashBoard.kt index 2025ba2..9bafb84 100644 --- a/Tetris/app/src/main/java/modele/DashBoard.kt +++ b/Tetris/app/src/main/java/modele/DashBoard.kt @@ -46,7 +46,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view: } //To check each grid line and remove if a line is full. Uses clearLine(), isLineFull() and shiftDown() - fun clearLines() { + fun clearLines():Int{ var nbRowCleared: Int = 0 for (row in 0 until this.height) { if (isLineFull(row)) { @@ -57,6 +57,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view: if (nbRowCleared != 0) { shiftDown(nbRowCleared) } + return nbRowCleared } fun addShape(shape: Shape): Boolean { @@ -139,7 +140,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view: } private fun moveDownPossible(shape: Shape):Boolean{ - val pos:MutableList = shape.sharePositionRight() + val pos:MutableList = shape.sharePositionDown(shape) for ( position in pos){ if ( position.y+shape.position.y >= height){ @@ -235,8 +236,8 @@ class DashBoard(private val width: Int,private val height: Int,private val view: view.invalidate() } - suspend fun fallingShape(shape: Shape){ + suspend fun fallingShape(shape: Shape):Boolean{ delay(800) - moveDown(shape) + return moveDown(shape) } } \ No newline at end of file diff --git a/Tetris/app/src/main/java/modele/Game.kt b/Tetris/app/src/main/java/modele/Game.kt index 32f566e..a98febb 100644 --- a/Tetris/app/src/main/java/modele/Game.kt +++ b/Tetris/app/src/main/java/modele/Game.kt @@ -1,12 +1,14 @@ package modele +import android.annotation.SuppressLint import android.util.Log +import android.widget.TextView import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.cancel import kotlinx.coroutines.launch import views.ViewsGame -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) lateinit var currentShape: Shape var difficulty: Difficulty = Difficulty.EASY @@ -17,13 +19,14 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V // The start game function fun startGame(){ - //this.setCurrentShape(TypeShape.SquareShape) - currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1)) - dashBoard.addShape(currentShape) + val listeTypeShape: List = listOf(EnumTypeShape.ZShape,EnumTypeShape.JShape,EnumTypeShape.IShape,EnumTypeShape.LShape,EnumTypeShape.TShape,EnumTypeShape.SquareShape,EnumTypeShape.SShape) + var indexListeTypeShape:Int = 0 + //currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1)) + //dashBoard.addShape(currentShape) - currentShape = Shape(TypeShape(EnumTypeShape.IShape),Position(2,5)) - dashBoard.addShape(currentShape) + //currentShape = Shape(TypeShape(EnumTypeShape.IShape),Position(2,5)) + //dashBoard.addShape(currentShape) //currentShape = Shape(TypeShape(EnumTypeShape.SquareShape),Position(2,6)) //dashBoard.addShape(currentShape) @@ -39,15 +42,42 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V //currentShape = Shape(TypeShape(EnumTypeShape.JShape),Position(1,1)) //dashBoard.addShape(currentShape) - - //dashBoard.gridOfGame[2][0] = 5 + /* + dashBoard.gridOfGame[0][0] = 5 + dashBoard.gridOfGame[0][1] = 5 + dashBoard.gridOfGame[0][2] = 5 + dashBoard.gridOfGame[0][3] = 5 + dashBoard.gridOfGame[0][4] = 5 + dashBoard.gridOfGame[0][5] = 5 + dashBoard.gridOfGame[0][6] = 5 + */ + currentShape = Shape(TypeShape(listeTypeShape[indexListeTypeShape]),Position(width/2,0)) + indexListeTypeShape++ + dashBoard.addShape(currentShape) println("RUN !!") dashBoard.updateViewGame() GlobalScope.launch { - while (true){ - dashBoard.fallingShape(currentShape) + while(true){ + if(! dashBoard.fallingShape(currentShape)){ + if ( indexListeTypeShape == 7 ){ + indexListeTypeShape = 0 + } + val nbLine = dashBoard.clearLines() + 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() + } + + currentShape = Shape(TypeShape(listeTypeShape[indexListeTypeShape]),Position(width/2,0)) + dashBoard.addShape(currentShape) + indexListeTypeShape++ + } } } } diff --git a/Tetris/app/src/main/java/modele/Shape.kt b/Tetris/app/src/main/java/modele/Shape.kt index e9c5a84..13e962d 100644 --- a/Tetris/app/src/main/java/modele/Shape.kt +++ b/Tetris/app/src/main/java/modele/Shape.kt @@ -51,7 +51,6 @@ class Shape(val typeShape: TypeShape,var position: Position) { } } } - return sharePosition } diff --git a/Tetris/app/src/main/res/layout/fragment_game.xml b/Tetris/app/src/main/res/layout/fragment_game.xml index 1efa397..e8e1c32 100644 --- a/Tetris/app/src/main/res/layout/fragment_game.xml +++ b/Tetris/app/src/main/res/layout/fragment_game.xml @@ -38,12 +38,32 @@ android:background="@drawable/flechegauche" android:layout_marginStart="15dp"/> + + + + + + + +