# Objekte verschwinden durch Explosion



## Mr. Moon (21. Sep 2013)

Hallo, ich sitze immer noch an meinem Bomberman Klon und habe nun einen Fehler, den ich mir nicht erklären Kann.  Ich kann nun mit meiner Figur über das Spielfeld laufen und Bomben legen, die explodieren. Außerdem gibt es feste Blöcke, durch die man nicht durchlaufen kann. Nun habe ich etwas Code geschrieben, damit die Bombe immer zentriert auf einem 75*75 Feld gelegt wird. Doch mit Diesem tritt ein merkwürdiger Fehler auf. Die Explosionsgrafiken werden nicht mehr korrekt dargestellt,d as Mittelteil fehlt und die festen Blöcke werden durch die Explosion einfach weggesprengt bzw. verschwinden einfach nachdem eine Explosion daneben stattgefunden hat.

Hier der Code, den ich neu eingefügt habe(Der auskommentierte Teil ist, wie es zuvor war) :


```
private void dropBomb(Player player) {
		
		for(int i = 0; i <= 600; i = i + 75) {
			for(int j = 0; j <= 750; j = j + 75) {
				if(((j / 75) % 2 != 0) && ((i / 75) % 2 != 0)) {
					continue;
				}
				
				if((player.getX() >= j && player.getX() < j + 75) && (player.getY() + 80 >= i && player.getY() + 80 < i + 75)) {
					bomb = new Bomb(bombpics, j , i, 100, this);
					actors.add(bomb);
				}
				
			}
		}
		
		//bomb = new Bomb(bombpics, player.getX() , player.getY() + 25, 100, this);
		//actors.add(bomb);
		
	}
```

Hier der Code meines Programmes:


