Skip to content
Snippets Groups Projects
Commit 54fc7f98 authored by izajime's avatar izajime
Browse files

Minijuego de husos funciona

parent 0506ad86
No related branches found
No related tags found
3 merge requests!65Feature:,!7Minijuego 1,!6Minijuego 1
......@@ -22,10 +22,11 @@
</activity>
<activity android:name=".RegisterActivity" android:exported="true"/>
<activity android:name=".MapActivity" android:exported="true"/>
<activity android:name=".Minijuego1" android:exported="true"/>
<activity android:name=".minijuego1.Minijuego1" android:exported="true"/>
<activity android:name=".Minijuego2" android:exported="true"/>
<activity android:name=".Minijuego3" android:exported="true"/>
<activity android:name=".Minijuego4" android:exported="true"/>
<activity android:name=".minijuego1.FinMinijuego1" android:exported="true"/>
</application>
......
......@@ -6,6 +6,7 @@ import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import androidx.appcompat.app.AppCompatActivity
import com.example.ellegadodepintia.minijuego1.Minijuego1
class MapActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
......
package com.example.ellegadodepintia
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class Minijuego1 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_minijuego1)
}
}
\ No newline at end of file
package com.example.ellegadodepintia.minijuego1
import android.content.Intent
import android.os.Bundle
import android.os.CountDownTimer
import android.os.Handler
import android.os.Looper
import android.widget.Button
import android.widget.FrameLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.ellegadodepintia.R
import kotlin.random.Random
class Minijuego1 : AppCompatActivity() {
private lateinit var frameLayout: FrameLayout // Layout donde se agregarán los botones
private lateinit var puntajeTextView: TextView // TextView para mostrar el puntaje
private lateinit var tiempoTextView: TextView // TextView para mostrar el tiempo
private var puntaje = 0 // Variable para almacenar el puntaje
private val handler = Handler(Looper.getMainLooper()) // Crear Handler con el Looper del hilo principal
private var tiempoRestante = 30 // Tiempo límite del juego (en segundos)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_minijuego1)
frameLayout = findViewById(R.id.frameLayout) // Referencia al FrameLayout
puntajeTextView = findViewById(R.id.puntajeTextView) // Referencia al TextView para el puntaje
tiempoTextView = findViewById(R.id.tiempoTextView) // Referencia al TextView para el tiempo
// Iniciar el juego
iniciarJuego()
iniciarContador() // Iniciar el contador de tiempo
}
private fun iniciarJuego() {
// Se programa la adición de un botón cada segundo
handler.postDelayed(object : Runnable {
override fun run() {
agregarBoton() // Llamar a la función para agregar un botón
handler.postDelayed(this, Random.nextLong(10, 2000)) // Repetir cada x segundo
}
}, 1000) // Inicializar después de 1 segundo
}
private fun agregarBoton() {
val boton = Button(this) // Crear un nuevo botón
boton.setBackgroundResource(R.drawable.hueso) // Imagen del botón
// Posicionamos el botón aleatoriamente dentro del FrameLayout
val huesoAleatorio = Random.nextInt(100, 300)
val params = FrameLayout.LayoutParams(huesoAleatorio, huesoAleatorio) // Tamaño del botón
params.leftMargin = Random.nextInt(0, frameLayout.width - 300) // Margen izquierdo aleatorio
params.topMargin = Random.nextInt(0, frameLayout.height - 300) // Margen superior aleatorio
boton.layoutParams = params // Asignar parámetros al botón
// Configurar la acción al pulsar el botón
boton.setOnClickListener {
puntaje++ // Incrementar el puntaje
actualizarPuntaje() // Actualizar el TextView con el puntaje
frameLayout.removeView(boton) // Remover el botón al ser pulsado
}
frameLayout.addView(boton) // Agregar el botón al FrameLayout
handler.postDelayed({
frameLayout.removeView(boton) // Remover el botón después de 3 segundos
}, Random.nextLong(800, 1000))
}
private fun iniciarContador() {
// Creamos un contador con un tiempo de 30 segundos
object : CountDownTimer(30 * 1000L, 1000) {
override fun onTick(millisUntilFinished: Long) {
// Actualizamos el tiempo restante cada segundo
tiempoRestante = (millisUntilFinished / 1000).toInt()
tiempoTextView.text = "Tiempo: $tiempoRestante s" // Mostrar el tiempo restante en el TextView
if (tiempoRestante == 1){
handler.removeCallbacksAndMessages(null) // Detener el juego al acabar el tiempo
}
}
override fun onFinish() {
// El juego termina cuando el tiempo se acaba
val intent = Intent(this@Minijuego1, FinMinijuego1::class.java)
intent.putExtra("puntuacion", puntaje) // Pasamos la puntuación
startActivity(intent) // Lanzamos la actividad de resultados
finish() // Cerramos la actividad actual
}
}.start() // Iniciar el contador
}
// Función para actualizar el puntaje en el TextView
private fun actualizarPuntaje() {
puntajeTextView.text = "Puntaje: $puntaje" // Actualiza el texto del puntaje
}
override fun onDestroy() {
super.onDestroy()
handler.removeCallbacksAndMessages(null) // Detener el juego al cerrar la actividad
}
}
\ No newline at end of file
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@color/pintiaBackground">
android:background="@color/pintiaBackground"
android:padding="16dp">
<!-- TextView para mostrar el puntaje en la parte superior izquierda -->
<TextView
android:id="@+id/puntajeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Puntaje: 0"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_gravity="top|start"
android:padding="16dp"/>
<TextView
android:id="@+id/titleTextView"
android:id="@+id/tiempoTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Minijuego 1"
android:textColor="@color/pintiaTitleText"
android:textSize="24sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
android:text="Tiempo: 30s"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_gravity="top|end"
android:padding="16dp"/>
<!-- FrameLayout donde se agregarán los botones -->
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"/>
</LinearLayout>
</FrameLayout>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment