Hallo Freunde,
ich möchte mir ein Bubble-Breaker selber schreiben. An sich funzt das auch soweit, die Logik ist richtig implementiert. Man kann die Figuren (in dem Fall Rauten) auch anklicken, nur werden sie leider nicht gezeichnet. Hier jetzt erstmal der Code:
Hauptklasse:
Container der Rauten:
Rautenklasse:
Die Funktion in der die Rauten "nach unten fallen" - also die leeren Rauten nach oben getauscht werden - ist diese hier:
Ich habe keine Idee, ihr könnt das Spiel ja auch mal probieren. wie gesagt: die Logik ist schon richtig implementiert, nur muss man jetzt noch so extrem mitdenken da man ja nicht sieht wo die rauten grad sind.
Ich hoffe ihr könnt mir ahnungslosem helfen
LG von mir
ich möchte mir ein Bubble-Breaker selber schreiben. An sich funzt das auch soweit, die Logik ist richtig implementiert. Man kann die Figuren (in dem Fall Rauten) auch anklicken, nur werden sie leider nicht gezeichnet. Hier jetzt erstmal der Code:
Hauptklasse:
Java:
package bubbleCaro;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class Bubble extends JFrame{
public static Dimension screen;
private static GameField gf;
private static final long serialVersionUID = 1L;
public Bubble(){
super("Risiko");
screen = Toolkit.getDefaultToolkit().getScreenSize();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ESCAPE){
System.exit(0);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e){
}
});
gf = new GameField(screen);
Dimension d = new Dimension(gf.getWidth()+8, gf.getHeight()+58);
this.setSize(d);
JMenuBar mb = new JMenuBar();
JMenu file = new JMenu("Datei");
JMenuItem neu = new JMenuItem("Neues Spiel");
JMenuItem ext = new JMenuItem("Beenden");
file.add(neu);
file.add(ext);
mb.add(file);
this.add(mb, BorderLayout.NORTH);
this.add(gf, BorderLayout.CENTER);
this.setVisible(true);
}
public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Bubble();
}
});
}
}
Container der Rauten:
Java:
package bubbleCaro;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
public class GameField extends JPanel{
private int lang;
private int fieldCol = 15;
private int fieldRow = 14;
private Field[][] fields = new Field[fieldRow][fieldCol];
private static final long serialVersionUID = 1L;
public void changeToTopFrom(int i, int j){
for(int i2 = i; i2 > 0; i2--){
Field dwn = fields[i2-1][j];
Field up = fields[i2][j];
fields[i2][j] = dwn;
fields[i2-1][j] = up;
dwn.setBounds(j*this.lang, i2*this.lang, lang, lang);
up.setBounds(j*this.lang, (i2-1)*this.lang, lang, lang);
}
}
public void clearSelected(){
for(int i = 0; i < fields.length; i++){
for(int j = 0; j < fields[i].length; j++){
if(fields[i][j].isSelected()){
fields[i][j].clear();
}
}
}
repaint();
for(int i = 1; i < fields.length; i++){
for(int j = 0; j < fields[i].length; j++){
if(fields[i][j].isCleared()){
changeToTopFrom(i,j);
}
}
}
this.removeAll();
for(int i = 0; i < fields.length; i++){
for(int j = 0; j < fields[i].length; j++){
this.add(fields[i][j]);
}
}
repaint();
setNeighb();
}
public void resetAllBefore(){
for(int i = 0; i < fields.length; i++){
for(int j = 0; j < fields[i].length; j++){
if(fields[i][j]!=null&&fields[i][j].isSelected()){
fields[i][j].resetValidNeighb();
}
}
}
}
public void setNeighb(){
for(int i = 0; i < fields.length; i++){
for(int j = 0; j < fields[i].length; j++){
Field up = null;
Field left = null;
Field right = null;
Field dwn = null;
if(j > 0){
left = fields[i][j-1];
}
if(j < fields[i].length-1){
right = fields[i][j+1];
}
if(i > 0){
up = fields[i-1][j];
}
if(i < fields.length-1){
dwn = fields[i+1][j];
}
fields[i][j].setNeighbours(up, left, right, dwn);
}
}
}
public void initFields(){
for(int i = 0; i < fieldRow; i ++){
for(int j = 0; j < fieldCol; j++){
int color = (int)(Math.random()*5);
Field f = new Field(j*this.lang,i*this.lang, color, this);
f.setBounds(j*this.lang, i*this.lang, lang, lang);
this.add(f);
fields[i][j] = f;
}
}
setNeighb();
repaint();
}
@Override
public int getWidth(){
return this.fieldCol*lang;
}
@Override
public int getHeight(){
return this.fieldRow*lang;
}
public GameField(Dimension d){
super();
this.lang = (int)(d.getHeight()/(fieldRow+2));
this.setSize(fieldCol*lang, fieldRow*lang);
this.setBackground(Color.WHITE);
this.setLayout(null);
initFields();
this.setVisible(true);
}
}
Rautenklasse:
Java:
package bubbleCaro;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.GeneralPath;
import javax.swing.JComponent;
public class Field extends JComponent implements MouseListener, MouseMotionListener{
private static final long serialVersionUID = 1L;
private int x;
private int y;
private boolean selected = false;
private boolean cleared = false;
private int lang;
private Color origColor;
private Color color;
private GeneralPath p;
private Field north;
private Field left;
private Field right;
private Field south;
private GameField gf;
public Field(int x, int y, int color, GameField gf){
this.gf = gf;
this.x=x;
this.y=y;
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setVisible(true);
switch(color){
case 0: {
this.color = new Color(255, 0, 255);
this.origColor = new Color(255, 0, 255);
break;
}
case 1: {
this.color = Color.YELLOW;
this.origColor = Color.YELLOW;
break;
}
case 2: {
this.color = Color.RED;
this.origColor = Color.RED;
break;
}
case 3: {
this.color = new Color(0, 0, 200);
this.origColor = new Color(0, 0, 200);
break;
}
case 4: {
this.color = Color.GREEN;
this.origColor = Color.GREEN;
break;
}
}
}
public void setNorth(Field f){
this.north = f;
}
public void setSouth(Field f){
this.south = f;
}
public void setLeft(Field f){
this.left = f;
}
public void setRight(Field f){
this.right = f;
}
public boolean isSelected(){
return this.selected;
}
public boolean isCleared(){
return this.cleared;
}
public int min(int i, int j){
if(i < j){
return i;
}
else{
return j;
}
}
public void reset(){
this.selected = false;
this.setColor(origColor);
repaint();
}
public void setColor(Color col){
this.color = col;
}
public Color getOrigColor(){
return this.origColor;
}
public void setOrigColor(Color c){
this.origColor = c;
}
public void setCleared(boolean cl){
this.cleared = cl;
}
public void draw(Graphics g){
if(!this.cleared){
Graphics2D g2 = (Graphics2D)g;
g2.setColor(this.color);
p = new GeneralPath();
p.moveTo(0, this.getHeight()/2);
p.lineTo(this.getWidth()/2, 0); p.lineTo(this.getWidth(), this.getHeight()/2);
p.lineTo(this.getWidth()/2, this.getHeight()); p.lineTo(0, this.getHeight()/2);
g2.fill(p);
}
}
public int maxOf(int i, int j){
if(i > j){
return i;
}
else{
return j;
}
}
public void markValidNeighb(String s){
boolean more = false;
mark();
if(!s.equalsIgnoreCase("so")&&this.north!=null&&!this.north.isSelected()&&this.north.getOrigColor().equals(this.origColor)){
more = true;
north.markValidNeighb("no");
}
if(!s.equalsIgnoreCase("ri")&&this.left!=null&&!this.left.isSelected()&&this.left.getOrigColor().equals(this.origColor)){
more = true;
left.markValidNeighb("le");
}
if(!s.equalsIgnoreCase("le")&&this.right!=null&&!this.right.isSelected()&&this.right.getOrigColor().equals(this.origColor)){
more = true;
right.markValidNeighb("ri");
}
if(!s.equalsIgnoreCase("no")&&this.south!=null&&!this.south.isSelected()&&this.south.getOrigColor().equals(this.origColor)){
more = true;
south.markValidNeighb("so");
}
if(!more&&s.equalsIgnoreCase("")){
this.reset();
}
}
public void mark(){
this.selected = true;
int red = maxOf(this.origColor.getRed()-50, 0);
int green = maxOf(this.origColor.getGreen()-50, 0);
int blue = maxOf(this.origColor.getBlue()-50, 0);
this.setColor(new Color(red, green, blue));
repaint();
}
public void resetValidNeighb(){
this.reset();
if(this.north!=null&&this.north.isSelected()&&this.north.getOrigColor().equals(this.origColor)){
north.resetValidNeighb();
}
if(this.left!=null&&this.left.isSelected()&&this.left.getOrigColor().equals(this.origColor)){
left.resetValidNeighb();
}
if(this.right!=null&&this.right.isSelected()&&this.right.getOrigColor().equals(this.origColor)){
right.resetValidNeighb();
}
if(this.south!=null&&this.south.isSelected()&&this.south.getOrigColor().equals(this.origColor)){
south.resetValidNeighb();
}
}
@Override
public void mouseClicked(MouseEvent e){
if(!this.cleared){
if(p.contains(e.getPoint())&&!this.selected){
gf.resetAllBefore();
this.markValidNeighb("");
}
else if(p.contains(e.getPoint())&&this.selected){
gf.clearSelected();
}
}
repaint();
}
public void clear(){
this.selected = false;
this.cleared = true;
repaint();
}
@Override
public void paint(Graphics g){
draw(g);
}
@Override
public void mouseEntered(MouseEvent e){
}
@Override
public void mouseExited(MouseEvent e){
}
@Override
public void mousePressed(MouseEvent e){
}
@Override
public void mouseReleased(MouseEvent e){
}
public boolean isIn(Point p){
if(p.getX()<this.x+lang&&p.getX()>this.x){
if(p.getY()<this.y+lang&&p.getY()>this.y){
return true;
}
}
return false;
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public void setLang(int lang) {
this.lang = lang;
}
public int getLang() {
return lang;
}
public void setNeighbours(Field north, Field left, Field right, Field south){
this.north = north;
this.left = left;
this.right = right;
this.south = south;
}
@Override
public void mouseDragged(MouseEvent e){
}
@Override
public void mouseMoved(MouseEvent e){
}
}
Die Funktion in der die Rauten "nach unten fallen" - also die leeren Rauten nach oben getauscht werden - ist diese hier:
Java:
public void changeToTopFrom(int i, int j){
for(int i2 = i; i2 > 0; i2--){
Field dwn = fields[i2-1][j];
Field up = fields[i2][j];
fields[i2][j] = dwn;
fields[i2-1][j] = up;
dwn.setBounds(j*this.lang, i2*this.lang, lang, lang);
up.setBounds(j*this.lang, (i2-1)*this.lang, lang, lang);
}
}
public void clearSelected(){
for(int i = 0; i < fields.length; i++){
for(int j = 0; j < fields[i].length; j++){
if(fields[i][j].isSelected()){
fields[i][j].clear();
}
}
}
for(int i = 1; i < fields.length; i++){
for(int j = 0; j < fields[i].length; j++){
if(fields[i][j].isCleared()){
changeToTopFrom(i,j);
}
}
}
this.removeAll();
for(int i = 0; i < fields.length; i++){
for(int j = 0; j < fields[i].length; j++){
this.add(fields[i][j]);
}
}
repaint();
setNeighb();
}
Ich habe keine Idee, ihr könnt das Spiel ja auch mal probieren. wie gesagt: die Logik ist schon richtig implementiert, nur muss man jetzt noch so extrem mitdenken da man ja nicht sieht wo die rauten grad sind.
Ich hoffe ihr könnt mir ahnungslosem helfen
LG von mir