“Stories, historias, iсторії, iστορίες

Mon, Jan 1

Free Workshop

This immersive event celebrates the universal human experience through the lenses of history and ancestry, featuring a diverse array of photographers whose works capture the essence of different cultures and historical moments.

RSVP

Close up photo of white flowers on a grey background

#!/usr/bin/env python
# coding: utf-8

# In[ ]:

import pygame
import os
import tkinter as tk
from tkinter import messagebox

# Inicialización de Pygame
pygame.init()

# Dimensiones del tablero
ANCHO, ALTO = 600, 600
TAMAÑO_CASILLA = ANCHO // 8

# Colores
BLANCO = (240, 217, 181)
NEGRO = (181, 136, 99)

# Crear ventana
ventana = pygame.display.set_mode((ANCHO, ALTO))
pygame.display.set_caption("Ajedrez de Gestión del Riesgo")

# Mensajes para cada casilla
mensajes_blancos = [
"El SIATA ayuda a monitorear el clima y prevenir desastres.",
"La gestión del riesgo comienza con el conocimiento del territorio.",
"Reforestar reduce el riesgo de inundaciones y deslizamientos.",
"Los sistemas de alerta temprana salvan vidas.",
"Fortalece la infraestructura crítica para proteger comunidades.",
"Capacitar a la comunidad mejora la resiliencia.",
"La vigilancia de ríos y quebradas previene emergencias.",
"El SIATA utiliza tecnología avanzada para gestionar riesgos."
] * 8

mensajes_negros = [
"Consulta el SIATA para estar informado sobre riesgos.",
"Participa en simulacros de evacuación en tu comunidad.",
"Conoce los planes de emergencia locales.",
"Evita construir en zonas de alto riesgo.",
"Infórmate sobre cómo actúa el SIATA en emergencias.",
"Aprende a interpretar alertas meteorológicas.",
"Reporta situaciones de riesgo a las autoridades.",
"Haz uso de la tecnología para prevenir desastres."
] * 8

# Variables de estado para el enroque
rey_blanco_movido = False
rey_negro_movido = False
torre_blanca_dama_movida = False
torre_blanca_rey_movida = False
torre_negra_dama_movida = False
torre_negra_rey_movida = False

# Tablero inicial
def crear_tablero():
tablero = [[" " for _ in range(8)] for _ in range(8)]
tablero[0] = ["r", "n", "b", "q", "k", "b", "n", "r"] # Piezas negras
tablero[1] = ["p" for _ in range(8)] # Peones negros
tablero[6] = ["P" for _ in range(8)] # Peones blancos
tablero[7] = ["R", "N", "B", "Q", "K", "B", "N", "R"] # Piezas blancas
return tablero

# Dibujar tablero
def dibujar_tablero():
for fila in range(8):
for col in range(8):
color = BLANCO if (fila + col) % 2 == 0 else NEGRO
pygame.draw.rect(ventana, color, (col * TAMAÑO_CASILLA, fila * TAMAÑO_CASILLA, TAMAÑO_CASILLA, TAMAÑO_CASILLA))

# Cargar imágenes de las piezas
def cargar_imagen(ruta):
if not os.path.exists(ruta):
print(f"Advertencia: La imagen {ruta} no existe. Usando marcador de posición.")
return pygame.Surface((TAMAÑO_CASILLA, TAMAÑO_CASILLA)) # Superficie vacía como marcador
return pygame.image.load(ruta)

PIEZAS = {
"r": {
"tematica": cargar_imagen("piezas/RiesgoTorre.png"),
"tradicional": cargar_imagen("piezas/TradicionalTorreNegro.png")
},
"n": {
"tematica": cargar_imagen("piezas/RiesgoCaballo.png"),
"tradicional": cargar_imagen("piezas/TradicionalCaballoNegro.png")
},
"b": {
"tematica": cargar_imagen("piezas/RiesgoAlfil.png"),
"tradicional": cargar_imagen("piezas/TradicionalAlfilNegro.png")
},
"q": {
"tematica": cargar_imagen("piezas/RiesgoReina.png"),
"tradicional": cargar_imagen("piezas/TradicionalReinaNegro.png")
},
"k": {
"tematica": cargar_imagen("piezas/RiesgoRey.png"),
"tradicional": cargar_imagen("piezas/TradicionalReyNegro.png")
},
"p": {
"tematica": cargar_imagen("piezas/RiesgoPeon.png"),
"tradicional": cargar_imagen("piezas/TradicionalPeonNegro.png")
},
"R": {
"tematica": cargar_imagen("piezas/RecursoTorre.png"),
"tradicional": cargar_imagen("piezas/TradicionalTorreBlanco.png")
},
"N": {
"tematica": cargar_imagen("piezas/RecursoCaballo.png"),
"tradicional": cargar_imagen("piezas/TradicionalCaballoBlanco.png")
},
"B": {
"tematica": cargar_imagen("piezas/RecursoAlfil.png"),
"tradicional": cargar_imagen("piezas/TradicionalAlfilBlanco.png")
},
"Q": {
"tematica": cargar_imagen("piezas/RecursoReina.png"),
"tradicional": cargar_imagen("piezas/TradicionalReinaBlanco.png")
},
"K": {
"tematica": cargar_imagen("piezas/RecursoRey.png"),
"tradicional": cargar_imagen("piezas/TradicionalReyBlanco.png")
},
"P": {
"tematica": cargar_imagen("piezas/ComunidadPeon.png"),
"tradicional": cargar_imagen("piezas/TradicionalPeonBlanco.png")
}
}

for key in PIEZAS:
for tipo in PIEZAS[key]:
if PIEZAS[key][tipo]:
PIEZAS[key][tipo] = pygame.transform.scale(PIEZAS[key][tipo], (TAMAÑO_CASILLA, TAMAÑO_CASILLA))

# Dibujar piezas
def dibujar_piezas(tablero, hover_pos=None):
for fila in range(8):
for col in range(8):
pieza = tablero[fila][col]
if pieza != " ":
img = PIEZAS[pieza]["tematica"] # Por defecto, muestra la temática

# Cambiar a la imagen tradicional si el mouse está sobre la pieza
if hover_pos == (fila, col):
img = PIEZAS[pieza]["tradicional"]

ventana.blit(img, (col * TAMAÑO_CASILLA, fila * TAMAÑO_CASILLA))

# Movimiento de piezas
def mover_pieza(tablero, origen, destino):
"""
Mueve una pieza de la posición origen a la posición destino en el tablero.
"""
x1, y1 = origen
x2, y2 = destino
pieza = tablero[x1][y1]
tablero[x2][y2] = pieza
tablero[x1][y1] = " "

# Mostrar mensaje en una ventana emergente
def mostrar_mensaje(mensaje):
root = tk.Tk()
root.withdraw() # Ocultar la ventana principal
messagebox.showinfo("Información", mensaje)
root.destroy()

# Obtener casilla desde posición del mouse
def obtener_casilla(pos):
x, y = pos
return y // TAMAÑO_CASILLA, x // TAMAÑO_CASILLA

# Validar si una casilla está bajo ataque
def casilla_atacada(tablero, casilla, atacante):
x, y = casilla
for fila in range(8):
for col in range(8):
pieza = tablero[fila][col]
if pieza != " " and ((pieza.islower() and atacante == "blanco") or (pieza.isupper() and atacante == "negro")):
if movimiento_valido(tablero, (fila, col), casilla, atacante):
return True
return False

# Movimiento válido
def movimiento_valido(tablero, origen, destino, turno):
x1, y1 = origen
x2, y2 = destino
pieza = tablero[x1][y1]

if pieza == " " or (pieza.islower() and turno == "blanco") or (pieza.isupper() and turno == "negro"):
return False

dx, dy = abs(x2 - x1), abs(y2 - y1)

# Peones
if pieza.lower() == "p":
if turno == "blanco":
if x2 >= x1:
return False
if x2 == x1 - 1 and dy == 0 and tablero[x2][y2] == " ":
return True
if x2 == x1 - 2 and x1 == 6 and dy == 0 and tablero[x2][y2] == " ":
return tablero[x1 - 1][y1] == " "
if x2 == x1 - 1 and dy == 1 and tablero[x2][y2] != " " and tablero[x2][y2].islower():
return True
else:
if x2 <= x1: return False if x2 == x1 + 1 and dy == 0 and tablero[x2][y2] == " ": return True if x2 == x1 + 2 and x1 == 1 and dy == 0 and tablero[x2][y2] == " ": return tablero[x1 + 1][y1] == " " if x2 == x1 + 1 and dy == 1 and tablero[x2][y2] != " " and tablero[x2][y2].isupper(): return True return False # Caballos if pieza.lower() == "n": if not ((dx == 2 and dy == 1) or (dx == 1 and dy == 2)): return False return tablero[x2][y2] == " " or tablero[x2][y2].islower() != pieza.islower() # Torres if pieza.lower() == "r": if dx != 0 and dy != 0: # Movimiento solo en línea recta return False return tablero[x2][y2] == " " or tablero[x2][y2].islower() != pieza.islower() # Alfiles if pieza.lower() == "b": if dx != dy: # Movimiento solo en diagonal return False return tablero[x2][y2] == " " or tablero[x2][y2].islower() != pieza.islower() # Reina if pieza.lower() == "q": if dx != dy and dx != 0 and dy != 0: # Línea recta o diagonal return False return tablero[x2][y2] == " " or tablero[x2][y2].islower() != pieza.islower() # Rey if pieza.lower() == "k": if dx > 1 or dy > 1: # Una casilla en cualquier dirección
return False

