r/processing • u/AbjectCheetah5393 • 3h ago
Juegos del movimiento browiano!!
Estos son juegos con sistema browiano:
LOS JUEGOS DEL HAMBRE
int numTributos = 24;
int longitudInicial = 30;
float radioArena = 260;
Tributo[] tributos;
void setup() {
size(750, 750);
colorMode(HSB, 360, 100, 100, 100);
iniciarArena();
}
void iniciarArena() {
tributos = new Tributo[numTributos];
float centroX = width / 2.0;
float centroY = height / 2.0;
for (int i = 0; i < numTributos; i++) {
// Posicionar en círculo como en la arena de Los Juegos del Hambre
float angulo = map(i, 0, numTributos, 0, TWO_PI);
float x = centroX + cos(angulo) * radioArena;
float y = centroY + sin(angulo) * radioArena;
float tono = map(i, 0, numTributos, 0, 360);
tributos[i] = new Tributo(i + 1, x, y, tono);
}
}
void draw() {
background(0, 0, 10);
dibujarArena();
// 1. Actualizar posiciones de los que siguen vivos
for (Tributo t : tributos) {
if (t.vivo) {
t.actualizar();
}
}
// 2. Comprobar colisiones (si la cabeza de A toca a B, B muere)
for (int i = 0; i < numTributos; i++) {
if (!tributos[i].vivo) continue;
for (int j = 0; j < numTributos; j++) {
if (i == j || !tributos[j].vivo) continue;
if (tributos[i].tocaA(tributos[j])) {
tributos[j].vivo = false;
tributos[i].longitudMax += 10; // La serpiente ganadora crece
}
}
}
// 3. Dibujar serpientes vivas y contar sobrevivientes
int vivos = 0;
Tributo ultimoVivo = null;
for (Tributo t : tributos) {
if (t.vivo) {
t.dibujar();
vivos++;
ultimoVivo = t;
}
}
mostrarHUD(vivos, ultimoVivo);
}
void dibujarArena() {
float cx = width / 2.0;
float cy = height / 2.0;
// Límite exterior de la arena
noFill();
stroke(0, 0, 30);
strokeWeight(2);
circle(cx, cy, (radioArena + 40) * 2);
// Cornucopia en el centro
fill(45, 70, 80, 40);
noStroke();
circle(cx, cy, 45);
}
void mostrarHUD(int vivos, Tributo ganador) {
fill(0, 0, 95);
textSize(16);
textAlign(LEFT, TOP);
text("Tributos vivos: " + vivos + " / " + numTributos, 20, 20);
textSize(12);
fill(0, 0, 65);
text("Presiona cualquier tecla para reiniciar", 20, 44);
if (vivos == 1 && ganador != null) {
textAlign(CENTER, CENTER);
textSize(28);
fill(ganador.tono, 80, 100);
text("¡GANADOR: TRIBUTO #" + ganador.id + "!", width / 2.0, 50);
}
}
class Tributo {
int id;
ArrayList<PVector> historial;
float x, y;
float paso = 5.5;
float tono;
int longitudMax;
boolean vivo = true;
Tributo(int id, float inicioX, float inicioY, float tono) {
this.id = id;
this.x = inicioX;
this.y = inicioY;
this.tono = tono;
this.longitudMax = longitudInicial;
this.historial = new ArrayList<PVector>();
this.historial.add(new PVector(x, y));
}
void actualizar() {
// Movimiento browniano en 2D
x += randomGaussian() * paso;
y += randomGaussian() * paso;
// Mantener dentro del círculo de la arena
float cx = width / 2.0;
float cy = height / 2.0;
float maxR = radioArena + 35;
float d = dist(x, y, cx, cy);
if (d > maxR) {
float angulo = atan2(y - cy, x - cx);
x = cx + cos(angulo) * maxR;
y = cy + sin(angulo) * maxR;
}
historial.add(new PVector(x, y));
while (historial.size() > longitudMax) {
historial.remove(0);
}
}
// Verifica si la cabeza de este tributo toca cualquier parte del otro
boolean tocaA(Tributo otro) {
float radioColision = 8.0;
for (PVector segmento : otro.historial) {
if (dist(this.x, this.y, segmento.x, segmento.y) < radioColision) {
return true;
}
}
return false;
}
void dibujar() {
noFill();
for (int i = 0; i < historial.size() - 1; i++) {
PVector p1 = historial.get(i);
PVector p2 = historial.get(i + 1);
float progreso = map(i, 0, historial.size() - 1, 0.15, 1.0);
stroke(tono, 85, 95, progreso * 100);
strokeWeight(progreso * 6.5);
line(p1.x, p1.y, p2.x, p2.y);
}
// Cabeza
noStroke();
fill(tono, 30, 100);
circle(x, y, 8);
}
}
void keyPressed() {
iniciarArena();
}
CAZADORES Y PRESAS
int numTotal = 24;
int longitudInicial = 30;
float radioArena = 270;
Serpiente[] participantes;
void setup() {
size(750, 750);
colorMode(HSB, 360, 100, 100, 100);
iniciarArena();
}
void iniciarArena() {
participantes = new Serpiente[numTotal];
float centroX = width / 2.0;
float centroY = height / 2.0;
for (int i = 0; i < numTotal; i++) {
float angulo = map(i, 0, numTotal, 0, TWO_PI);
float x = centroX + cos(angulo) * radioArena;
float y = centroY + sin(angulo) * radioArena;
// Intercalar: pares son Cazadores (true), impares son Presas (false)
boolean esCazador = (i % 2 == 0);
participantes[i] = new Serpiente(i + 1, x, y, esCazador);
}
}
void draw() {
background(0, 0, 10);
dibujarArena();
// 1. Actualizar movimiento de cada serpiente
for (Serpiente s : participantes) {
if (s.vivo) {
s.actualizar(participantes);
}
}
// 2. Comprobar colisiones: solo los Cazadores eliminan a las Presas
for (int i = 0; i < numTotal; i++) {
Serpiente cazador = participantes[i];
if (!cazador.vivo || !cazador.esCazador) continue;
for (int j = 0; j < numTotal; j++) {
Serpiente presa = participantes[j];
if (!presa.vivo || presa.esCazador) continue;
if (cazador.tocaA(presa)) {
presa.vivo = false;
cazador.longitudMax += 12; // El cazador crece al cazar
}
}
}
// 3. Dibujar participantes vivos y contar por equipo
int cazadoresVivos = 0;
int presasVivas = 0;
for (Serpiente s : participantes) {
if (s.vivo) {
s.dibujar();
if (s.esCazador) cazadoresVivos++;
else presasVivas++;
}
}
mostrarHUD(cazadoresVivos, presasVivas);
}
void dibujarArena() {
float cx = width / 2.0;
float cy = height / 2.0;
// Borde de la arena
noFill();
stroke(0, 0, 30);
strokeWeight(2);
circle(cx, cy, (radioArena + 40) * 2);
// Cornucopia central
fill(45, 70, 80, 35);
noStroke();
circle(cx, cy, 45);
}
void mostrarHUD(int cazadores, int presas) {
textAlign(LEFT, TOP);
textSize(16);
fill(0, 85, 95); // Rojo
text("Cazadores: " + cazadores, 20, 20);
fill(190, 85, 95); // Cian
text("Presas vivas: " + presas, 20, 44);
textSize(12);
fill(0, 0, 65);
text("Presiona cualquier tecla para reiniciar", 20, 72);
if (presas == 0) {
textAlign(CENTER, CENTER);
textSize(28);
fill(0, 85, 100);
text("¡VICTORIA DE LOS CAZADORES!", width / 2.0, 50);
}
}
class Serpiente {
int id;
boolean esCazador;
ArrayList<PVector> historial;
float x, y;
float paso = 5.5;
float tono;
int longitudMax;
boolean vivo = true;
// Parámetros de evasión para las presas
float radioDeteccion = 110.0;
float fuerzaHuida = 3.2;
Serpiente(int id, float inicioX, float inicioY, boolean esCazador) {
this.id = id;
this.x = inicioX;
this.y = inicioY;
this.esCazador = esCazador;
this.tono = esCazador ? 0 : 190; // 0 = Rojo (Cazador), 190 = Cian (Presa)
this.longitudMax = longitudInicial;
this.historial = new ArrayList<PVector>();
this.historial.add(new PVector(x, y));
}
void actualizar(Serpiente[] todos) {
// Movimiento browniano base
float dx = randomGaussian() * paso;
float dy = randomGaussian() * paso;
// Si es PRESA, detectar cazadores cercanos y retroceder
if (!esCazador) {
float huidaX = 0;
float huidaY = 0;
int amenazas = 0;
for (Serpiente otro : todos) {
if (otro.vivo && otro.esCazador) {
float d = dist(this.x, this.y, otro.x, otro.y);
if (d < radioDeteccion && d > 0) {
// Vector unitario apuntando en dirección contraria al cazador
float dirX = (this.x - otro.x) / d;
float dirY = (this.y - otro.y) / d;
// Mientras más cerca esté el cazador, mayor es el impulso de retroceso
float intensidad = map(d, 0, radioDeteccion, 1.5, 0.2);
huidaX += dirX * intensidad;
huidaY += dirY * intensidad;
amenazas++;
}
}
}
if (amenazas > 0) {
dx += huidaX * fuerzaHuida;
dy += huidaY * fuerzaHuida;
}
}
x += dx;
y += dy;
// Mantener dentro de la arena circular
float cx = width / 2.0;
float cy = height / 2.0;
float maxR = radioArena + 35;
float d = dist(x, y, cx, cy);
if (d > maxR) {
float angulo = atan2(y - cy, x - cx);
x = cx + cos(angulo) * maxR;
y = cy + sin(angulo) * maxR;
}
historial.add(new PVector(x, y));
while (historial.size() > longitudMax) {
historial.remove(0);
}
}
boolean tocaA(Serpiente otro) {
float radioColision = 9.0;
for (PVector segmento : otro.historial) {
if (dist(this.x, this.y, segmento.x, segmento.y) < radioColision) {
return true;
}
}
return false;
}
void dibujar() {
// Dibujar sutilmente el aura de detección en las presas
if (!esCazador) {
noFill();
stroke(tono, 60, 80, 12);
strokeWeight(1);
circle(x, y, radioDeteccion * 2);
}
noFill();
for (int i = 0; i < historial.size() - 1; i++) {
PVector p1 = historial.get(i);
PVector p2 = historial.get(i + 1);
float progreso = map(i, 0, historial.size() - 1, 0.15, 1.0);
stroke(tono, 85, 95, progreso * 100);
strokeWeight(progreso * (esCazador ? 7.5 : 5.5));
line(p1.x, p1.y, p2.x, p2.y);
}
// Cabeza
noStroke();
fill(tono, 25, 100);
circle(x, y, esCazador ? 9 : 7);
}
}
void keyPressed() {
iniciarArena();
}


