Hi ich bräuchte ein wenig Hilfe bei meinen Problem.
Ich will das mein Blöcke sich übereinander stapelt aber irgentwas stimmt nicht mit meinen beiden For schleifen ich denke ich habe einen Falschen Denkansatz ich hoffe ihr könnt mir helfen
Block Class
Main Class
Ich will das mein Blöcke sich übereinander stapelt aber irgentwas stimmt nicht mit meinen beiden For schleifen ich denke ich habe einen Falschen Denkansatz ich hoffe ihr könnt mir helfen
Block Class
Code:
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Block {
private int x;
private int y;
private int breite;
private int höhe;
private boolean FallAktive;
private JFrame jf;
private JLabel sprite;
private Color color;
Block(int x, int y, Color color, JFrame jf){
this.x = x;
this.y = y;
this.breite = 20;
this.höhe = 20;
this.FallAktive = false;
this.jf = jf;
this.color = color;
AddBlock();
}
private void AddBlock(){
this.sprite = new JLabel();
this.sprite.setOpaque(true);
this.sprite.setSize(this.breite, this.höhe);
this.sprite.setBackground(color);
this.sprite.setLocation(this.x, this.y);
this.jf.add(this.sprite);
this.jf.repaint();
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getBreite() {
return breite;
}
public int getHöhe() {
return höhe;
}
public boolean isFallAktive() {
return FallAktive;
}
public void setFallAktive(boolean fallAktive) {
FallAktive = fallAktive;
}
public void move(){
if (this.FallAktive){
this.y = this.y + 2;
this.sprite.setLocation(this.x, this.y);
}
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
}
Main Class
Code:
import java.awt.Color;
import java.util.ArrayList;
import javax.swing.JFrame;
public class BlockKiller extends JFrame {
static JFrame jf;
static ArrayList<Block> block = new ArrayList<Block>();
BlockStapeler() {
jf = new JFrame();
jf.setVisible(true);
jf.setTitle("BlockStapler");
jf.setSize(100, 180);
jf.setOpacity(1);
jf.getContentPane().setBackground(Color.black);
jf.setResizable(false);
jf.setLayout(null);
jf.setLocation(300, 300);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static boolean blockcollision(Block block1, Block block2) {
if ((block1.getX() < block2.getX() + block2.getBreite())
&& (block1.getX() + block1.getX() > block2.getX())
&& (block1.getY() < block2.getY() + block2.getHöhe())
&& (block1.getHöhe() + 2 + block1.getY() > block2.getY()))
return true;
return false;
}
public static void main(String[] args) throws InterruptedException {
int[][] Feld = { { 0, 0, 3, 0, 0 },
{ 0, 0, 1, 1, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 } };
// System.exit(0);
new BlockKiller();
for (int i = 0; i < Feld[0].length; i++) {
for (int j = 0; j < Feld.length; j++) {
switch (Feld[j][i]) {
case 1:
block.add(new Block(i * 22, j * 20, Color.red, jf));
break;
case 2:
block.add(new Block(i * 22, j * 20, Color.green, jf));
break;
case 3:
block.add(new Block(i * 22, j * 20, Color.blue, jf));
break;
}
}
}
while (true) {
for (int i = 0; i < block.size(); i++) {
block.get(i).setFallAktive(true);
for (int j = i+1; j < block.size(); j++) {
if (i == j)continue;
if (blockcollision(block.get(i), block.get(j))) {
System.out.println("K");
block.get(j).setFallAktive(false);
break;
}
}
if (block.get(i).getY() >= 180 - block.get(i).getHöhe() - 29) {
block.get(i).setFallAktive(false);
}
block.get(i).move();
}
Thread.sleep(20);
}
}
}