```
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Vector;

public class GamePanel extends JPanel implements Runnable, KeyListener {
    
	private static final long serialVersionUID = 1L;
	boolean game_running = true;
	
	long delta = 0;
	long last  = 0;
	long fps   = 0;
	
	boolean up    = false;
	boolean down  = false;
	boolean left  = false;
	boolean right = false;
	boolean drop  = false;
	
	int speed = 50;
	int h = 0;
	int fire = 3;
	Bomb bomb;
	Player player;
	Explosion explosion;
	Destructble_Block[] desBlocks;
	Sprite[] blocks;
	Vector<Sprite> actors;
	BufferedImage[] bombpics;
	BufferedImage[] explopics;
	BufferedImage[] destructblepics;
	
	public GamePanel(int w, int h) {
		this.setPreferredSize(new Dimension(w,h));
		JFrame frame = new JFrame("BomberMJ");
		frame.setLocation(100,100);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.addKeyListener(this);
		frame.add(this);
		frame.pack();
		frame.setVisible(true);
		doInitializations();
	}
	
	public int getFire() {
		return fire;
	}
	
	public Sprite[] getBlocks() {
		return blocks;
	}
	
	public BufferedImage[] getExplopics() {
		return explopics;
	}
	
	private void doInitializations() {
	    int k = 0;
		
		last = System.nanoTime();
		
	    actors = new Vector<Sprite>();
	    BufferedImage[] mjpics = this.loadPics("pics/MJ.gif", 36);
	    BufferedImage[] blockpics = this.loadPics("pics/Block.gif", 1);
	    explopics = this.loadPics("pics/Explosion.gif", 18);
	    bombpics = this.loadPics("pics/Bomb.gif", 2);
	    destructblepics = this.loadPics("pics/destructble_Block.gif", 5);
        
	    desBlocks = new Destructble_Block[40];
	    blocks = new Sprite[20];
	    
	    //setDestructbleBlocks();
	    
	    for(int i = 1; i < 8; i = i + 2) {
	    	
	    	for(int j = 1; j < 10; j = j + 2) {
	    		
	    		if(k < blocks.length ) {
	    			blocks[k] = new Block(blockpics, j * 75 , i * 75, 100, this);
	    			actors.add(blocks[k]);
	    			
	    			k++;
	    		}
	    			
	    	}
	    	
	    }
	    
	    
	    player = new Player(mjpics, 0, 0, 100, this);
	    player.setLoop(0,0);
	    
	    actors.add(player);
		Thread t = new Thread(this);
	    t.start();
	}
	
	private BufferedImage[] loadPics(String path, int pics) {
		
		BufferedImage[] anim = new BufferedImage[pics];
		BufferedImage source = null;
		
		URL pic_url = getClass().getClassLoader().getResource(path);
		
		try {
			source = ImageIO.read(pic_url);
		}
		
		catch(IOException e) {
			
		}
		
		for(int x = 0; x < pics; x++) {
			anim[x] = source.getSubimage(x * source.getWidth() / pics, 0, source.getWidth() / pics, source.getHeight());
		}
		
		return anim;
	}
	
	
	private void setDestructbleBlocks() {
		
		int[][] a = new int[2][99];
		boolean[] b = new boolean[99];
		int k = 0;
		int l = 0;
		int m = 0;
		int n = 0;
		
		for(int i = 0; i <= 600; i = i + 75) {
			for(int j = 0; j <= 750; j = j + 75) {
				if(((j / 75) % 2 != 0) && ((i / 75) % 2 != 0)) {
					continue;
				}
				
				a[0][k] = j;
				a[1][k] = i;
				k++;
			}
		}
		
		while(l < 40) {
			m = (int) (Math.random() * 98);
			
			if(!b[m]) {
				desBlocks[l] = new Destructble_Block(destructblepics, a[0][m], a[1][m], 100, this);
				desBlocks[l].setLoop(0, 0);
				actors.add(desBlocks[l]);
				b[m] = true;
				l++;
			}
		}
		
		
	}
	
	private void dropBomb(Player player) {
		
		for(int i = 0; i <= 600; i = i + 75) {
			for(int j = 0; j <= 750; j = j + 75) {
				if(((j / 75) % 2 != 0) && ((i / 75) % 2 != 0)) {
					continue;
				}
				
				if((player.getX() >= j && player.getX() < j + 75) && (player.getY() + 80 >= i && player.getY() + 80 < i + 75)) {
					bomb = new Bomb(bombpics, j , i, 100, this);
					actors.add(bomb);
				}
				
			}
		}
		
		//bomb = new Bomb(bombpics, player.getX() , player.getY() + 25, 100, this);
		//actors.add(bomb);
		
	}
	
	private void computeDelta() {
		delta = System.nanoTime() - last;
		last  = System.nanoTime();
		
		fps = ((long) 1e9) / delta;
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		g.setColor(Color.red);
		g.drawString("FPS: " + Long.toString(fps), 20, 10);
		
		if(actors != null) {
			for(Drawable draw : actors) {
				draw.drawObjects(g);
			}
		}
	}
	
	private void doLogic() {
		
		Vector<Sprite> trash = new Vector<Sprite>();
		Sprite check, s1, s2;
		
		for(Movable mov : actors) {
			mov.doLogic(delta);
			check = (Sprite) mov;
			
			if(check.remove) {
				trash.add(check);
			}
		}
		
		for(int i = 0; i < actors.size(); i++) {
			for(int n = 0; n < actors.size(); n++) {
				
				if(i != n) {
					s1 = actors.elementAt(i);
					s2 = actors.elementAt(n);
					//System.out.println(i + "," + n);
					s1.collidedWith(s2);
				}
			    
			}
		}
		
		if(trash.size() > 0) {
			actors.removeAll(trash);
			trash.clear();
		}
	}
	
	private void moveObjects() {
		for(Movable mov : actors) {
			mov.move(delta);
		}
	}
	
	private void checkKeys() {
		if(drop) {
			dropBomb(player);
			drop = false;
		}
		
		if(up) {
			player.setVerticalSpeed(-speed);
			player.setDirection('d');
		}
		
		if(down) {
			player.setVerticalSpeed(speed);
			player.setDirection('u');
		}
		
		if(right) {
			player.setHorizontalSpeed(speed);
			player.setDirection('l');
		}
		
		if(left) {
			player.setHorizontalSpeed(-speed);
			player.setDirection('r');
		}
		
		if(!up && !down) {
			player.setVerticalSpeed(0);
		}
		
		if(!left && !right) {
			player.setHorizontalSpeed(0);
		}
	}
	public void run() {
		while(game_running) {
		    computeDelta();
		    checkKeys();
		    doLogic();
		    moveObjects();
		    
		    repaint();
			
			
			try {
				Thread.sleep(10);
			}
			
			catch (InterruptedException e) {}
		}
	}
	public static void main(String[] args) {
		
		new GamePanel(825,675);
	}

	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
		/*if(e.getKeyCode() == KeyEvent.VK_SPACE) {
			drop = true;
			
		}*/
		
		if(e.getKeyCode() == KeyEvent.VK_UP) {
			up = true;
			
			if(h != 1) {
				player.setLoop(4, 7);
				h = 1;
			}
		}
		
		if(e.getKeyCode() == KeyEvent.VK_DOWN) {
			down = true;

            if(h != 2) {
            	player.setLoop(0, 3);
            	h = 2;
            }
		}
		
		if(e.getKeyCode() == KeyEvent.VK_LEFT) {
			left = true;
			
			if(h != 3) {
				player.setLoop(12, 15);
				h = 3;
			}
		}
		
		if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
			right = true;
			if(h != 4) {
				player.setLoop(8, 11);
				h = 4;
			}
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
		if(e.getKeyCode() == KeyEvent.VK_SPACE) {
			drop = true;
			
		}
		
		if(e.getKeyCode() == KeyEvent.VK_UP) {
			up = false;
			if(h != 0) {
				player.setLoop(1,1);
				h = 0;
			}
		}
		
		if(e.getKeyCode() == KeyEvent.VK_DOWN) {
			down = false;
			if(h != 0) {
				player.setLoop(1,1);
				h = 0;
			}
		}
		
		if(e.getKeyCode() == KeyEvent.VK_LEFT) {
			left = false;
			if(h != 0) {
				player.setLoop(1,1);
				h = 0;
			}
		}
		
		if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
			right = false;
			if(h != 0) {
				player.setLoop(1,1);
				h = 0;
			}
		}
		
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		/*if(e.getKeyCode() == KeyEvent.VK_SPACE) {
			drop = true;
			
		}*/
	}

	
	
}
```


