ciclo de la vida CON CHAMBA 67

Python
# ============================================================
# VARIABLES GLOBALES
# ============================================================

aldeano: Sprite = None
granja: Sprite = None
casa: Sprite = None
restaurante: Sprite = None
enemigo_sprite: Sprite = None

contador_dia = 0
enemigo_presente = False
es_dia = True

# ============================================================
# POSICIONES
# ============================================================

CASA_X = 20
CASA_Y = 100

GRANJA_X = 140
GRANJA_Y = 100

RESTAURANTE_X = 80
RESTAURANTE_Y = 30

ESQUELETO_X = 80
ESQUELETO_Y = 60

# ============================================================
# CONFIGURACIÓN
# ============================================================

VELOCIDAD = 50
DURACION_DIA = 300

# ============================================================
# ESTADO DE LA FSM
# ============================================================

estado = "EN_CASA"


# ============================================================
# ACTUALIZAR CIELO
# ============================================================

def actualizar_cielo():
    if estado == "EN_RESTAURANTE":
        scene.set_background_color(5)
    elif es_dia:
        scene.set_background_color(9)
    else:
        scene.set_background_color(12)


# ============================================================
# TRANSICIONES DE LA FSM
# ============================================================

def transicion_fsm():
    global estado

    # --------------------------------------------------------
    # EL ESQUELETO TIENE PRIORIDAD
    # --------------------------------------------------------

    if enemigo_presente:

        if aldeano.x != CASA_X or aldeano.y != CASA_Y:
            estado = "REGRESANDO_A_CASA"
        else:
            estado = "EN_CASA"

        return

    # --------------------------------------------------------
    # EN CASA
    # --------------------------------------------------------

    if estado == "EN_CASA":

        if es_dia:
            estado = "IR_A_GRANJA"

    # --------------------------------------------------------
    # IR A LA GRANJA
    # --------------------------------------------------------

    elif estado == "IR_A_GRANJA":

        if not es_dia:
            estado = "REGRESANDO_A_CASA"

        elif aldeano.x >= GRANJA_X:

            aldeano.set_position(
                GRANJA_X,
                GRANJA_Y
            )

            estado = "TRABAJANDO"

    # --------------------------------------------------------
    # TRABAJANDO
    # --------------------------------------------------------

    elif estado == "TRABAJANDO":

        if not es_dia:
            estado = "REGRESANDO_A_CASA"
        else:
            estado = "IR_A_RESTAURANTE"

    # --------------------------------------------------------
    # IR AL RESTAURANTE
    # --------------------------------------------------------

    elif estado == "IR_A_RESTAURANTE":

        if not es_dia:
            estado = "REGRESANDO_A_CASA"

        elif (abs(aldeano.x - RESTAURANTE_X) <= VELOCIDAD and
              abs(aldeano.y - RESTAURANTE_Y) <= VELOCIDAD):

            aldeano.set_position(
                RESTAURANTE_X,
                RESTAURANTE_Y
            )

            estado = "EN_RESTAURANTE"

    # --------------------------------------------------------
    # EN EL RESTAURANTE
    # --------------------------------------------------------

    elif estado == "EN_RESTAURANTE":

        if not es_dia:
            estado = "REGRESANDO_A_CASA"

    # --------------------------------------------------------
    # REGRESANDO A CASA
    # --------------------------------------------------------

    elif estado == "REGRESANDO_A_CASA":

        if (abs(aldeano.x - CASA_X) <= VELOCIDAD and
            abs(aldeano.y - CASA_Y) <= VELOCIDAD):

            aldeano.set_position(
                CASA_X,
                CASA_Y
            )

            estado = "EN_CASA"


# ============================================================
# ACCIONES DE LA FSM
# ============================================================