# Enroque
if dx == 0 and dy == 2:
if turno == "blanco":
if x1 == 7 and y1 == 4: # Enroque blanco
if y2 == 6 and not rey_blanco_movido and not torre_blanca_rey_movida:
if tablero[7][5] == " " and tablero[7][6] == " ":
if not casilla_atacada(tablero, (7, 4), "negro") and \
not casilla_atacada(tablero, (7, 5), "negro") and \
not casilla_atacada(tablero, (7, 6), "negro"):
return True
if y2 == 2 and not rey_blanco_movido and not torre_blanca_dama_movida:
if tablero[7][1] == " " and tablero[7][2] == " " and tablero[7][3] == " ":
if not casilla_atacada(tablero, (7, 4), "negro") and \
not casilla_atacada(tablero, (7, 3), "negro") and \
not casilla_atacada(tablero, (7, 2), "negro"):
return True
else: # Enroque negro
if x1 == 0 and y1 == 4:
if y2 == 6 and not rey_negro_movido and not torre_negra_rey_movida:
if tablero[0][5] == " " and tablero[0][6] == " ":
if not casilla_atacada(tablero, (0, 4), "blanco") and \
not casilla_atacada(tablero, (0, 5), "blanco") and \
not casilla_atacada(tablero, (0, 6), "blanco"):
return True
if y2 == 2 and not rey_negro_movido and not torre_negra_dama_movida:
if tablero[0][1] == " " and tablero[0][2] == " " and tablero[0][3] == " ":
if not casilla_atacada(tablero, (0, 4), "blanco") and \
not casilla_atacada(tablero, (0, 3), "blanco") and \
not casilla_atacada(tablero, (0, 2), "blanco"):
return True
return tablero[x2][y2] == " " or tablero[x2][y2].islower() != pieza.islower()

return False

# Verificar si el camino está libre
def camino_libre(tablero, origen, destino):
x1, y1 = origen
x2, y2 = destino
dx = 1 if x2 > x1 else -1 if x2 < x1 else 0 dy = 1 if y2 > y1 else -1 if y2 < y1 else 0 x, y = x1 + dx, y1 + dy while (x, y) != (x2, y2): if tablero[x][y] != " ": return False x += dx y += dy return True # Juego principal def jugar_ajedrez(): tablero = crear_tablero() seleccion = None turno = "blanco" hover_pos = None corriendo = True while corriendo: for evento in pygame.event.get(): if evento.type == pygame.QUIT: corriendo = False elif evento.type == pygame.MOUSEMOTION: hover_pos = obtener_casilla(evento.pos) elif evento.type == pygame.MOUSEBUTTONDOWN: fila, col = obtener_casilla(evento.pos) if seleccion: origen = seleccion destino = (fila, col) if movimiento_valido(tablero, origen, destino, turno): mover_pieza(tablero, origen, destino) mensaje = mensajes_blancos[fila * 8 + col] if turno == "blanco" else mensajes_negros[fila * 8 + col] mostrar_mensaje(mensaje) turno = "negro" if turno == "blanco" else "blanco" else: print("Movimiento inválido") seleccion = None else: if tablero[fila][col] != " " and ( (turno == "blanco" and tablero[fila][col].isupper()) or (turno == "negro" and tablero[fila][col].islower()) ): seleccion = (fila, col) dibujar_tablero() dibujar_piezas(tablero, hover_pos=hover_pos) if seleccion: x, y = seleccion pygame.draw.rect(ventana, (0, 255, 0), (y * TAMAÑO_CASILLA, x * TAMAÑO_CASILLA, TAMAÑO_CASILLA, TAMAÑO_CASILLA), 3) pygame.display.flip() # Iniciar el juego if __name__ == "__main__": jugar_ajedrez() pygame.quit()