affichage spinner + sharedfPreference (bug)

Enzo
Jolys Enzo 2 years ago
parent 46737d786c
commit c16d78bf2f

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

@ -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<Spinner>(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])
}
}
}
})*/
})
}
}

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

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

@ -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"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Difficulty"/>
android:text="@string/difficulty"/>
<Spinner
android:id="@+id/spinnerDifficulty"

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