def accion_fsm():

    # --------------------------------------------------------
    # EN CASA
    # --------------------------------------------------------

    if estado == "EN_CASA":

        aldeano.vx = 0
        aldeano.vy = 0

    # --------------------------------------------------------
    # IR A LA GRANJA
    # --------------------------------------------------------

    elif estado == "IR_A_GRANJA":

        aldeano.vx = VELOCIDAD
        aldeano.vy = 0

    # --------------------------------------------------------
    # TRABAJANDO
    # --------------------------------------------------------

    elif estado == "TRABAJANDO":

        aldeano.vx = 0
        aldeano.vy = 0

    # --------------------------------------------------------
    # IR AL RESTAURANTE
    # --------------------------------------------------------

    elif estado == "IR_A_RESTAURANTE":

        if aldeano.x < RESTAURANTE_X:
            aldeano.vx = VELOCIDAD
        elif aldeano.x > RESTAURANTE_X:
            aldeano.vx = -VELOCIDAD
        else:
            aldeano.vx = 0

        if aldeano.y > RESTAURANTE_Y:
            aldeano.vy = -VELOCIDAD
        elif aldeano.y < RESTAURANTE_Y:
            aldeano.vy = VELOCIDAD
        else:
            aldeano.vy = 0

    # --------------------------------------------------------
    # EN EL RESTAURANTE
    # --------------------------------------------------------

    elif estado == "EN_RESTAURANTE":

        aldeano.vx = 0
        aldeano.vy = 0

        # Cielo amarillo
        scene.set_background_color(5)

    # --------------------------------------------------------
    # REGRESANDO A CASA
    # --------------------------------------------------------

    elif estado == "REGRESANDO_A_CASA":

        if aldeano.x > CASA_X:
            aldeano.vx = -VELOCIDAD
        elif aldeano.x < CASA_X:
            aldeano.vx = VELOCIDAD
        else:
            aldeano.vx = 0

        if aldeano.y > CASA_Y:
            aldeano.vy = -VELOCIDAD
        elif aldeano.y < CASA_Y:
            aldeano.vy = VELOCIDAD
        else:
            aldeano.vy = 0


# ============================================================
# CICLO DÍA / NOCHE
# ============================================================

def actualizar_ciclo_dia():
    global contador_dia
    global es_dia

    contador_dia += 1

    if contador_dia >= DURACION_DIA:

        contador_dia = 0
        es_dia = not es_dia

        actualizar_cielo()


# ============================================================
# CREAR ESQUELETO
# ============================================================

def colocar_enemigo():
    global enemigo_sprite
    global enemigo_presente

    if not enemigo_presente:

        enemigo_sprite = sprites.create(
            assets.image("""
                enemy
            """),
            SpriteKind.enemy
        )

        enemigo_sprite.set_position(
            ESQUELETO_X,
            ESQUELETO_Y
        )

        enemigo_presente = True


# ============================================================
# ELIMINAR ESQUELETO
# ============================================================

def eliminar_enemigo():
    global enemigo_sprite
    global enemigo_presente

    if enemigo_presente:

        enemigo_sprite.destroy()

        enemigo_sprite = None

        enemigo_presente = False

        actualizar_cielo()


# ============================================================
# BOTÓN A
# ============================================================

def on_a_pressed():
    colocar_enemigo()


controller.A.on_event(
    ControllerButtonEvent.PRESSED,
    on_a_pressed
)


# ============================================================
# BOTÓN B
# ============================================================

def on_b_pressed():
    eliminar_enemigo()


controller.B.on_event(
    ControllerButtonEvent.PRESSED,
    on_b_pressed
)


# ============================================================
# CONFIGURAR JUEGO
# ============================================================

def configurar():
    global casa
    global granja
    global restaurante
    global aldeano

    # --------------------------------------------------------
    # CASA
    # --------------------------------------------------------

    casa = sprites.create(
        assets.image("""
            house
        """),
        SpriteKind.food
    )

    casa.set_position(
        CASA_X,
        CASA_Y
    )

    # --------------------------------------------------------
    # GRANJA
    # --------------------------------------------------------

    granja = sprites.create(
        assets.image("""
            farm
        """),
        SpriteKind.food
    )

    granja.set_position(
        GRANJA_X,
        GRANJA_Y
    )

    # --------------------------------------------------------
    # RESTAURANTE
    # --------------------------------------------------------

    restaurante = sprites.create(
        assets.image("""
            restaurant
        """),
        SpriteKind.food
    )

    restaurante.set_position(
        RESTAURANTE_X,
        RESTAURANTE_Y
    )

    # --------------------------------------------------------
    # ALDEANO
    # --------------------------------------------------------

    aldeano = sprites.create(
        assets.image("""
            villager
        """),
        SpriteKind.player
    )

    aldeano.set_position(
        CASA_X,
        CASA_Y
    )

    # --------------------------------------------------------
    # DETENER EDIFICIOS
    # --------------------------------------------------------

    casa.vx = 0
    casa.vy = 0

    granja.vx = 0
    granja.vy = 0

    restaurante.vx = 0
    restaurante.vy = 0


# ============================================================
# INICIO DEL JUEGO
# ============================================================

configurar()

es_dia = True
estado = "EN_CASA"

scene.set_background_color(9)


# ============================================================
# BUCLE PRINCIPAL
# ============================================================

def on_on_update():

    actualizar_ciclo_dia()

    transicion_fsm()

    accion_fsm()


game.on_update(on_on_update)
Feedback wanted
Uploaded Aug 26, 2026