```
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.Timer;



public class Bomb extends Sprite implements ActionListener {
	
	private boolean isUnderPlayer = true;
	Timer timer;
	Explosion[] ex = new Explosion[33];
	boolean stop = false;
	
	public Bomb(BufferedImage[] i, double x, double y, long delay, GamePanel p) {
		super(i, x, y, delay, p);
		
		timer = new Timer(3000, this);
    	timer.start();
	}
	
	public void setIsUnderPlayer(boolean d) {
		isUnderPlayer = d;
	}
	
	public boolean getIsUnderPlayer() {
		return isUnderPlayer;
	}
    public boolean collidedWith(Sprite s) {
		
		if(checkOpaqueColorCollisions(s)) {
			System.out.println("Kollision Bombe");
			return true;
		}
		
		return false;
	}
    
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource().equals(timer)  && !stop) {
			int k = 1;
			boolean[] collision = new boolean[4];
			
			collision[0] = false;
			collision[1] = false;
			collision[2] = false;
			collision[3] = false;
			stop   = true;
			remove = true;
			
			
			
			ex[0] = new Explosion(parent.getExplopics(), (int) getX(), (int) getY(), 100, parent, null);
			ex[0].setLoop(0,1);
						
			parent.actors.add(ex[0]);
			
			for(int i = 1; i <= parent.getFire() * 4; i = i + 4) {
				
				
				if(i == 1) {
				    ex[i] = new Explosion(parent.getExplopics(), (int) getX() - k*75, (int) getY(), 100, parent, ex[i-1]);
				    ex[i].setLoop(12, 13);
				    parent.actors.add(ex[i]);
				    
				    ex[i + 1] = new Explosion(parent.getExplopics(), (int) getX() + k*75, (int) getY(), 100, parent, ex[i-1]);
				    ex[i + 1].setLoop(6, 7);
				    parent.actors.add(ex[i + 1]);
				    
				    ex[i + 2] = new Explosion(parent.getExplopics(), (int) getX(), (int) getY() - k*75, 100, parent, ex[i-1]);
				    ex[i + 2].setLoop(10, 11);
				    parent.actors.add(ex[i + 2]);
				    
				    ex[i + 3] = new Explosion(parent.getExplopics(), (int) getX(), (int) getY() + k*75, 100, parent, ex[i-1]);
				    ex[i + 3].setLoop(8, 9);
				    parent.actors.add(ex[i + 3]);
				    
				    for(int j = 0; j < parent.getBlocks().length; j++ ) {
				    	if(ex[i].collidedWith(parent.getBlocks()[j])) {
				    		collision[0] = true;
				    	}
				    	
				    	if(ex[i + 1].collidedWith(parent.getBlocks()[j])) {
				    		collision[1] = true;
				    	}
				    	
				    	if(ex[i + 2].collidedWith(parent.getBlocks()[j])) {
				    		collision[2] = true;
				    	}
				    	
				    	if(ex[i + 3].collidedWith(parent.getBlocks()[j])) {
				    		collision[3] = true;
				    	}
				    }
				    
				    k++;
				}
				
				else {
					
					if(!collision[0]) {
					    ex[i] = new Explosion(parent.getExplopics(), (int) getX() - k*75, (int) getY(), 100, parent, ex[i-4]);
				        ex[i].setLoop(12, 13);
				        ex[i].setCorrectPics();
				        ex[i - 4].setExlast(ex[i]);
				        parent.actors.add(ex[i]);
					}
					
					if(!collision[1]) {
				        ex[i + 1] = new Explosion(parent.getExplopics(), (int) getX() + k*75, (int) getY(), 100, parent, ex[i-3]);
				        ex[i + 1].setLoop(6, 7);
				        ex[i + 1].setCorrectPics();
				        ex[i - 3].setExlast(ex[i + 1]);
				        parent.actors.add(ex[i + 1]);
					}
					
					if(!collision[2]) {
				        ex[i + 2] = new Explosion(parent.getExplopics(), (int) getX(), (int) getY() - k*75, 100, parent, ex[i-2]);
				        ex[i + 2].setLoop(10, 11);
				        ex[i + 2].setCorrectPics();
				        ex[i - 2].setExlast(ex[i + 2]);
				        parent.actors.add(ex[i + 2]);
					}
					
					if(!collision[3]) {
				        ex[i + 3] = new Explosion(parent.getExplopics(), (int) getX(), (int) getY() + k*75, 100, parent, ex[i-1]);
				        ex[i + 3].setLoop(8, 9);
				        ex[i + 3].setCorrectPics();
				        ex[i - 1].setExlast(ex[i + 3]);
				        parent.actors.add(ex[i + 3]);
					}
					
					else{
						break;
					}
				    
					for(int j = 0; j < parent.getBlocks().length; j++ ) {
				    	if(ex[i].collidedWith(parent.getBlocks()[j])) {
				    		collision[0] = true;
				    	}
				    	
				    	if(ex[i + 1].collidedWith(parent.getBlocks()[j])) {
				    		collision[1] = true;
				    	}
				    	
				    	if(ex[i + 2].collidedWith(parent.getBlocks()[j])) {
				    		collision[2] = true;
				    	}
				    	
				    	if(ex[i + 3].collidedWith(parent.getBlocks()[j])) {
				    		collision[3] = true;
				    	}
				    }
					 
					k++;
				}
				
			}
			
			
			
		}
	}
}
```


