Compare commits

...

2 Commits

Author SHA1 Message Date
c4ae2f9638 Imágenes 2025-02-16 22:31:23 +01:00
e6ab87defa "Vsync" simulado para evitar el shutering 2025-02-11 13:58:10 +01:00
17 changed files with 219 additions and 76 deletions

View File

@@ -27,6 +27,8 @@ public class Arkanoid {
private static int anchoVentana = separacionBloques + ((anchoLadrillos + separacionBloques) * numLadrillosPorLinea); private static int anchoVentana = separacionBloques + ((anchoLadrillos + separacionBloques) * numLadrillosPorLinea);
private List<Actor> actores = crearActores(); private List<Actor> actores = crearActores();
private List<Actor> actoresParaEliminar = new ArrayList<Actor>();
private List<Actor> actoresParaIncorporar = new ArrayList<Actor>();
private JFrame ventana = new JFrame("Arkanoid - Natanael Gómez Ortiz"); private JFrame ventana = new JFrame("Arkanoid - Natanael Gómez Ortiz");
private MiCanvas canvas = new MiCanvas(actores); private MiCanvas canvas = new MiCanvas(actores);
@@ -112,17 +114,17 @@ public class Arkanoid {
public static List<Actor> crearActores() { public static List<Actor> crearActores() {
List<Actor> actores = new ArrayList<Actor>(); List<Actor> actores = new ArrayList<Actor>();
int anchoJugador = 40; int anchoJugador = 55;
int altoJugador = 10; int altoJugador = 15;
actores.add(new Jugador(anchoJugador, altoJugador, (anchoVentana - anchoJugador) / 2, altoVentana - (altoVentana / 9))); actores.add(new Jugador(anchoJugador, altoJugador, (anchoVentana - anchoJugador) / 2, altoVentana - (altoVentana / 9), ResourceCache.IMAGEN_JUGADOR));
int diametroPelota = 20; int diametroPelota = 20;
actores.add(new Pelota(diametroPelota, (anchoVentana - diametroPelota) / 2, 400)); actores.add(new Pelota(diametroPelota, (anchoVentana - diametroPelota) / 2, 400, ResourceCache.IMAGEN_PELOTA));
for (int i = 0; i < Ladrillo.colores.length; i++) { for (int i = 0; i < ResourceCache.IMAGEN_LADRILLO.length; i++) {
for (int j = 0; j < numLadrillosPorLinea; j++) { for (int j = 0; j < numLadrillosPorLinea; j++) {
actores.add(new Ladrillo(anchoLadrillos, 10, i, j, separacionBloques, altoVentana/12)); actores.add(new Ladrillo(anchoLadrillos, 10, i, j, separacionBloques, altoVentana / 12, ResourceCache.IMAGEN_LADRILLO));
} }
} }
@@ -139,10 +141,15 @@ public class Arkanoid {
long tiempoInicio = new Date().getTime(); long tiempoInicio = new Date().getTime();
if (!pausado) { if (!pausado) {
for (Actor actor : actores) {
actor.setFrameTerminado(false);
}
for (Actor actor : actores) { for (Actor actor : actores) {
actor.actua(); actor.actua();
} }
canvas.repaint(); detectarColisiones();
actualizarActores();
canvas.pintar();
} }
long tiempoTranscurrido = new Date().getTime() - tiempoInicio; long tiempoTranscurrido = new Date().getTime() - tiempoInicio;
@@ -168,6 +175,41 @@ public class Arkanoid {
} }
} }
private void detectarColisiones() {
for (Actor act1 : this.actores) {
Rectangle act1Rectangle = new Rectangle(act1.getX(), act1.getY(), act1.getAncho(), act1.getAlto());
for (Actor act2 : this.actores) {
if (!act1.equals(act2)) {
Rectangle act2Rectangle = new Rectangle(act2.getX(), act2.getY(), act2.getAncho(), act2.getAlto());
if (act1Rectangle.intersects(act2Rectangle)) {
act1.colisionaCon(act2, act1Rectangle, act2Rectangle);
act2.colisionaCon(act1, act2Rectangle, act1Rectangle);
}
}
}
}
}
public void incorporaActor(Actor a) {
this.actoresParaIncorporar.add(a);
}
public void eliminaActor(Actor a) {
this.actoresParaEliminar.add(a);
}
private void actualizarActores() {
this.actores.removeAll(actoresParaEliminar);
this.actoresParaEliminar.clear();
this.actores.addAll(actoresParaIncorporar);
this.actoresParaIncorporar.clear();
}
public static void main(String[] args) { public static void main(String[] args) {
Arkanoid.getInstance().bucleJuego(); Arkanoid.getInstance().bucleJuego();
} }

View File

@@ -3,11 +3,14 @@ package net.h4ckx0r;
import net.h4ckx0r.actores.Actor; import net.h4ckx0r.actores.Actor;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferStrategy;
import java.util.List; import java.util.List;
public class MiCanvas extends Canvas { public class MiCanvas extends Canvas {
List<Actor> actores = null; List<Actor> actores = null;
private BufferStrategy strategy = null;
private int[][] estrellitas = null; private int[][] estrellitas = null;
public MiCanvas(List<Actor> actores) { public MiCanvas(List<Actor> actores) {
@@ -24,9 +27,18 @@ public class MiCanvas extends Canvas {
} }
} }
@Override public void pintar() {
public void paint(Graphics g) {
this.setBackground(Color.BLACK); if (this.strategy == null) {
this.createBufferStrategy(2);
strategy = getBufferStrategy();
Toolkit.getDefaultToolkit().sync();
}
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0,0, this.getWidth(), this.getHeight());
for (int[] estrella : estrellitas) { for (int[] estrella : estrellitas) {
g.setColor(Color.GRAY); g.setColor(Color.GRAY);
@@ -36,6 +48,8 @@ public class MiCanvas extends Canvas {
for (Actor actor : actores) { for (Actor actor : actores) {
actor.paint(g); actor.paint(g);
} }
strategy.show();
} }

View File

@@ -0,0 +1,48 @@
package net.h4ckx0r;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
public class ResourceCache {
public static final String IMAGEN_JUGADOR = "Player1.png";
public static final String IMAGEN_PELOTA = "Pelota.png";
public static final String[] IMAGEN_LADRILLO = new String[] {"LadrilloRojo.png", "LadrilloNaranja.png", "LadrilloAmarillo.png", "LadrilloVerde.png", "LadrilloAzul.png", "LadrilloAzulOscuro.png" };
private HashMap<String, BufferedImage> cacheImagenes = new HashMap<>();
private static ResourceCache instance = null;
public static ResourceCache getInstance() {
if (instance == null) {
instance = new ResourceCache();
}
return instance;
}
private BufferedImage cargarImagen (String nombreImg) {
URL imgPath;
try {
imgPath = getClass().getResource(nombreImg);
return ImageIO.read(imgPath);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
return null;
}
public BufferedImage getImage (String nombreImg) {
BufferedImage img = cacheImagenes.get(nombreImg);
if (img == null) {
img = cargarImagen("/images/" + nombreImg);
cacheImagenes.put(nombreImg, img);
}
return img;
}
}

View File

@@ -1,11 +1,26 @@
package net.h4ckx0r.actores; package net.h4ckx0r.actores;
import net.h4ckx0r.ResourceCache;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage;
public abstract class Actor { public abstract class Actor {
int x, y, ancho, alto; int x, y, ancho, alto;
BufferedImage sprite;
boolean frameTerminado = false;
public abstract void paint(Graphics g); public Actor(int x, int y, int ancho, int alto, String imgName) {
this.x = x;
this.y = y;
this.ancho = ancho;
this.alto = alto;
this.sprite = ResourceCache.getInstance().getImage(imgName);
}
public void paint(Graphics g) {
g.drawImage(this.sprite, this.x, this.y, this.ancho, this.alto, null);
}
public abstract void actua(); public abstract void actua();
@@ -40,4 +55,16 @@ public abstract class Actor {
public void setAlto(int alto) { public void setAlto(int alto) {
this.alto = alto; this.alto = alto;
} }
public boolean isFrameTerminado() {
return frameTerminado;
}
public void setFrameTerminado(boolean frameTerminado) {
this.frameTerminado = frameTerminado;
}
public void colisionaCon(Actor a, Rectangle act1Rectangle, Rectangle act2Rectangle) {
}
} }

View File

@@ -11,17 +11,8 @@ public class Jugador extends Actor {
private int velocidadX = 4; private int velocidadX = 4;
public Jugador(int ancho, int alto, int initialX, int initialY) { public Jugador(int ancho, int alto, int initialX, int initialY, String imgName) {
this.ancho = ancho; super(initialX, initialY, ancho, alto, imgName);
this.alto = alto;
this.x = initialX;
this.y = initialY;
}
@Override
public void paint(Graphics g) {
g.setColor(Color.RED);
g.fillRect(this.x, this.y, this.ancho, this.alto);
} }
@Override @Override

View File

@@ -6,25 +6,14 @@ import java.awt.*;
public class Ladrillo extends Actor { public class Ladrillo extends Actor {
public static Color[] colores = new Color[]{Color.RED, Color.YELLOW, Color.PINK, Color.CYAN, Color.GREEN, Color.ORANGE};
int fila; public Ladrillo(int ancho, int alto, int fila, int columna, int separacion, int separacionSuperior, String[] imgNames) {
super(separacion + (columna * ancho) + (separacion * columna),
public Ladrillo(int ancho, int alto, int fila, int columna, int separacion, int separacionSuperior) { separacion + (fila * alto) + (separacion * fila) + separacionSuperior,
this.setAncho(ancho); ancho, alto, imgNames[fila]);
this.setAlto(alto);
this.fila = fila;
this.x = separacion + (columna * ancho) + (separacion * columna);
this.y = separacion + (fila * alto) + (separacion * fila) + separacionSuperior;
} }
@Override
public void paint(Graphics g) {
g.setColor(colores[fila]);
g.fillRect(this.x, this.y, this.ancho, this.alto);
}
@Override @Override
public void actua() { public void actua() {
@@ -32,13 +21,9 @@ public class Ladrillo extends Actor {
} }
@Override @Override
public String toString() { public void colisionaCon(Actor a, Rectangle act1Rectangle, Rectangle act2Rectangle) {
return "Ladrillo{" + if (a instanceof Pelota) {
"fila=" + fila + Arkanoid.getInstance().eliminaActor(this);
", x=" + x + }
", y=" + y +
", ancho=" + ancho +
", alto=" + alto +
'}';
} }
} }

View File

@@ -1,34 +1,70 @@
package net.h4ckx0r.actores; package net.h4ckx0r.actores;
import net.h4ckx0r.Arkanoid; import net.h4ckx0r.Arkanoid;
import net.h4ckx0r.ResourceCache;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage;
public class Pelota extends Actor { public class Pelota extends Actor {
private static int velocidadX = 5; private int velocidadX = 5;
private static int velocidadY = 2; private int velocidadY = 2;
public Pelota(int diametro, int initialX, int initialY) { public Pelota(int diametro, int initialX, int initialY, String imgName) {
this.ancho = diametro; super(initialX, initialY, diametro, diametro, imgName);
this.alto = diametro;
this.x = initialX;
this.y = initialY;
}
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillOval(this.x, this.y, this.ancho, this.alto);
} }
@Override @Override
public void actua() { public void actua() {
this.x += Pelota.velocidadX; this.x += this.velocidadX;
this.y += Pelota.velocidadY; this.y += this.velocidadY;
if (this.x + this.ancho >= Arkanoid.getInstance().getCanvas().getWidth() if (this.x + this.ancho >= Arkanoid.getInstance().getCanvas().getWidth()
|| this.x <= 0) Pelota.velocidadX = -Pelota.velocidadX; || this.x <= 0) this.velocidadX = -this.velocidadX;
if (this.y + this.alto >= Arkanoid.getInstance().getCanvas().getHeight() if (this.y + this.alto >= Arkanoid.getInstance().getCanvas().getHeight()
|| this.y <= 0) Pelota.velocidadY = -Pelota.velocidadY; || this.y <= 0) this.velocidadY = -this.velocidadY;
}
public void colisionaCon(Actor a, Rectangle rectPelota, Rectangle rectActor) {
if (a instanceof Jugador) {
Rectangle interseccion = rectPelota.intersection(rectActor);
if (!interseccion.isEmpty() && !frameTerminado) {
if (interseccion.getWidth() < interseccion.getHeight()) {
this.velocidadX = -this.velocidadX;
} else {
this.velocidadY = -this.velocidadY;
}
frameTerminado = true;
}
} else if (a instanceof Ladrillo) {
Rectangle interseccion = rectPelota.intersection(rectActor);
if (!interseccion.isEmpty() && !frameTerminado) {
if (interseccion.getWidth() < interseccion.getHeight()) {
this.velocidadX = -this.velocidadX;
} else {
this.velocidadY = -this.velocidadY;
}
frameTerminado = true;
}
}
}
@Override
public String toString() {
return "Pelota{" +
"velocidadX=" + velocidadX +
", velocidadY=" + velocidadY +
", x=" + x +
", y=" + y +
", ancho=" + ancho +
", alto=" + alto +
'}';
} }
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB