gyro + move down

Enzo
Jolys Enzo 2 years ago
parent 2951569cfd
commit 51e6b7f7b9

@ -1,7 +1,10 @@
package but.androidstudio.tetris
import android.annotation.SuppressLint
import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.media.MediaPlayer
import android.os.Bundle
@ -14,8 +17,10 @@ import android.widget.TextView
import androidx.core.content.ContextCompat.getSystemService
import modele.Game
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 withGame:Int = 7 // Column number
@ -23,6 +28,7 @@ class GameFragment : Fragment(){
private lateinit var modeleGame:Game
private lateinit var sensorManager: SensorManager
private var sensor: Sensor? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -36,6 +42,11 @@ class GameFragment : Fragment(){
return inflater.inflate(R.layout.fragment_game, container, false)
}
override fun onPause() {
super.onPause()
sensorManager.unregisterListener(this)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@ -43,14 +54,15 @@ class GameFragment : Fragment(){
viewGame.nbCaseHauteur = heightGame
viewGame.nbCaseLargeur = withGame
val sensorManager = activity?.getSystemService(Context.SENSOR_SERVICE) as SensorManager
val sensor: Sensor? = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)
sensorManager = activity?.getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)
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)
val buttonRotateRight:Button = view.findViewById(R.id.Button_Right_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)
modeleGame = Game(height = heightGame, width = withGame, viewGame = viewGame, points = points)
@ -67,9 +79,40 @@ class GameFragment : Fragment(){
buttonRotateLeft.setOnClickListener {
modeleGame.dashBoard.rotateShapeLeft(modeleGame.currentShape)
}
buttonDown.setOnClickListener {
modeleGame.dashBoard.moveDown(modeleGame.currentShape)
}
sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL)
mediaPlayer.start()
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,11 +1,12 @@
package modele
import android.util.Log
import kotlinx.coroutines.delay
import views.ViewsGame
import kotlin.io.path.fileVisitor
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
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>) {
Log.println(Log.DEBUG,"ActionDashborad","Shift Down !")
println("Shift Down")
for( index in listeLine){
println(index)
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{
println("Shape action -> Move Left ! ")
Log.println(Log.DEBUG,"ActionShape","Move Left !")
shape.position.decrementeX()
if (!moveLeftPossible(shape)){
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{
println("Shape action -> Move down ! ")
Log.println(Log.DEBUG,"ActionShape","Rotation down !")
shape.position.addY()
if (!moveDownPossible(shape)){
shape.position.decrementeY()
@ -186,7 +187,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
fun rotateShapeRight(shape: Shape){
println("Shape action -> Rotation right ! ")
Log.println(Log.DEBUG,"ActionShape","Rotation right !")
deleteShape(shape)
rotationShapePossible(shape,0)
@ -195,7 +196,7 @@ class DashBoard(private val width: Int,private val height: Int,private val view:
fun rotateShapeLeft(shape: Shape){
println("Shape action -> Rotation left ! ")
Log.println(Log.DEBUG,"ActionShape","Rotation left !")
deleteShape(shape)
rotationShapePossible(shape,1)

@ -1,6 +1,8 @@
package modele
import android.util.Log
import android.widget.TextView
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import views.ViewsGame
@ -30,15 +32,12 @@ class Game(private val width: Int,private val height: Int,private val viewGame:V
// The start game function
fun startGame() {
currentShape = Shape(getNextShape(),Position(width/2,0))
dashBoard.addShape(currentShape)
println("RUN !!")
dashBoard.updateViewGame()
// Ne pas utiliser de global scope !!!! SA ENLEVE DES POINTS !!!!!!
// Ne pas utiliser de global scope !!!!
GlobalScope.launch {
while(true){
if(dashBoard.fallingShape(currentShape,difficulty)){
@ -64,7 +63,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_height="100dp"
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
android:id="@+id/Button_Right_Rotation"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/rotationdroite"
android:layout_marginStart="75dp"/>
android:layout_marginStart="15dp"/>
</LinearLayout>

Loading…
Cancel
Save