Ajout chute auto des pièces avec usage de coroutines

pull/10/head
Alexis1663 2 years ago
parent 5027ae4dbf
commit dde1c6dad3

@ -1,11 +1,15 @@
package but.androidstudio.tetris package but.androidstudio.tetris
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 kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import modele.Game import modele.Game
import views.ViewsGame import views.ViewsGame

@ -1,5 +1,7 @@
package modele package modele
import android.util.Log
import kotlinx.coroutines.delay
import views.ViewsGame import views.ViewsGame
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) {
@ -196,4 +198,13 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
view.tableau = gridOfGame view.tableau = gridOfGame
view.invalidate() view.invalidate()
} }
suspend fun fallingShape(shape: Shape){
delay(800)
moveDown(shape)
}
fun rotateRight(shape: Shape){
}
} }

@ -1,6 +1,9 @@
package modele package modele
import android.view.InputQueue import android.util.Log
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import views.ViewsGame 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) {
@ -15,18 +18,6 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V
fun startGame(){ fun startGame(){
//this.setCurrentShape(TypeShape.SquareShape) //this.setCurrentShape(TypeShape.SquareShape)
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)) currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1))
dashBoard.addShape(currentShape) dashBoard.addShape(currentShape)
@ -34,10 +25,13 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V
dashBoard.gridOfGame[1][0] = 2 dashBoard.gridOfGame[1][0] = 2
dashBoard.gridOfGame[1][6] = 3 dashBoard.gridOfGame[1][6] = 3
//println(currentShape.position.getX())
//println(currentShape.position.getY())
println("RUN !!") println("RUN !!")
dashBoard.updateViewGame() dashBoard.updateViewGame()
}
GlobalScope.launch {
while (true){
dashBoard.fallingShape(currentShape)
}
}
}
} }

@ -3,13 +3,6 @@ package modele
import java.lang.reflect.Type import java.lang.reflect.Type
class Shape(val typeShape: TypeShape,var position: Position) { class Shape(val typeShape: TypeShape,var position: Position) {
//private var sharePosition = mutableListOf<Position>()
/* init {
val
}*/
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){
@ -61,17 +54,22 @@ class Shape(val typeShape: TypeShape,var position: Position) {
return sharePosition return sharePosition
} }
fun rotateLeft(){
//val tmp = this.position.getX()
//this.position.setX(this.position.getY() * -1)
//this.position.setY(tmp)
//update
}
fun rotateRight(){ fun sharePositionUp(shape: Shape): MutableList<Position>{
//val tmp = this.position.getX() val sharePosition = mutableListOf<Position>()
//this.position.setX(this.position.getY()) for( line in 0..3){
//this.position.setY(tmp * -1) for (column in 0..3) {
//update if ( line != 3 ){
if ( (typeShape.showShape[line][column] == 1) and (typeShape.showShape[line+1][column] == 0)){
sharePosition.add(Position(column,line))
}
}
if ( (line == 3) and (typeShape.showShape[line][column] == 1) ){
sharePosition.add(Position(column,line))
}
}
}
return sharePosition
} }
} }

@ -66,12 +66,9 @@ class ViewsGame(context:Context, attrs: AttributeSet?) : View(context, attrs) {
tableau[6][3] = 7 tableau[6][3] = 7
tableau[6][4] = 7 tableau[6][4] = 7
tableau[7][4] = 7 tableau[7][4] = 7
<<<<<<< HEAD
tableau[7][5] = 7*/ tableau[7][5] = 7*/
=======
tableau[7][5] = 7 tableau[7][5] = 7
*/ */
>>>>>>> Enzo
myPaint.color = Color.GRAY myPaint.color = Color.GRAY
canvas.drawRect(0F,0F,tailleTableauLargeur,tailleTableauHauteur,myPaint) canvas.drawRect(0F,0F,tailleTableauLargeur,tailleTableauHauteur,myPaint)

Loading…
Cancel
Save