From c16d78bf2f0cbf076e2e7a95ea9dd1bfbbbe6ad9 Mon Sep 17 00:00:00 2001 From: Jolys Enzo Date: Sun, 9 Apr 2023 18:41:03 +0200 Subject: [PATCH] affichage spinner + sharedfPreference (bug) --- .../but/androidstudio/tetris/GameFragment.kt | 11 ++++++- .../androidstudio/tetris/OptionFragment.kt | 29 +++++++++++++++---- Tetris/app/src/main/java/modele/DashBoard.kt | 4 +-- Tetris/app/src/main/java/modele/Game.kt | 11 ++----- .../src/main/res/layout/fragment_option.xml | 4 +-- Tetris/app/src/main/res/values/strings.xml | 4 ++- 6 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt b/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt index 3ad9a2b..56689b1 100644 --- a/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt +++ b/Tetris/app/src/main/java/but/androidstudio/tetris/GameFragment.kt @@ -15,6 +15,7 @@ import android.view.ViewGroup import android.widget.Button import android.widget.TextView import androidx.core.content.ContextCompat.getSystemService +import modele.Difficulty import modele.Game import views.ViewsGame import kotlin.math.atan2 @@ -57,6 +58,14 @@ class GameFragment : Fragment(), SensorEventListener{ sensorManager = activity?.getSystemService(Context.SENSOR_SERVICE) as SensorManager 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 buttonRight:Button = view.findViewById(R.id.Button_Right) val buttonLeft:Button = view.findViewById(R.id.Button_Left) @@ -65,7 +74,7 @@ class GameFragment : Fragment(), SensorEventListener{ val buttonDown:Button = view.findViewById(R.id.Button_Down) 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 { modeleGame.dashBoard.moveRight(modeleGame.currentShape) diff --git a/Tetris/app/src/main/java/but/androidstudio/tetris/OptionFragment.kt b/Tetris/app/src/main/java/but/androidstudio/tetris/OptionFragment.kt index d3558d0..4990b5c 100644 --- a/Tetris/app/src/main/java/but/androidstudio/tetris/OptionFragment.kt +++ b/Tetris/app/src/main/java/but/androidstudio/tetris/OptionFragment.kt @@ -1,5 +1,8 @@ package but.androidstudio.tetris + +import android.content.Context +import android.content.SharedPreferences import android.os.Bundle import androidx.fragment.app.Fragment import android.view.LayoutInflater @@ -25,21 +28,35 @@ class OptionFragment : Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 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 adaptateurSpinnerDifficulty = ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,difficulty) - spinnerDifficulty.adapter = adaptateurSpinnerDifficulty + val adaptateurSpinnerDifficulty = ArrayAdapter(requireContext(),android.R.layout.simple_spinner_dropdown_item,difficulty) + adaptateurSpinnerDifficulty.setDropDownViewResource(android.R.layout.simple_spinner_item) + + val monSpinner = requireView().findViewById(R.id.spinnerDifficulty) + monSpinner.adapter = adaptateurSpinnerDifficulty spinnerDifficulty.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener { override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) { 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<*>) { - // 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]) + } + } } - })*/ + }) } } \ No newline at end of file diff --git a/Tetris/app/src/main/java/modele/DashBoard.kt b/Tetris/app/src/main/java/modele/DashBoard.kt index b6e6b90..77bcefc 100644 --- a/Tetris/app/src/main/java/modele/DashBoard.kt +++ b/Tetris/app/src/main/java/modele/DashBoard.kt @@ -149,7 +149,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view: } fun moveDown(shape: Shape):Boolean{ - Log.println(Log.DEBUG,"ActionShape","Rotation down !") + Log.println(Log.DEBUG,"ActionShape","Move down !") shape.position.addY() if (!moveDownPossible(shape)){ shape.position.decrementeY() @@ -240,7 +240,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view: when(difficulty){ Difficulty.EASY -> delay(700) Difficulty.MEDIUM -> delay(500) - Difficulty.HARD -> delay(300) + Difficulty.HARD -> delay(200) } return moveDown(shape) } diff --git a/Tetris/app/src/main/java/modele/Game.kt b/Tetris/app/src/main/java/modele/Game.kt index be8034b..417b6d9 100644 --- a/Tetris/app/src/main/java/modele/Game.kt +++ b/Tetris/app/src/main/java/modele/Game.kt @@ -2,17 +2,13 @@ package modele import android.util.Log import android.widget.TextView -import kotlinx.coroutines.DelicateCoroutinesApi -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.launch +import kotlinx.coroutines.* import views.ViewsGame 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) lateinit var currentShape: Shape - private var difficulty: Difficulty = Difficulty.EASY - //To get the next shape private fun getNextShape(): TypeShape { @@ -30,8 +26,7 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V // The start game function - - fun startGame() { + fun startGame(){ currentShape = Shape(getNextShape(),Position(width/2,0)) dashBoard.addShape(currentShape) diff --git a/Tetris/app/src/main/res/layout/fragment_option.xml b/Tetris/app/src/main/res/layout/fragment_option.xml index 8b9a26e..e29f6ec 100644 --- a/Tetris/app/src/main/res/layout/fragment_option.xml +++ b/Tetris/app/src/main/res/layout/fragment_option.xml @@ -21,13 +21,13 @@ android:id="@+id/backButton" android:layout_width="match_parent" android:layout_height="wrap_content" - android:text="Retour" + android:text="@string/retour" /> + android:text="@string/difficulty"/> Tetris Score : ?? - Hello blank fragment Start Options @@ -9,4 +8,7 @@ X Points : 0 + Retour + Difficulty + Hard \ No newline at end of file