rotation finis + music + avancement gyros

pull/9/head
Jolys Enzo 2 years ago
parent e8544b419a
commit 2951569cfd

@ -3,17 +3,14 @@ package but.androidstudio.tetris
import android.content.Context import android.content.Context
import android.hardware.Sensor import android.hardware.Sensor
import android.hardware.SensorManager import android.hardware.SensorManager
import android.media.MediaPlayer
import android.os.Bundle import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Button import android.widget.Button
import android.widget.TextView import android.widget.TextView
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import androidx.core.content.ContextCompat.getSystemService import androidx.core.content.ContextCompat.getSystemService
import modele.Game import modele.Game
import views.ViewsGame import views.ViewsGame
@ -42,11 +39,14 @@ class GameFragment : Fragment(){
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
// La vue se refresh quand on fait quelque chose dessus (changer un parametre )
viewGame = view.findViewById<ViewsGame>(R.id.tabGame) viewGame = view.findViewById<ViewsGame>(R.id.tabGame)
viewGame.nbCaseHauteur = heightGame viewGame.nbCaseHauteur = heightGame
viewGame.nbCaseLargeur = withGame 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 buttonRight:Button = view.findViewById(R.id.Button_Right)
val buttonLeft:Button = view.findViewById(R.id.Button_Left) val buttonLeft:Button = view.findViewById(R.id.Button_Left)
val buttonRotateRight:Button = view.findViewById(R.id.Button_Right_Rotation) val buttonRotateRight:Button = view.findViewById(R.id.Button_Right_Rotation)
@ -65,11 +65,11 @@ class GameFragment : Fragment(){
modeleGame.dashBoard.rotateShapeRight(modeleGame.currentShape) modeleGame.dashBoard.rotateShapeRight(modeleGame.currentShape)
} }
buttonRotateLeft.setOnClickListener { buttonRotateLeft.setOnClickListener {
//modeleGame.dashBoard.rotateShapeLeft(modeleGame.currentShape) modeleGame.dashBoard.rotateShapeLeft(modeleGame.currentShape)
modeleGame.dashBoard.moveDown(modeleGame.currentShape)
} }
mediaPlayer.start()
modeleGame.startGame() modeleGame.startGame()
} }
} }

