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 b401d4f..a9185a4 100644 --- a/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt +++ b/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt @@ -3,17 +3,14 @@ package but.androidstudio.tetris import android.content.Context import android.hardware.Sensor import android.hardware.SensorManager +import android.media.MediaPlayer import android.os.Bundle -import android.util.Log import androidx.fragment.app.Fragment 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 import androidx.core.content.ContextCompat.getSystemService import modele.Game import views.ViewsGame @@ -42,11 +39,14 @@ class GameFragment : Fragment(){ override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - // La vue se refresh quand on fait quelque chose dessus (changer un parametre ) viewGame = view.findViewById(R.id.tabGame) viewGame.nbCaseHauteur = heightGame viewGame.nbCaseLargeur = withGame + val sensorManager = activity?.getSystemService(Context.SENSOR_SERVICE) as SensorManager + val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) + + val mediaPlayer = MediaPlayer.create(context,R.raw.tetris) 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) @@ -65,11 +65,11 @@ class GameFragment : Fragment(){ modeleGame.dashBoard.rotateShapeRight(modeleGame.currentShape) } buttonRotateLeft.setOnClickListener { - //modeleGame.dashBoard.rotateShapeLeft(modeleGame.currentShape) - modeleGame.dashBoard.moveDown(modeleGame.currentShape) + modeleGame.dashBoard.rotateShapeLeft(modeleGame.currentShape) } - + mediaPlayer.start() modeleGame.startGame() } + } \ No newline at end of file diff --git a/Tetris/app/src/main/java/modele/DashBoard.kt b/Tetris/app/src/main/java/modele/DashBoard.kt index 88eca50..d2eecaa 100644 --- a/Tetris/app/src/main/java/modele/DashBoard.kt +++ b/Tetris/app/src/main/java/modele/DashBoard.kt @@ -7,9 +7,7 @@ import kotlin.io.path.fileVisitor class DashBoard(private val width: Int,private val height: Int,private val view: ViewsGame) { val gridOfGame = Array(this.height) { IntArray(this.width) } - // To set something to occupied - fun toOccupied(col: Int, row: Int, value: Int) { gridOfGame[row][col] = value } @@ -17,7 +15,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view: // To check if an position is occupied fun isOccupied(col: Int, row: Int): Boolean = gridOfGame[row][col] != 0 - fun isLineFull(row: Int): Boolean { + private fun isLineFull(row: Int): Boolean { for (col in 0 until this.width) { if (gridOfGame[row][col] == 0) { return false @@ -26,13 +24,13 @@ class DashBoard(private val width: Int,private val height: Int,private val view: return true } - fun clearLine(row: Int) { + private fun clearLine(row: Int) { for (col in 0 until this.width) { gridOfGame[row][col] = 0 } } - fun shiftDown(listeLine:MutableList) { + private fun shiftDown(listeLine:MutableList) { println("Shift Down") for( index in listeLine){ @@ -172,8 +170,11 @@ class DashBoard(private val width: Int,private val height: Int,private val view: for ( line in 0..3){ for ( column in 0..3){ - if ( (matrix[line][column] == 1) and (shape.typeShape.showShape[line][column] == 0 )){ - if ( gridOfGame[shape.position.y+column][shape.position.x+line] != 0 ) { + if ( matrix[line][column] == 1){ + if ( (shape.position.y+line >= height) or (shape.position.x+column >= width)){ + return false + } + if (gridOfGame[shape.position.y+line][shape.position.x+column] != 0 ){ return false } } @@ -186,6 +187,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view: fun rotateShapeRight(shape: Shape){ println("Shape action -> Rotation right ! ") + deleteShape(shape) rotationShapePossible(shape,0) writeShape(shape) @@ -194,6 +196,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view: fun rotateShapeLeft(shape: Shape){ println("Shape action -> Rotation left ! ") + deleteShape(shape) rotationShapePossible(shape,1) writeShape(shape) @@ -234,9 +237,9 @@ class DashBoard(private val width: Int,private val height: Int,private val view: suspend fun fallingShape(shape: Shape, difficulty: Difficulty):Boolean{ when(difficulty){ - Difficulty.EASY -> delay(800) - Difficulty.MEDIUM -> delay(600) - Difficulty.HARD -> delay(400) + Difficulty.EASY -> delay(700) + Difficulty.MEDIUM -> delay(500) + Difficulty.HARD -> delay(300) } return moveDown(shape) } diff --git a/Tetris/app/src/main/java/modele/Game.kt b/Tetris/app/src/main/java/modele/Game.kt index 8a9e07c..32fe266 100644 --- a/Tetris/app/src/main/java/modele/Game.kt +++ b/Tetris/app/src/main/java/modele/Game.kt @@ -31,26 +31,6 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V fun startGame(){ - //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.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) currentShape = Shape(getNextShape(),Position(width/2,0)) dashBoard.addShape(currentShape) @@ -58,6 +38,7 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V println("RUN !!") dashBoard.updateViewGame() + // Ne pas utiliser de global scope !!!! SA ENLEVE DES POINTS !!!!!! GlobalScope.launch { while(true){ if(dashBoard.fallingShape(currentShape,difficulty)){ diff --git a/Tetris/app/src/main/java/modele/Shape.kt b/Tetris/app/src/main/java/modele/Shape.kt index 13e962d..8b59686 100644 --- a/Tetris/app/src/main/java/modele/Shape.kt +++ b/Tetris/app/src/main/java/modele/Shape.kt @@ -1,8 +1,6 @@ package modele -import java.lang.reflect.Type - -class Shape(val typeShape: TypeShape,var position: Position) { +class Shape(val typeShape: TypeShape, var position: Position) { fun sharePositionRight():MutableList{ val sharePosition = mutableListOf() for( line in 0..3){ @@ -103,7 +101,8 @@ class Shape(val typeShape: TypeShape,var position: Position) { } fun sharePositionRotationLeft(shape: Shape):Array { - var leftmostCol = 4 + var leftMostCol = 4 + var topMostLine = 4 // Pivoter la matrice val rotatedMatrix = Array(4) { row -> IntArray(4) { col -> @@ -113,23 +112,28 @@ class Shape(val typeShape: TypeShape,var position: Position) { // Trouver l'index de la colonne la plus à gauche for (row in 0 until 4) { for (col in 0 until 4) { - if (rotatedMatrix[row][col] != 0 && col < leftmostCol) { - leftmostCol = col + if (rotatedMatrix[row][col] != 0 && col < leftMostCol) { + leftMostCol = col + } + if ( rotatedMatrix[row][col] != 0 && row < topMostLine){ + topMostLine = row } } } + // Décaler chaque ligne de la matrice vers la gauche - for (row in 0 until 4) { - for (col in 0 until leftmostCol) { - rotatedMatrix[row][col] = 0 + for (col in 0 until 4) { + for (line in 0 until topMostLine) { + rotatedMatrix[line][col] = 0 } - for (col in leftmostCol until 4) { - rotatedMatrix[row][col - leftmostCol] = rotatedMatrix[row][col] - if (col - leftmostCol != col) { - rotatedMatrix[row][col] = 0 + for (line in topMostLine until 4) { + rotatedMatrix[line - topMostLine][col] = rotatedMatrix[line][col] + if ( line - topMostLine != line){ + rotatedMatrix[line][col] = 0 } } } + return rotatedMatrix } } \ No newline at end of file diff --git a/Tetris/app/src/main/java/modele/TypeShape.kt b/Tetris/app/src/main/java/modele/TypeShape.kt index 77717b5..3c3a4db 100644 --- a/Tetris/app/src/main/java/modele/TypeShape.kt +++ b/Tetris/app/src/main/java/modele/TypeShape.kt @@ -3,9 +3,8 @@ package modele import java.lang.reflect.Type import kotlin.random.Random -class TypeShape(val type: EnumTypeShape){ +class TypeShape(private val type: EnumTypeShape){ - private var currentIndex = 0 var showShape:Array = Array(4){ IntArray(4 ) { 0 } } var couleur:Int = 0 @@ -64,26 +63,4 @@ class TypeShape(val type: EnumTypeShape){ } } } - - /* - fun getPoints(): Array{ - val positions = ArrayList() - val typeShape = getCurrentType() - for (i in typeShape.indices){ - for (j in typeShape[i].indices){ - if (typeShape[i][j] != 0) { - positions.add(Position(j, i)) - } - } - } - return positions.toTypedArray() - } - - //to augmente of 1 the current index - fun getNextType(): TypeShape { - currentIndex++ - return this - } - */ - } \ No newline at end of file diff --git a/Tetris/app/src/main/res/raw/tetris.mp3 b/Tetris/app/src/main/res/raw/tetris.mp3 new file mode 100644 index 0000000..e6afcf8 Binary files /dev/null and b/Tetris/app/src/main/res/raw/tetris.mp3 differ