Compare commits

...

8 Commits

@ -1,7 +1,10 @@
package but.androidstudio.tetris package but.androidstudio.tetris
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.hardware.Sensor import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager import android.hardware.SensorManager
import android.media.MediaPlayer import android.media.MediaPlayer
import android.os.Bundle import android.os.Bundle
@ -12,10 +15,13 @@ import android.view.ViewGroup
import android.widget.Button import android.widget.Button
import android.widget.TextView import android.widget.TextView
import androidx.core.content.ContextCompat.getSystemService import androidx.core.content.ContextCompat.getSystemService
import modele.Difficulty
import modele.Game import modele.Game
import views.ViewsGame import views.ViewsGame
import kotlin.math.atan2
import kotlin.math.sqrt
class GameFragment : Fragment(){ class GameFragment : Fragment(), SensorEventListener{
private val heightGame:Int = 15 // Line number private val heightGame:Int = 15 // Line number
private val withGame:Int = 7 // Column number private val withGame:Int = 7 // Column number
@ -23,6 +29,7 @@ class GameFragment : Fragment(){
private lateinit var modeleGame:Game private lateinit var modeleGame:Game
private lateinit var sensorManager: SensorManager private lateinit var sensorManager: SensorManager
private var sensor: Sensor? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -36,6 +43,11 @@ class GameFragment : Fragment(){
return inflater.inflate(R.layout.fragment_game, container, false) return inflater.inflate(R.layout.fragment_game, container, false)
} }
override fun onPause() {
super.onPause()
sensorManager.unregisterListener(this)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
@ -43,17 +55,26 @@ class GameFragment : Fragment(){
viewGame.nbCaseHauteur = heightGame viewGame.nbCaseHauteur = heightGame
viewGame.nbCaseLargeur = withGame viewGame.nbCaseLargeur = withGame
val sensorManager = activity?.getSystemService(Context.SENSOR_SERVICE) as SensorManager sensorManager = activity?.getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)
val difficultyString = sharedPref?.getString(getString(R.string.spinnerValues),"Hard")
var difficulty:Difficulty = Difficulty.EASY
when(difficultyString){
"Medium" -> difficulty = Difficulty.MEDIUM
"Hard" -> difficulty = Difficulty.HARD
}
val mediaPlayer = MediaPlayer.create(context,R.raw.tetris) 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)
val buttonRotateLeft:Button = view.findViewById(R.id.Button_Left_Rotation) val buttonRotateLeft:Button = view.findViewById(R.id.Button_Left_Rotation)
val buttonDown:Button = view.findViewById(R.id.Button_Down)
val points:TextView = view.findViewById(R.id.Id_Points) val points:TextView = view.findViewById(R.id.Id_Points)
modeleGame = Game(height = heightGame, width = withGame, viewGame = viewGame, points = points) modeleGame = Game(height = heightGame, width = withGame, viewGame = viewGame, points = points, difficulty = difficulty)
buttonRight.setOnClickListener { buttonRight.setOnClickListener {
modeleGame.dashBoard.moveRight(modeleGame.currentShape) modeleGame.dashBoard.moveRight(modeleGame.currentShape)
@ -67,9 +88,40 @@ class GameFragment : Fragment(){
buttonRotateLeft.setOnClickListener { buttonRotateLeft.setOnClickListener {
modeleGame.dashBoard.rotateShapeLeft(modeleGame.currentShape) modeleGame.dashBoard.rotateShapeLeft(modeleGame.currentShape)
} }
buttonDown.setOnClickListener {
modeleGame.dashBoard.moveDown(modeleGame.currentShape)
}
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL)
mediaPlayer.start() mediaPlayer.start()
modeleGame.startGame() modeleGame.startGame()
// END GAME
//buttonLeft.setOnClickListener(null)
//buttonRight.setOnClickListener(null)
//buttonRotateLeft.setOnClickListener(null)
//buttonRotateRight.setOnClickListener(null)
//mediaPlayer.stop()
}
override fun onSensorChanged(event: SensorEvent?) {
if (event?.sensor?.type == Sensor.TYPE_ACCELEROMETER) {
val x = event.values[0] // Valeur de l'accélération le long de l'axe x
val y = event.values[1] // Valeur de l'accélération le long de l'axe y
val z = event.values[2] // Valeur de l'accélération le long de l'axe z
val inclination = atan2(x.toDouble(), sqrt((y * y + z * z).toDouble())) * (180 / Math.PI) // Calcule l'angle d'inclinaison sur l'axe x en degrés
if ( inclination > 25 ){
modeleGame.dashBoard.moveLeft(modeleGame.currentShape)
}
else if ( inclination < -25 ){
modeleGame.dashBoard.moveRight(modeleGame.currentShape)
}
}
}
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
} }
} }

