Compare commits
2 Commits
c4ae2f9638
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| e11028d45f | |||
| 3f2eb9226b |
@@ -116,7 +116,7 @@ public class Arkanoid {
|
|||||||
List<Actor> actores = new ArrayList<Actor>();
|
List<Actor> actores = new ArrayList<Actor>();
|
||||||
int anchoJugador = 55;
|
int anchoJugador = 55;
|
||||||
int altoJugador = 15;
|
int altoJugador = 15;
|
||||||
actores.add(new Jugador(anchoJugador, altoJugador, (anchoVentana - anchoJugador) / 2, altoVentana - (altoVentana / 9), ResourceCache.IMAGEN_JUGADOR));
|
actores.add(new Jugador(anchoJugador, altoJugador, (anchoVentana - anchoJugador) / 2, altoVentana - (altoVentana / 9)));
|
||||||
|
|
||||||
int diametroPelota = 20;
|
int diametroPelota = 20;
|
||||||
actores.add(new Pelota(diametroPelota, (anchoVentana - diametroPelota) / 2, 400, ResourceCache.IMAGEN_PELOTA));
|
actores.add(new Pelota(diametroPelota, (anchoVentana - diametroPelota) / 2, 400, ResourceCache.IMAGEN_PELOTA));
|
||||||
@@ -124,7 +124,7 @@ public class Arkanoid {
|
|||||||
|
|
||||||
for (int i = 0; i < ResourceCache.IMAGEN_LADRILLO.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, ResourceCache.IMAGEN_LADRILLO));
|
actores.add(new Ladrillo(anchoLadrillos, 15, i, j, separacionBloques, altoVentana / 12, ResourceCache.IMAGEN_LADRILLO));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,20 +1,25 @@
|
|||||||
package net.h4ckx0r;
|
package net.h4ckx0r;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.sound.sampled.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class ResourceCache {
|
public class ResourceCache {
|
||||||
|
|
||||||
public static final String IMAGEN_JUGADOR = "Player1.png";
|
|
||||||
public static final String IMAGEN_PELOTA = "Pelota.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" };
|
public static final String[] IMAGEN_LADRILLO = new String[] {"LadrilloRojo.png", "LadrilloNaranja.png", "LadrilloAmarillo.png", "LadrilloVerde.png", "LadrilloAzul.png" };
|
||||||
|
|
||||||
|
|
||||||
|
public static final String SONIDO_EXPLOSION = "explosion.wav";
|
||||||
|
|
||||||
private HashMap<String, BufferedImage> cacheImagenes = new HashMap<>();
|
private HashMap<String, BufferedImage> cacheImagenes = new HashMap<>();
|
||||||
|
|
||||||
|
private HashMap<String, Clip> cacheSonidos = new HashMap<>();
|
||||||
|
|
||||||
private static ResourceCache instance = null;
|
private static ResourceCache instance = null;
|
||||||
|
|
||||||
public static ResourceCache getInstance() {
|
public static ResourceCache getInstance() {
|
||||||
@@ -45,4 +50,39 @@ public class ResourceCache {
|
|||||||
}
|
}
|
||||||
return img;
|
return img;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Clip getSound(String nombreSonido) {
|
||||||
|
Clip clip = cacheSonidos.get(nombreSonido);
|
||||||
|
if (clip == null) {
|
||||||
|
try {
|
||||||
|
URL sonido = getClass().getResource("/sonidos/" + nombreSonido);
|
||||||
|
AudioInputStream audioStream = AudioSystem.getAudioInputStream(sonido);
|
||||||
|
clip = AudioSystem.getClip();
|
||||||
|
clip.open(audioStream);
|
||||||
|
cacheSonidos.put(nombreSonido, clip);
|
||||||
|
} catch (IOException | UnsupportedAudioFileException | LineUnavailableException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return clip;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reproducirSonido(String sonido) {
|
||||||
|
new Thread(() -> {
|
||||||
|
try {
|
||||||
|
Clip clip = getSound(sonido);
|
||||||
|
if (clip == null) return;
|
||||||
|
|
||||||
|
clip.setFramePosition(0);
|
||||||
|
clip.start();
|
||||||
|
|
||||||
|
while (clip.isRunning()) {
|
||||||
|
Thread.sleep(10);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,13 @@ public abstract class Actor {
|
|||||||
this.sprite = ResourceCache.getInstance().getImage(imgName);
|
this.sprite = ResourceCache.getInstance().getImage(imgName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Actor(int x, int y, int ancho, int alto) {
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
|
this.ancho = ancho;
|
||||||
|
this.alto = alto;
|
||||||
|
}
|
||||||
|
|
||||||
public void paint(Graphics g) {
|
public void paint(Graphics g) {
|
||||||
g.drawImage(this.sprite, this.x, this.y, this.ancho, this.alto, null);
|
g.drawImage(this.sprite, this.x, this.y, this.ancho, this.alto, null);
|
||||||
}
|
}
|
||||||
|
|||||||
47
src/main/java/net/h4ckx0r/actores/Explosion.java
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package net.h4ckx0r.actores;
|
||||||
|
|
||||||
|
import net.h4ckx0r.Arkanoid;
|
||||||
|
import net.h4ckx0r.ResourceCache;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Explosion extends Actor{
|
||||||
|
|
||||||
|
List<BufferedImage> spritesAnimacion = new ArrayList<>();
|
||||||
|
int velocidadCambioSprite = 5;
|
||||||
|
int tiempoSprites = 0;
|
||||||
|
|
||||||
|
public Explosion(int x, int y, int ancho, int alto) {
|
||||||
|
super(x, y, ancho, alto);
|
||||||
|
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("explosion/sprite-explosion2.png"));
|
||||||
|
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("explosion/sprite-explosion1.png"));
|
||||||
|
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("explosion/sprite-explosion3.png"));
|
||||||
|
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("explosion/sprite-explosion4.png"));
|
||||||
|
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("explosion/sprite-explosion5.png"));
|
||||||
|
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("explosion/sprite-explosion6.png"));
|
||||||
|
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("explosion/sprite-explosion7.png"));
|
||||||
|
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("explosion/sprite-explosion8.png"));
|
||||||
|
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("explosion/sprite-explosion9.png"));
|
||||||
|
this.sprite = this.spritesAnimacion.getFirst();
|
||||||
|
|
||||||
|
ResourceCache.getInstance().reproducirSonido(ResourceCache.SONIDO_EXPLOSION);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actua() {
|
||||||
|
if (!this.spritesAnimacion.isEmpty()) {
|
||||||
|
tiempoSprites++;
|
||||||
|
if (tiempoSprites % velocidadCambioSprite == 0){
|
||||||
|
tiempoSprites = 0;
|
||||||
|
int indiceSpriteActual = spritesAnimacion.indexOf(this.sprite);
|
||||||
|
int indiceSiguienteSprite = (indiceSpriteActual + 1) % spritesAnimacion.size();
|
||||||
|
this.sprite = spritesAnimacion.get(indiceSiguienteSprite);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this.sprite.equals(this.spritesAnimacion.getLast())) {
|
||||||
|
Arkanoid.getInstance().eliminaActor(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,12 @@
|
|||||||
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;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Jugador extends Actor {
|
public class Jugador extends Actor {
|
||||||
|
|
||||||
@@ -11,8 +15,16 @@ public class Jugador extends Actor {
|
|||||||
|
|
||||||
private int velocidadX = 4;
|
private int velocidadX = 4;
|
||||||
|
|
||||||
public Jugador(int ancho, int alto, int initialX, int initialY, String imgName) {
|
List<BufferedImage> spritesAnimacion = new ArrayList<>();
|
||||||
super(initialX, initialY, ancho, alto, imgName);
|
int velocidadCambioSprite = 5;
|
||||||
|
int tiempoSprites = 0;
|
||||||
|
|
||||||
|
public Jugador(int ancho, int alto, int initialX, int initialY) {
|
||||||
|
super(initialX, initialY, ancho, alto);
|
||||||
|
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("Player1.png"));
|
||||||
|
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("Player2.png"));
|
||||||
|
this.spritesAnimacion.add(ResourceCache.getInstance().getImage("Player3.png"));
|
||||||
|
this.sprite = this.spritesAnimacion.getFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -24,6 +36,17 @@ public class Jugador extends Actor {
|
|||||||
if (this.x < 0) this.x = 0;
|
if (this.x < 0) this.x = 0;
|
||||||
if (this.x + this.ancho > Arkanoid.getInstance().getCanvas().getWidth())
|
if (this.x + this.ancho > Arkanoid.getInstance().getCanvas().getWidth())
|
||||||
this.x = Arkanoid.getInstance().getCanvas().getWidth() - this.ancho;
|
this.x = Arkanoid.getInstance().getCanvas().getWidth() - this.ancho;
|
||||||
|
|
||||||
|
|
||||||
|
if (!this.spritesAnimacion.isEmpty()) {
|
||||||
|
tiempoSprites++;
|
||||||
|
if (tiempoSprites % velocidadCambioSprite == 0){
|
||||||
|
tiempoSprites = 0;
|
||||||
|
int indiceSpriteActual = spritesAnimacion.indexOf(this.sprite);
|
||||||
|
int indiceSiguienteSprite = (indiceSpriteActual + 1) % spritesAnimacion.size();
|
||||||
|
this.sprite = spritesAnimacion.get(indiceSiguienteSprite);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void movimientoRaton(int x, int y) {
|
public void movimientoRaton(int x, int y) {
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ public class Ladrillo extends Actor {
|
|||||||
public void colisionaCon(Actor a, Rectangle act1Rectangle, Rectangle act2Rectangle) {
|
public void colisionaCon(Actor a, Rectangle act1Rectangle, Rectangle act2Rectangle) {
|
||||||
if (a instanceof Pelota) {
|
if (a instanceof Pelota) {
|
||||||
Arkanoid.getInstance().eliminaActor(this);
|
Arkanoid.getInstance().eliminaActor(this);
|
||||||
|
Arkanoid.getInstance().incorporaActor(new Explosion(this.x, this.y, this.ancho, this.alto));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
src/main/resources/images/explosion/sprite-explosion1.png
Normal file
|
After Width: | Height: | Size: 327 B |
BIN
src/main/resources/images/explosion/sprite-explosion2.png
Normal file
|
After Width: | Height: | Size: 467 B |
BIN
src/main/resources/images/explosion/sprite-explosion3.png
Normal file
|
After Width: | Height: | Size: 658 B |
BIN
src/main/resources/images/explosion/sprite-explosion4.png
Normal file
|
After Width: | Height: | Size: 841 B |
BIN
src/main/resources/images/explosion/sprite-explosion5.png
Normal file
|
After Width: | Height: | Size: 1016 B |
BIN
src/main/resources/images/explosion/sprite-explosion6.png
Normal file
|
After Width: | Height: | Size: 1010 B |
BIN
src/main/resources/images/explosion/sprite-explosion7.png
Normal file
|
After Width: | Height: | Size: 1.1 KiB |
BIN
src/main/resources/images/explosion/sprite-explosion8.png
Normal file
|
After Width: | Height: | Size: 991 B |
BIN
src/main/resources/images/explosion/sprite-explosion9.png
Normal file
|
After Width: | Height: | Size: 884 B |