Ich hab ein Spiel programmiert.Nun will ich , bevor das Spiel startet einen Startbildschirm programmieren , in dem man die Spielfeldgröße des Spiels festlegt .
Jedoch komm ich irgendwie nicht weiter.
Hier die 2 Klassen
Jedoch komm ich irgendwie nicht weiter.
Hier die 2 Klassen
Java:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Startbildschirm implements ActionListener {
JPanel startpanel=new JPanel();
JButton Leicht=new JButton("Leicht");
JButton Mittel=new JButton("Mittel");
JButton Schwierig=new JButton("Schwierig");
JFrame fenster = new JFrame();
int spielfeldgröße;
public Startbildschirm() {
startpanel.setLayout(new GridLayout(3,1));
Leicht.addActionListener(this);
Leicht.setActionCommand("Leicht");
Mittel.addActionListener(this);
Mittel.setActionCommand("Mittel");
Schwierig.addActionListener(this);
Schwierig.setActionCommand("Schwierig");
startpanel.add(Leicht);
startpanel.add(Mittel);
startpanel.add(Schwierig);
fenster.getContentPane().add(startpanel);
fenster.setVisible(true);
fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fenster.pack();
fenster.setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent arg0) {
String Schwierigkeit = arg0.getActionCommand();
if(Schwierigkeit.equals("Leicht")){
spielfeldgröße = 25;
Reactor re = new Reactor();
}
if(Schwierigkeit.equals("Mittel")){
spielfeldgröße = 49;
Reactor re = new Reactor();
}
if(Schwierigkeit.equals("Schwierig")){
spielfeldgröße = 81;
Reactor re = new Reactor();
}
}
public static void main(String[] args) {
Startbildschirm sb = new Startbildschirm();
}
}
Java:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Reactor implements ActionListener {
JButton[] btns = new JButton[25];
int punktzahl = 0;
int schwierigkeit = 5;
int sekundenzähler = 0;
int limit = 1000000000;
int wartezeit = 1000;
int high = 26;
int low = 1;
int spielfeldgröße ;
int mommentaneZahl ;
int spielfeldlänge = (int) Math.sqrt(spielfeldgröße);
boolean start = false;
JLabel label=new JLabel();
JPanel verliererpanel = new JPanel();
JPanel unterverliererpanel = new JPanel();
JPanel panel = new JPanel();
JTextField text1=new JTextField("Du hast verloren !");
JTextField text2=new JTextField("Deine Punktzahl :"+punktzahl+"");
JPanel spielfeld = new JPanel();
JFrame f = new JFrame();
JButton button=new JButton("OK");
public Reactor() {
initializeButtons();
button.addActionListener(this);
button.setActionCommand("OK");
panel.setLayout(new BorderLayout());
verliererpanel.setLayout(new GridLayout(2,1));
unterverliererpanel.setLayout(new GridLayout(2,1));
unterverliererpanel.add(text1);
text1.setHorizontalAlignment(0);
unterverliererpanel.add(text2);
text2.setHorizontalAlignment(0);
verliererpanel.add(unterverliererpanel,BorderLayout.CENTER);
verliererpanel.add(button);
spielfeld.setLayout(new GridLayout(spielfeldlänge, spielfeldlänge));
for (int zeile = 0; zeile < spielfeldgröße; zeile++) {
spielfeld.add(btns[zeile]);
btns[zeile].addActionListener(this);
btns[zeile].setBackground(Color.WHITE);
btns[zeile].setText("" + (zeile) + "");
btns[zeile].setActionCommand("" + zeile + "");
}
btns[((spielfeldgröße-1)/2)].setBackground(Color.RED);
mommentaneZahl = ((spielfeldgröße-1)/2);
panel.add(spielfeld, BorderLayout.CENTER);
panel.add(label, BorderLayout.NORTH);
f.getContentPane().add(panel);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
}
private void initializeButtons() {
for (int a = 0; a < spielfeldgröße; a++) {
btns[a] = new JButton(" ");
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
String gedrückteZahl = arg0.getActionCommand();
if(gedrückteZahl.equals("OK")){
Startbildschirm st=new Startbildschirm();
}
start = true;
if (gedrückteZahl.equals(""+mommentaneZahl+"")) {
punktzahl++;
label.setText(""+punktzahl+"");
if (punktzahl > 5) {
int zufallszahlextra1 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra1].setBackground(Color.BLUE);
}
if (punktzahl > 10) {
int zufallszahlextra2 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra2].setBackground(Color.GREEN);
}
if (punktzahl > 15) {
int zufallszahlextra3 = (int) (Math.random() * (spielfeldgröße));
btns[zufallszahlextra3].setBackground(Color.YELLOW);
}
if (punktzahl > 20) {
int zufallszahlextra4 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra4].setBackground(Color.ORANGE);
}
if (punktzahl > 25) {
int zufallszahlextra5 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra5].setBackground(Color.BLACK);
}
if (punktzahl == 30) {
for (int zeile = 0; zeile < spielfeldgröße; zeile++) {
btns[zeile].setBackground(Color.WHITE);
}
}
if (punktzahl >30) {
int zufallszahlextra6 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra6].setBackground(Color.PINK);
int zufallszahlextra7 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra7].setBackground(Color.MAGENTA);
}
if (punktzahl >50) {
int zufallszahlextra8 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra8].setBackground(Color.CYAN);
int zufallszahlextra9 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra9].setBackground(Color.BLUE);
}
if (punktzahl >100) {
int zufallszahlextra8 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra8].setBackground(Color.CYAN);
int zufallszahlextra9 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra9].setBackground(Color.BLUE);
int zufallszahlextra6 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra6].setBackground(Color.PINK);
int zufallszahlextra10 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra10].setBackground(Color.MAGENTA);
int zufallszahlextra11 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra11].setBackground(Color.BLACK);
int zufallszahlextra12 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra12].setBackground(Color.GREEN);
}
if (punktzahl >150) {
int zufallszahlextra13 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra13].setBackground(Color.CYAN);
int zufallszahlextra14= (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra14].setBackground(Color.BLUE);
int zufallszahlextra15 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra15].setBackground(Color.PINK);
int zufallszahlextra16 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra16].setBackground(Color.MAGENTA);
int zufallszahlextra17 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra17].setBackground(Color.BLACK);
int zufallszahlextra18 = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahlextra18].setBackground(Color.GREEN);
int zufallszahlextra19 = (int) (Math.random() * (spielfeldgröße));
btns[zufallszahlextra19].setBackground(Color.YELLOW);
}
btns[mommentaneZahl].setBackground(Color.WHITE);
int zufallszahl = (int) (Math.random() * (spielfeldgröße ));
btns[zufallszahl].setBackground(Color.RED);
mommentaneZahl = zufallszahl;
System.out.print(""+sekundenzähler+"");
sekundenzähler = 0;
}
else {
for (int zeile = 0; zeile < spielfeldgröße; zeile++) {
btns[zeile].removeActionListener(this);
start = false;
f.remove(panel);
f.add(verliererpanel, BorderLayout.CENTER);
}
}
}
}