Boucle de jeu, points

pull/9/head
Jolys Enzo 2 years ago
parent 7b52be7c56
commit ba3b2aaf60

@ -10,6 +10,7 @@ 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 kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -46,12 +47,14 @@ class GameFragment : Fragment(){
viewGame.nbCaseHauteur = heightGame viewGame.nbCaseHauteur = heightGame
viewGame.nbCaseLargeur = withGame viewGame.nbCaseLargeur = withGame
modeleGame = Game(height = heightGame, width = withGame, viewGame = viewGame)
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)
val buttonRotateLeft:Button = view.findViewById(R.id.Button_Left_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 { buttonRight.setOnClickListener {
modeleGame.dashBoard.moveRight(modeleGame.currentShape) modeleGame.dashBoard.moveRight(modeleGame.currentShape)
} }
@ -62,7 +65,8 @@ 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)
} }

@ -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() //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 var nbRowCleared: Int = 0
for (row in 0 until this.height) { for (row in 0 until this.height) {
if (isLineFull(row)) { if (isLineFull(row)) {
@ -57,6 +57,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
if (nbRowCleared != 0) { if (nbRowCleared != 0) {
shiftDown(nbRowCleared) shiftDown(nbRowCleared)
} }
return nbRowCleared
} }
fun addShape(shape: Shape): Boolean { 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{ private fun moveDownPossible(shape: Shape):Boolean{
val pos:MutableList<Position> = shape.sharePositionRight() val pos:MutableList<Position> = shape.sharePositionDown(shape)
for ( position in pos){ for ( position in pos){
if ( position.y+shape.position.y >= height){ 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() view.invalidate()
} }
suspend fun fallingShape(shape: Shape){ suspend fun fallingShape(shape: Shape):Boolean{
delay(800) delay(800)
moveDown(shape) return moveDown(shape)
} }
} }

@ -1,12 +1,14 @@
package modele package modele
import android.annotation.SuppressLint
import android.util.Log import android.util.Log
import android.widget.TextView
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.cancel import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch 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,private val points:TextView) {
val dashBoard: DashBoard = DashBoard(width,height,viewGame) val dashBoard: DashBoard = DashBoard(width,height,viewGame)
lateinit var currentShape: Shape lateinit var currentShape: Shape
var difficulty: Difficulty = Difficulty.EASY 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 // The start game function
fun startGame(){ fun startGame(){
//this.setCurrentShape(TypeShape.SquareShape) val listeTypeShape: List<EnumTypeShape> = listOf(EnumTypeShape.ZShape,EnumTypeShape.JShape,EnumTypeShape.IShape,EnumTypeShape.LShape,EnumTypeShape.TShape,EnumTypeShape.SquareShape,EnumTypeShape.SShape)
currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1)) var indexListeTypeShape:Int = 0
dashBoard.addShape(currentShape)
//currentShape = Shape(TypeShape(EnumTypeShape.SShape),Position(1,1))
//dashBoard.addShape(currentShape)
currentShape = Shape(TypeShape(EnumTypeShape.IShape),Position(2,5)) //currentShape = Shape(TypeShape(EnumTypeShape.IShape),Position(2,5))
dashBoard.addShape(currentShape) //dashBoard.addShape(currentShape)
//currentShape = Shape(TypeShape(EnumTypeShape.SquareShape),Position(2,6)) //currentShape = Shape(TypeShape(EnumTypeShape.SquareShape),Position(2,6))
//dashBoard.addShape(currentShape) //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)) //currentShape = Shape(TypeShape(EnumTypeShape.JShape),Position(1,1))
//dashBoard.addShape(currentShape) //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 !!") println("RUN !!")
dashBoard.updateViewGame() dashBoard.updateViewGame()
GlobalScope.launch { GlobalScope.launch {
while(true){ while(true){
dashBoard.fallingShape(currentShape) 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++
}
} }
} }
} }

@ -51,7 +51,6 @@ class Shape(val typeShape: TypeShape,var position: Position) {
} }
} }
} }
return sharePosition return sharePosition
} }

@ -38,12 +38,32 @@
android:background="@drawable/flechegauche" android:background="@drawable/flechegauche"
android:layout_marginStart="15dp"/> android:layout_marginStart="15dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="50dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/points" />
<TextView
android:id="@+id/Id_Points"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/_0" />
</LinearLayout>
<Button <Button
android:id="@+id/Button_Right" android:id="@+id/Button_Right"
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="100dp" android:layout_height="100dp"
android:background="@drawable/flechedroite" android:background="@drawable/flechedroite"
android:layout_marginStart="150dp"/> android:layout_marginStart="50dp"/>
</LinearLayout> </LinearLayout>

@ -7,4 +7,6 @@
<string name="options">Options</string> <string name="options">Options</string>
<string name="best_score">Best Score :</string> <string name="best_score">Best Score :</string>
<string name="x">X</string> <string name="x">X</string>
<string name="points">Points :</string>
<string name="_0">0</string>
</resources> </resources>
Loading…
Cancel
Save