```
import java.awt.image.BufferedImage;


public class Block extends Sprite {
    
	public Block(BufferedImage[] i, double x, double y, long delay, GamePanel p) {
		super(i, x, y, delay, p);
	}
	
	public boolean collidedWith(Sprite s) {
		
		if(checkOpaqueColorCollisions(s)) {
			System.out.println("Kollision Block");
			return true;
		}
		
		return false;		
	}
}
```


```
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.Timer;


public class Explosion extends Sprite implements ActionListener {
    
	private Explosion exfirst, exlast;
	private Timer timer;

	public Explosion(BufferedImage[] i, double x, double y, long delay, GamePanel p, Explosion ex) {
		super(i, x, y, delay, p);
		
		this.exfirst = ex;
		
		timer = new Timer(2000, this);
		timer.start();
	}
	
	public void setExlast(Explosion ex) {
		exlast  = ex;
	}
	
	public Explosion getExfirst() {
		return exfirst;
	}
	
	public Explosion getExlast() {
		return exlast;
	}
	
	public void setCorrectPics() {
		
		if(exfirst != null && (loop_from == 6 || loop_from == 12)) {
			
			if(exfirst.getExfirst() != null) {
				exfirst.setLoop(2, 3);
			}
			
		}
		
		if(exfirst != null && (loop_from == 8 || loop_from == 10)) {

			if(exfirst.getExfirst() != null) {
				exfirst.setLoop(4, 5);
			}
		}
	}
	
    public boolean collidedWith(Sprite s) {
		
		if(checkOpaqueColorCollisions(s)) {
			System.out.println("Kollision Explosion");
			
			if(s instanceof Block) {
				
				
				if(exfirst != null && loop_from != 0) {
					setRemove(true);
					
				}
				
			}
			return true;
		}
		
		return false;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource().equals(timer)) {
			remove = true;
		}
	}
}
```