@ -7,9 +7,7 @@ import kotlin.io.path.fileVisitor
class DashBoard(private val width: Int,private val height: Int,private val view: ViewsGame) { class DashBoard(private val width: Int,private val height: Int,private val view: ViewsGame) {
val gridOfGame = Array(this.height) { IntArray(this.width) } val gridOfGame = Array(this.height) { IntArray(this.width) }
// To set something to occupied // To set something to occupied
fun toOccupied(col: Int, row: Int, value: Int) { fun toOccupied(col: Int, row: Int, value: Int) {
gridOfGame[row][col] = value 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 // To check if an position is occupied
fun isOccupied(col: Int, row: Int): Boolean = gridOfGame[row][col] != 0 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) { for (col in 0 until this.width) {
if (gridOfGame[row][col] == 0) { if (gridOfGame[row][col] == 0) {
return false return false
@ -26,13 +24,13 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
return true return true
} }
fun clearLine(row: Int) { private fun clearLine(row: Int) {
for (col in 0 until this.width) { for (col in 0 until this.width) {
gridOfGame[row][col] = 0 gridOfGame[row][col] = 0
} }
} }
fun shiftDown(listeLine:MutableList<Int>) { private fun shiftDown(listeLine:MutableList<Int>) {
println("Shift Down") println("Shift Down")
for( index in listeLine){ 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 ( line in 0..3){
for ( column in 0..3){ for ( column in 0..3){
if ( (matrix[line][column] == 1) and (shape.typeShape.showShape[line][column] == 0 )){ if ( matrix[line][column] == 1){
if ( gridOfGame[shape.position.y+column][shape.position.x+line] != 0 ) { 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 return false
} }
} }
@ -186,6 +187,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
fun rotateShapeRight(shape: Shape){ fun rotateShapeRight(shape: Shape){
println("Shape action -> Rotation right ! ") println("Shape action -> Rotation right ! ")
deleteShape(shape) deleteShape(shape)
rotationShapePossible(shape,0) rotationShapePossible(shape,0)
writeShape(shape) writeShape(shape)
@ -194,6 +196,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
fun rotateShapeLeft(shape: Shape){ fun rotateShapeLeft(shape: Shape){
println("Shape action -> Rotation left ! ") println("Shape action -> Rotation left ! ")
deleteShape(shape) deleteShape(shape)
rotationShapePossible(shape,1) rotationShapePossible(shape,1)
writeShape(shape) 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{ suspend fun fallingShape(shape: Shape, difficulty: Difficulty):Boolean{
when(difficulty){ when(difficulty){
Difficulty.EASY -> delay(800) Difficulty.EASY -> delay(700)
Difficulty.MEDIUM -> delay(600) Difficulty.MEDIUM -> delay(500)
Difficulty.HARD -> delay(400) Difficulty.HARD -> delay(300)
} }
return moveDown(shape) return moveDown(shape)
} }

@ -31,26 +31,6 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V
fun startGame(){ 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)) currentShape = Shape(getNextShape(),Position(width/2,0))
dashBoard.addShape(currentShape) dashBoard.addShape(currentShape)
@ -58,6 +38,7 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V
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){
if(dashBoard.fallingShape(currentShape,difficulty)){ if(dashBoard.fallingShape(currentShape,difficulty)){

@ -1,8 +1,6 @@
package modele 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<Position>{ fun sharePositionRight():MutableList<Position>{
val sharePosition = mutableListOf<Position>() val sharePosition = mutableListOf<Position>()
for( line in 0..3){ for( line in 0..3){
@ -103,7 +101,8 @@ class Shape(val typeShape: TypeShape,var position: Position) {
} }
fun sharePositionRotationLeft(shape: Shape):Array<IntArray> { fun sharePositionRotationLeft(shape: Shape):Array<IntArray> {
var leftmostCol = 4 var leftMostCol = 4
var topMostLine = 4
// Pivoter la matrice // Pivoter la matrice
val rotatedMatrix = Array(4) { row -> val rotatedMatrix = Array(4) { row ->
IntArray(4) { col -> IntArray(4) { col ->
@ -113,23 +112,28 @@ class Shape(val typeShape: TypeShape,var position: Position) {
// Trouver l'index de la colonne la plus à gauche // Trouver l'index de la colonne la plus à gauche
for (row in 0 until 4) { for (row in 0 until 4) {
for (col in 0 until 4) { for (col in 0 until 4) {
if (rotatedMatrix[row][col] != 0 && col < leftmostCol) { if (rotatedMatrix[row][col] != 0 && col < leftMostCol) {
leftmostCol = col leftMostCol = col
}
if ( rotatedMatrix[row][col] != 0 && row < topMostLine){
topMostLine = row
} }
} }
} }
// Décaler chaque ligne de la matrice vers la gauche // Décaler chaque ligne de la matrice vers la gauche
for (row in 0 until 4) { for (col in 0 until 4) {
for (col in 0 until leftmostCol) { for (line in 0 until topMostLine) {
rotatedMatrix[row][col] = 0 rotatedMatrix[line][col] = 0
} }
for (col in leftmostCol until 4) { for (line in topMostLine until 4) {
rotatedMatrix[row][col - leftmostCol] = rotatedMatrix[row][col] rotatedMatrix[line - topMostLine][col] = rotatedMatrix[line][col]
if (col - leftmostCol != col) { if ( line - topMostLine != line){
rotatedMatrix[row][col] = 0 rotatedMatrix[line][col] = 0
} }
} }
} }
return rotatedMatrix return rotatedMatrix
} }
} }

@ -3,9 +3,8 @@ package modele
import java.lang.reflect.Type import java.lang.reflect.Type
import kotlin.random.Random import kotlin.random.Random
class TypeShape(val type: EnumTypeShape){ class TypeShape(private val type: EnumTypeShape){
private var currentIndex = 0
var showShape:Array<IntArray> = Array(4){ IntArray(4 ) { 0 } } var showShape:Array<IntArray> = Array(4){ IntArray(4 ) { 0 } }
var couleur:Int = 0 var couleur:Int = 0
@ -64,26 +63,4 @@ class TypeShape(val type: EnumTypeShape){
} }
} }
} }
/*
fun getPoints(): Array<Position>{
val positions = ArrayList<Position>()
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
}
*/
} }
Loading…
Cancel
Save