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.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<ViewsGame>(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()
}
}

@ -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<Int>) {
private fun shiftDown(listeLine:MutableList<Int>) {
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)
}

@ -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)){

@ -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<Position>{
val sharePosition = mutableListOf<Position>()
for( line in 0..3){
@ -103,7 +101,8 @@ class Shape(val typeShape: TypeShape,var position: Position) {
}
fun sharePositionRotationLeft(shape: Shape):Array<IntArray> {
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
}
}

@ -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<IntArray> = Array(4){ IntArray(4 ) { 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