```
import  java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

public abstract class Sprite extends Rectangle2D.Double implements Drawable, Movable {

	long delay;
	long animation = 0;
	protected double dx;
	protected double dy;
	GamePanel parent;
	BufferedImage[] pics;
	int currentpic = 0;
	boolean remove = false;
	int loop_from;
	int loop_to;
	char direction = 'n';
	
	public Sprite(BufferedImage[] i, double x, double y, long delay, GamePanel p) {
		pics        = i;
		this.x      = x;
		this.y      = y;
		this.delay  = delay;
		this.width  = pics[0].getWidth();
		this.height = pics[0].getHeight();
		parent      = p;
		loop_from   = 0;
		loop_to     = pics.length - 1;
	}
	
	public void setX(double i) {
		x = i;
	}
	
	public void setY(double i) {
		y = i;
	}
	
	public void setVerticalSpeed(double d) {
		dy = d;
	}
	
	public void setHorizontalSpeed(double d) {
		dx = d;
	}
	
	public void setDirection(char direction) {
		this.direction = direction;
	}
	
	public void setRemove(boolean d) {
		remove = d;
	}
	
	public double getVerticalSpeed() {
		return dy;
	}
	
	public double getHorizontalSpeed() {
		return dx;
	}
	
	public abstract boolean collidedWith(Sprite s);
	
	public boolean checkOpaqueColorCollisions(Sprite s) {
		
		Rectangle2D.Double cut = (Double) this.createIntersection(s);
		
		if((cut.width < 1) || (cut.height < 1)) {
			return false;
		}
		
		Rectangle2D.Double sub_me  = getSubRec(this, cut);
		Rectangle2D.Double sub_him = getSubRec(s, cut);
		System.out.println("");
		BufferedImage img_me = pics[currentpic].getSubimage((int) sub_me.x, (int) sub_me.y, (int) sub_me.width, (int) sub_me.height);
		BufferedImage img_him = s.pics[s.currentpic].getSubimage((int) sub_him.x, (int) sub_him.y, (int) sub_him.width, (int) sub_him.height);
		
		for(int i = 0; i < img_me.getWidth(); i++) {
			for(int n = 0; n < img_him.getHeight(); n++) {
				
				int rgb1 = img_me.getRGB(i, n);
				int rgb2 = img_him.getRGB(i, n);
				
				if(isOpaque(rgb1) && isOpaque(rgb2)) {
					return true;
				}
			}
		}
		
		return false;
	}
	
	protected Rectangle2D.Double getSubRec(Rectangle2D.Double source, Rectangle2D.Double part) {
		
		Rectangle2D.Double sub = new Rectangle2D.Double();
		
		if(source.x > part.x) {
			sub.x = 0;
		}
		
		else {
			sub.x = part.x - source.x;
		}
		
		if(source.y > part.y) {
			sub.y = 0;
		}
		
		else {
			sub.y = part.y - source.y;
		}
		
		sub.width  = part.width;
		sub.height = part.height;
		
		return sub;
	}
	
	protected boolean isOpaque(int rgb) {
		
		int alpha = (rgb >> 24) & 0xff;
		//red   = (rgb >> 16) & 0xff;
		//green   = (rgb >> 8) & 0xff;
		//blue   = (rgb) & 0xff;
		
		if(alpha == 0) {
			return false;
		}
		
		return true;
		
	}
	
	public void drawObjects(Graphics g) {
		g.drawImage(pics[currentpic], (int) x, (int) y, null);
	}
	
	public void doLogic(long delta) {
	
		animation += (delta / 1000000);
		
		if(animation > delay) {
			animation = 0;
			computeAnimation();
		}
		
	}
	
	public void move(long delta) {
		
		if(dx != 0) {
			x += dx * (delta / 1e9);
		}
		
		if(dy != 0) {
			y += dy * (delta / 1e9);
		}
	}
	
	private void computeAnimation() {
		
		currentpic++;
		
		if(currentpic > loop_to) {
			currentpic = loop_from;
		}
	}
	
	public void setLoop(int from, int to) {
		loop_from  = from;
		loop_to    = to;
		currentpic = from;	
	}
}
```


----------



## eMmiE (2. Okt 2013)

HI,
ich halte es (im Verfahren) für wenig sinnvoll, Leute, die helfen wollen, erstmal den ganzen Code an den Kopf zu ballern.

Deshalb:
Erklär doch erstmal, wie dein Spiel abläuft, welche Schirtte es macht, um zum Ziel (eine Bombe zu legen) zu kommen.

DANN... kannst du schrittweise unter Berücksichtigung des geistigen Zustands der Leute auf der anderen Seite des Internets Code-Schnippsel posten...

Gruß eMmiE

P.S.: Informatiker sind faul...


----------



## MrClave (7. Okt 2013)

Und bitte nächstes mal alles in Spoiler verfrachten.
Das erspart langes scrollen...

Starttag: 



Spoiler: NAME



Endtag: '['/SPOILER] (ohne " ' ")


----------

