Skip to main content

Overview

The Memorice main script (memorice.gd) manages a memory card matching game where players flip clam shells to find matching pairs. Features a point system with rewards for matches and penalties for mismatches, audio feedback, floating text effects, and pause functionality. Script Path: minigames/memorice/scripts/memorice.gd Extends: Node2D

Signals

back_to_menu
signal
Emitted when player wants to return to main menu.

Node References

UI Elements

@onready var puntos_label = $UI/PuntosLabel
@onready var intro_panel: Control = $intro_panel
@onready var intro_button: Button = $intro_panel/Button
@onready var backButton: Button = $UI/backButton

Game Over Panel

@onready var panel: Panel = $UI/GameOverPanel
@onready var title: Label = $UI/GameOverPanel/Title
@onready var score_lbl: Label = $UI/GameOverPanel/score
@onready var back_btn: TextureButton = $UI/GameOverPanel/Buttons/BackButton
@onready var retry_btn: TextureButton = $UI/GameOverPanel/Buttons/RetryButton

Game Content

@onready var almejas_container = $Almejas
var texto_flotante_escena = preload("res://minigames/memorice/scenes/texto.tscn")

Audio

@onready var musica_fondo = $MusicaFondo
@onready var sfx_abrir = $SFX_AbrirAlmeja
@onready var sfx_acierto = $SFX_Acierto
@onready var sfx_error = $SFX_Error
@onready var sfx_menos_puntos = $SFX_menos_puntos
@onready var sfx_mas_puntos = $SFX_mas_puntos

State Variables

puntos
int
default:"100"
Current points. Starts at 100, increases by 100 for matches, decreases by 50 for mismatches. Game ends at 0.
primera_seleccion
Node
default:"null"
Reference to the first clam selected in a matching attempt.
segunda_seleccion
Node
default:"null"
Reference to the second clam selected in a matching attempt.
comparando
bool
default:"false"
Whether the game is currently comparing two selected clams. Blocks further input.
juego_iniciado
bool
default:"false"
Whether the game has started (intro completed).
is_paused
bool
default:"false"
Whether the game is currently paused.
last_coins_gained
int
default:"0"
Coins earned in current session for display on game over/victory.

Core Functions

Initialization

_ready
void
Sets up the game board and UI.Key Actions:
  • Sets window to 480×800 portrait mode
  • Shows intro panel
  • Blocks all clams until game starts
  • Connects button signals
  • Configures pause-compatible process modes
  • Generates and shuffles game board
  • Connects clam signals
_generar_tablero
void
Generates the game board with paired clams.Board Composition:
  • 10 unique clam types (camaron, zapatilla, lata, gusano, alga, plastico, botella, basura, salmon, perla)
  • 2 of each type = 20 clams total
  • Arranged in 4×5 grid (4 columns, 5 rows)
Layout:
  • 65px horizontal separation, 60px vertical separation
  • Centered on screen with offset (+30, +100)
  • Shuffled random positions
conectar_almejas
void
Connects the “almeja_abierta” signal from all clam children to _on_almeja_abierta handler.

Game Flow

_on_intro_button_pressed
void
Starts the game when intro button is pressed.Actions:
  • Hides intro panel
  • Sets juego_iniciado = true
  • Calls mostrar_y_cerrar_inicialmente to preview all clams
mostrar_y_cerrar_inicialmente
void
Shows all clams briefly at game start so player can memorize positions.Parameters:
  • almejas (Array): Array of clam nodes
Flow:
  1. Block all clams
  2. Open all clams (play “abrir” animation)
  3. Wait 5 seconds
  4. Close all clams
  5. Unblock all clams (game begins)

Selection & Matching

_on_almeja_abierta
void
Called when a clam is opened by the player.Parameters:
  • almeja (Node): The clam node that was opened
Behavior:
  • Ignores if game not started, paused, or comparing
  • Plays opening sound
  • If first selection: stores reference
  • If second selection: stores reference, blocks all clams, calls _verificar_pareja
_verificar_pareja
void
Checks if the two selected clams match.Match Logic: Compares primera_seleccion.objeto_id with segunda_seleccion.objeto_idOn Match:
  1. Play acierto sound
  2. Wait 0.6s, play mas_puntos sound
  3. Show “+100” green floating text
  4. Add 100 points
  5. Wait 0.8s
  6. Remove both clams from scene
  7. Check if board is empty (victory condition)
On Mismatch:
  1. Play error sound
  2. Wait 0.4s, play menos_puntos sound
  3. Show “-50” red floating text
  4. Subtract 50 points (minimum 0)
  5. Wait 1.0s
  6. Close both clams
  7. Wait 0.8s
  8. Check if points reached 0 (game over condition)
Post-Check:
  • Resets selection variables to null
  • Sets comparando = false
  • Unblocks all clams

Helper Functions

bloquear_todas
void
Blocks or unblocks all clams.Parameters:
  • bloquear (bool): True to block, false to unblock
  • except (Array): Clams to exclude from blocking (default: empty)
Effect: Sets almeja.bloqueada property for each clam
actualizar_puntaje
void
Updates the points display label.Format: “Puntos:
_mostrar_texto_flotante
void
Spawns floating text effect.Parameters:
  • pos (Vector2): World position for text
  • texto (String): Text to display (e.g., “+100”, “-50”)
  • color (Color): Text color
Implementation: Instantiates texto_flotante_escena and calls its mostrar() method

End Game Conditions

mostrar_victoria
void
Displays victory screen when all pairs are matched.Actions:
  • Processes coins (calls _procesar_monedas_finales)
  • Sets panel title to “¡Ganaste!”
  • Shows final score and coins earned
  • Makes panel visible
mostrar_game_over
void
Displays game over screen when points reach 0.Actions:
  • Processes coins (calls _procesar_monedas_finales)
  • Sets panel title to “¡Fin del Juego!”
  • Shows final score and coins earned
  • Makes panel visible
_procesar_monedas_finales
void
Saves score and awards coins via ScoreManager.Logic:
  • Saves high score to “memorice” key
  • Calculates coins: puntos × 0.5
  • Adds coins to player account
  • Stores in last_coins_gained for display

Pause System

pause_game
void
Pauses the game.Actions:
  • Pauses all audio streams
  • Blocks all clams
  • Sets comparando = true to prevent input
  • Shows pause panel with current score
resume_game
void
Resumes from pause.Actions:
  • Resumes all audio streams
  • Unblocks clams if board not empty
  • Sets comparando = false
  • Hides pause panel
_on_back_pressed
void
Returns to main menu.Actions:
  • Resumes game if paused
  • Resets screen to portrait orientation
  • Changes to main menu scene
_on_retry_pressed
void
Reloads current scene to restart game.
_on_backButton_pressed
void
Toggles pause state.
_unhandled_input
void
Handles escape key (ui_cancel) to toggle pause.

Clam Scene Requirements

Each clam instance must have:
objeto_id
String
Unique identifier for the object inside the clam (e.g., “camaron”, “lata”).
bloqueada
bool
Whether the clam is currently blocked from interaction.
abierta
bool
Whether the clam is currently open.
anim_sprite
AnimatedSprite2D
Sprite that plays open/close animations.
abrir
void
Opens the clam and emits “almeja_abierta” signal.
cerrar
void
Closes the clam.
almeja_abierta
signal
Emitted when clam is opened, passes self as parameter.

Clam Types

The game includes 10 different clam types:
  • almeja_camaron.tscn - Shrimp (food)
  • almeja_zapatilla.tscn - Shoe (trash)
  • almeja_lata.tscn - Can (trash)
  • almeja_gusano.tscn - Worm (bait)
  • almeja_alga.tscn - Seaweed (natural)
  • almeja_plastico.tscn - Plastic (trash)
  • almeja_botella.tscn - Bottle (trash)
  • almeja_basura.tscn - Garbage (trash)
  • almeja_salmon.tscn - Salmon (food)
  • almeja_perla.tscn - Pearl (treasure)

ScoreManager Integration

var score_manager = get_node("/root/ScoreManager")
if score_manager:
    score_manager.save_high_score("memorice", puntos)
    score_manager.add_coins(coins_earned)
Game Key: "memorice" Coin Conversion: Final puntos × 0.5

Common Patterns

Point System

  • Starting Points: 100
  • Match Reward: +100
  • Mismatch Penalty: -50
  • Minimum: 0 (triggers game over)
  • Maximum: 1000 (if all matches without mistakes)

Board Layout Calculation

var columnas = 4
var separacion = Vector2(65, 60)
var filas = ceil(float(lista_instancias.size()) / columnas)
var ancho_tablero = columnas * separacion.x
var alto_tablero = filas * separacion.y
var inicio = pantalla_centro - Vector2(ancho_tablero / 2, alto_tablero / 2)
inicio += Vector2(30, 100)  # Offset adjustment

Sequential Audio Feedback

Matches and mismatches use staggered sound effects:
# Match
sfx_acierto.play()
await get_tree().create_timer(0.6).timeout
sfx_mas_puntos.play()

# Mismatch
sfx_error.play()
await get_tree().create_timer(0.4).timeout
sfx_menos_puntos.play()

Async Clam Operations

Clams close asynchronously to allow animation to complete:
if is_instance_valid(primera_seleccion): await primera_seleccion.cerrar()
if is_instance_valid(segunda_seleccion): await segunda_seleccion.cerrar()
The 5-second preview at game start allows players to memorize clam positions, making this a true memory challenge.
Always check is_instance_valid() before interacting with clams, as they may be removed from the scene during match processing.

Build docs developers (and LLMs) love