@ -1,5 +1,8 @@
package but.androidstudio.tetris package but.androidstudio.tetris
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle import android.os.Bundle
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import android.view.LayoutInflater import android.view.LayoutInflater
@ -25,21 +28,35 @@ class OptionFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
/*
val spinnerDifficulty: Spinner = view.findViewById(R.id.spinnerDifficulty) val spinnerDifficulty:Spinner = view.findViewById(R.id.spinnerDifficulty)
val difficulty = arrayOf("Easy","Medium","Hard") val difficulty = arrayOf("Easy","Medium","Hard")
/*val adaptateurSpinnerDifficulty = ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,difficulty) val adaptateurSpinnerDifficulty = ArrayAdapter(requireContext(),android.R.layout.simple_spinner_dropdown_item,difficulty)
spinnerDifficulty.adapter = adaptateurSpinnerDifficulty adaptateurSpinnerDifficulty.setDropDownViewResource(android.R.layout.simple_spinner_item)
val monSpinner = requireView().findViewById<Spinner>(R.id.spinnerDifficulty)
monSpinner.adapter = adaptateurSpinnerDifficulty
spinnerDifficulty.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener { spinnerDifficulty.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) { override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
val selectedItem = parent.getItemAtPosition(position).toString() val selectedItem = parent.getItemAtPosition(position).toString()
// Faites quelque chose avec l'élément sélectionné
val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)
if (sharedPref != null) {
with(sharedPref.edit()){
putString("difficultyValue",selectedItem)
}
}
} }
override fun onNothingSelected(parent: AdapterView<*>) { override fun onNothingSelected(parent: AdapterView<*>) {
// Ne rien faire si aucun élément n'est sélectionné val sharedPref = activity?.getPreferences(Context.MODE_PRIVATE)
if (sharedPref != null) {
with(sharedPref.edit()){
putString("difficultyValue",difficulty[0])
}
}
} }
})*/ })*/

@ -1,11 +1,12 @@
package modele package modele
import android.util.Log
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import views.ViewsGame import views.ViewsGame
import kotlin.io.path.fileVisitor 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) } private 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) {
@ -31,8 +32,8 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
} }
private fun shiftDown(listeLine:MutableList<Int>) { private fun shiftDown(listeLine:MutableList<Int>) {
Log.println(Log.DEBUG,"ActionDashborad","Shift Down !")
println("Shift Down")
for( index in listeLine){ for( index in listeLine){
println(index) println(index)
for ( line in index downTo 1 ){ for ( line in index downTo 1 ){
@ -120,7 +121,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
} }
fun moveLeft(shape:Shape):Boolean{ fun moveLeft(shape:Shape):Boolean{
println("Shape action -> Move Left ! ") Log.println(Log.DEBUG,"ActionShape","Move Left !")
shape.position.decrementeX() shape.position.decrementeX()
if (!moveLeftPossible(shape)){ if (!moveLeftPossible(shape)){
shape.position.addX() shape.position.addX()
@ -148,7 +149,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
} }
fun moveDown(shape: Shape):Boolean{ fun moveDown(shape: Shape):Boolean{
println("Shape action -> Move down ! ") Log.println(Log.DEBUG,"ActionShape","Move down !")
shape.position.addY() shape.position.addY()
if (!moveDownPossible(shape)){ if (!moveDownPossible(shape)){
shape.position.decrementeY() shape.position.decrementeY()
@ -186,7 +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 ! ") Log.println(Log.DEBUG,"ActionShape","Rotation right !")
deleteShape(shape) deleteShape(shape)
rotationShapePossible(shape,0) rotationShapePossible(shape,0)
@ -195,7 +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 ! ") Log.println(Log.DEBUG,"ActionShape","Rotation left !")
deleteShape(shape) deleteShape(shape)
rotationShapePossible(shape,1) rotationShapePossible(shape,1)
@ -239,7 +240,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
when(difficulty){ when(difficulty){
Difficulty.EASY -> delay(700) Difficulty.EASY -> delay(700)
Difficulty.MEDIUM -> delay(500) Difficulty.MEDIUM -> delay(500)
Difficulty.HARD -> delay(300) Difficulty.HARD -> delay(200)
} }
return moveDown(shape) return moveDown(shape)
} }

@ -1,16 +1,14 @@
package modele package modele
import android.util.Log
import android.widget.TextView import android.widget.TextView
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.*
import kotlinx.coroutines.launch
import views.ViewsGame import views.ViewsGame
import kotlin.random.Random import kotlin.random.Random
class Game(private val width: Int,private val height: Int,private val viewGame:ViewsGame,private val points:TextView) { class Game(private val width: Int,private val height: Int,private val viewGame:ViewsGame,private val points:TextView,private val difficulty: Difficulty ) {
val dashBoard: DashBoard = DashBoard(width,height,viewGame) val dashBoard: DashBoard = DashBoard(width,height,viewGame)
lateinit var currentShape: Shape lateinit var currentShape: Shape
private var difficulty: Difficulty = Difficulty.EASY
//To get the next shape //To get the next shape
private fun getNextShape(): TypeShape { private fun getNextShape(): TypeShape {
@ -28,17 +26,13 @@ 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(){
currentShape = Shape(getNextShape(),Position(width/2,0)) currentShape = Shape(getNextShape(),Position(width/2,0))
dashBoard.addShape(currentShape) dashBoard.addShape(currentShape)
println("RUN !!")
dashBoard.updateViewGame() dashBoard.updateViewGame()
// Ne pas utiliser de global scope !!!! SA ENLEVE DES POINTS !!!!!! // Ne pas utiliser de global scope !!!!
GlobalScope.launch { GlobalScope.launch {
while(true){ while(true){
if(dashBoard.fallingShape(currentShape,difficulty)){ if(dashBoard.fallingShape(currentShape,difficulty)){
@ -64,7 +58,7 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V
} }
} }
} }
println("Game end !!") Log.println(Log.INFO,"Status","GAME END !!")
} }
} }
} }

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20,12l-1.41,-1.41L13,16.17V4h-2v12.17l-5.58,-5.59L4,12l8,8 8,-8z"/>
</vector>

@ -78,14 +78,21 @@
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="100dp" android:layout_height="100dp"
android:background="@drawable/rotationgauche" android:background="@drawable/rotationgauche"
android:layout_marginStart="50dp"/> android:layout_marginStart="25dp"/>
<Button
android:id="@+id/Button_Down"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/flechedown"
android:layout_marginStart="15dp"/>
<Button <Button
android:id="@+id/Button_Right_Rotation" android:id="@+id/Button_Right_Rotation"
android:layout_width="100dp" android:layout_width="100dp"
android:layout_height="100dp" android:layout_height="100dp"
android:background="@drawable/rotationdroite" android:background="@drawable/rotationdroite"
android:layout_marginStart="75dp"/> android:layout_marginStart="15dp"/>
</LinearLayout> </LinearLayout>

@ -10,13 +10,7 @@
android:layout_gravity="center" android:layout_gravity="center"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical">
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5">
<TextView <TextView
android:layout_width="match_parent" android:layout_width="match_parent"

@ -1,7 +1,6 @@
<resources> <resources>
<string name="app_name">Tetris</string> <string name="app_name">Tetris</string>
<string name="TextScore">Score : ??</string> <string name="TextScore">Score : ??</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string> <string name="hello_blank_fragment">Hello blank fragment</string>
<string name="start">Start</string> <string name="start">Start</string>
<string name="options">Options</string> <string name="options">Options</string>
@ -9,4 +8,7 @@
<string name="x">X</string> <string name="x">X</string>
<string name="points">Points :</string> <string name="points">Points :</string>
<string name="_0">0</string> <string name="_0">0</string>
<string name="retour">Retour</string>
<string name="difficulty">Difficulty</string>
<string name="spinnerValues">Hard</string>
</resources> </resources>
Loading…
Cancel
Save