act01_ascii/PeceraGUI.java

147 lines
4.6 KiB
Java
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import javax.swing.*;
import java.awt.*;
import java.util.Random;
/**
*
* @author car20
*/
public class PeceraGUI extends JPanel {
private static final int ANCHO = 60;
private static final int ALTO = 20;
private static final String[][] PECES = {
{
" ,-",
" ,/.( __",
" ,-' `!._/ /",
" > @ )<| _ <",
" `-....,,;' \\\\_"
},
{
" ,-",
" ,/.( __",
" ,-' `!._/ /",
" > @ )<| _ <",
" `-....,,;' \\\\_"
},
{
"|\\ \\\\__ o",
"| \\_/ o \\ o",
"> _ (( <_ oo",
"| / \\__+___/",
"|/ |/"
}
};
private static final String[] ALGAS = {
" ⠀⠀⠀⠀⠀⠀⣴⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀",
" ⠀⠀⠀⠀⣸⠛⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀",
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡏⢠⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀",
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣧⡈⢿⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀",
"⠀⠀⠀⠀⢸⣶⡀⠀⠀⠀⠀⠙⣷⣄⠙⣦⡀⠀⠀⠀⠀⠀⠀⠀",
"⠀⠀⠀⠀⠀⣿⠿⣄⠀⠀⠀⠀⢸⣿⡆⢸⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀",
"⠀⠀⠀⠀⢀⡇⢰⣿⠂⠀⠀⠀⣸⣿⠃⣸⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀",
"⠀⠀⠀⠀⣿⠁⣼⠁⠀⠀⠀⣰⡿⠁⣴⡿⠁⠀⠀⠀⠀",
"⠀⠀⠀⠀⣿⠀⣿⣆⠀⠀⢠⣿⠇⢸⣿⠃⠀⠀⠀⠀",
"⠀⠀⠀⠀⢹⣇⠘⣿⡄⠀⠘⣿⠀⣾⣿⡀⠀⠀",
"⠀⠀⠀⠀⣸⣿⠀⣿⡇⠀⣤⠙⠀⢸⣿⣧⡀⠀⠀",
"⠀⠀⠀⣰⣿⠏⢠⣿⡇⠀⢹⣷⣤⠈⢿⠁",
"⠀⠀⠠⡿⠃⣰⣿⠟⠀⠀",
"⠀⠀⠀⠀⠊⠉⠀⠀⠀⠀⠀"
};
private static int[] pecesX = new int[PECES.length];
private static int[] pecesY = new int[PECES.length];
private static int[] direccionesX = new int[PECES.length];
private static final Random random = new Random();
private int[] burbujasX = new int[10];
private int[] burbujasY = new int[10];
public PeceraGUI() {
setPreferredSize(new Dimension(ANCHO * 10, ALTO * 20));
setBackground(Color.BLACK);
setForeground(Color.WHITE);
setFont(new Font("Monospaced", Font.PLAIN, 12));
for (int i = 0; i < PECES.length; i++) {
pecesX[i] = random.nextInt(ANCHO - 10) + 1;
pecesY[i] = random.nextInt(ALTO - 5) + 1;
direccionesX[i] = random.nextBoolean() ? 1 : -1;
}
// Inicializar burbujas
for (int i = 0; i < burbujasX.length; i++) {
burbujasX[i] = random.nextInt(ANCHO - 2) + 1;
burbujasY[i] = random.nextInt(ALTO - 2) + 1;
}
Timer timer = new Timer(200, e -> {
moverPeces();
moverBurbujas();
repaint();
});
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
dibujarMar(g);
}
private void dibujarMar(Graphics g) {
g.setColor(Color.WHITE);
// Dibujar burbujas
for (int i = 0; i < burbujasX.length; i++) {
g.drawString("o", burbujasX[i] * 10, burbujasY[i] * 20);
}
// Dibujar peces
for (int i = 0; i < PECES.length; i++) {
for (int j = 0; j < PECES[i].length; j++) {
g.drawString(PECES[i][j], pecesX[i] * 10, (pecesY[i] + j) * 20);
}
}
// Dibujar algas al fondo
for (int i = 0; i < ALGAS.length; i++) {
g.drawString(ALGAS[i], 5, (ALTO * 20) - (ALGAS.length - i) * 15);
}
}
private void moverPeces() {
for (int i = 0; i < PECES.length; i++) {
pecesX[i] += direccionesX[i];
if (pecesX[i] + 10 >= ANCHO || pecesX[i] <= 0) {
direccionesX[i] *= -1;
}
}
}
private void moverBurbujas() {
for (int i = 0; i < burbujasY.length; i++) {
if (i % 2 == 0) {
burbujasY[i] -= 1; // Mover más lento
}
if (burbujasY[i] <= 0) {
burbujasY[i] = ALTO - 2;
burbujasX[i] = random.nextInt(ANCHO - 2) + 1;
}
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Pecera ASCII");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PeceraGUI());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}