Michael
Grünschnabel
Dabei seit: 01.07.2012
Beiträge: 2
|
|
Ich habe das problem inzwischen gelöst falls dich interessiert wie:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.math.*;
import java.awt.image.BufferedImage;
public class Viereck extends JFrame {
private JButton viereck,rechts,oben,unten;
private static int arg1=150,arg2=100,arg3=500,arg4=250,ver,ver2;
private static int arg9=650, arg10=100;
private JButton viereck2, rechts2, oben2, unten2;
private static JLabel ball, linie1, linie2, goals1, goals2;
public static int score1=0, score2=0;
Viereck() {
super();
this.setTitle("viereck bewegen");
this.setLayout(null);
goals1 = new JLabel();
goals1.setText("Spieler 1 hat " + String.valueOf(score1) + " Punkte");
goals1.setBounds(200, 35, 250, 15);
goals2 = new JLabel();
goals2.setText("Spieler 2 hat " + String.valueOf(score2) + " Punkte");
goals2.setBounds(775, 35, 250, 15);
linie1 = new JLabel();
linie2 = new JLabel();
ImageIcon vonLinie = new ImageIcon("linie.png");
linie1.setIcon(vonLinie);
linie2.setIcon(vonLinie);
linie1.setBounds(200, 50, 700, 10);
linie2.setBounds(200, 550, 700, 10);
ball = new JLabel();
ImageIcon vonball = new ImageIcon("ball.gif");
ball.setIcon(vonball);
ball.setBounds(arg3, arg4, 20, 20);
int alt = (int)Math.round((Math.random()*3));
if (alt == 0 ) {ver = 1;ver2 = 1;}
if (alt == 1 ) {ver = -1;ver2 = 1;}
if (alt == 2 ) {ver = 1;ver2 = -1;}
if (alt == 3 ) {ver = -1;ver2 = -1;}
// für das blaue Viereck
rechts = new JButton("r");
//rechts.setVisible(false);
rechts.setMnemonic(KeyEvent.VK_D);
rechts.addActionListener(new Richtung());
rechts.setBounds(1300, 800, 5, 5);
oben = new JButton("o");
//oben.setVisible(false);
oben.setMnemonic(KeyEvent.VK_W);
oben.addActionListener(new Richtung());
oben.setBounds(1300, 800, 5, 5);
unten = new JButton("u");
//unten.setVisible(false);
unten.setMnemonic(KeyEvent.VK_S);
unten.addActionListener(new Richtung());
unten.setBounds(1300, 800, 5, 5);
viereck = new JButton("Hallo");
viereck.setBounds(arg1, arg2, 60, 60);
viereck.setMnemonic(KeyEvent.VK_A);
viereck.addActionListener(new Richtung());
// für das Rote Viereck
rechts2 = new JButton("r");
//rechts.setVisible(false);
rechts2.setMnemonic(KeyEvent.VK_RIGHT);
rechts2.addActionListener(new Richtung2());
rechts2.setBounds(1300, 800, 5, 5);
oben2 = new JButton("o");
//oben.setVisible(false);
oben2.setMnemonic(KeyEvent.VK_UP);
oben2.addActionListener(new Richtung2());
oben2.setBounds(1300, 800, 5, 5);
unten2 = new JButton("u");
//unten.setVisible(false);
unten2.setMnemonic(KeyEvent.VK_DOWN);
unten2.addActionListener(new Richtung2());
unten2.setBounds(1300, 800, 5, 5);
viereck2 = new JButton("Hallo2");
viereck2.setBounds(arg9, arg10, 60, 60);
viereck2.setMnemonic(KeyEvent.VK_LEFT);
viereck2.addActionListener(new Richtung2());
ImageIcon blau = new ImageIcon("blau.gif");
ImageIcon rot = new ImageIcon("rot.gif");
viereck.setIcon(blau);
viereck2.setIcon(rot);
this.getContentPane().setBackground(new Color(0, 0, 0));
this.getContentPane().add(viereck);
this.getContentPane().add(goals2);
this.getContentPane().add(goals1);
this.getContentPane().add(rechts);
this.getContentPane().add(oben);
this.getContentPane().add(unten);
this.getContentPane().add(viereck2);
this.getContentPane().add(rechts2);
this.getContentPane().add(oben2);
this.getContentPane().add(unten2);
this.getContentPane().add(ball);
this.getContentPane().add(linie1);
this.getContentPane().add(linie2);
this.getContentPane().update(getGraphics());
}
Viereck(int score1,int score2) {
this();
this.score();
}
// blaue bewegung
class Richtung implements ActionListener {
public void actionPerformed(ActionEvent e) {
String richt = e.getActionCommand();
if (richt.equals("r")) {
arg1 += 20;
}
if (richt.equals("Hallo")) {
arg1 -= 20;
}
if (richt.equals("o")) {
arg2 -= 20;
}
if (richt.equals("u")) {
arg2 += 20;
}
viereck.setBounds(arg1, arg2, 60, 60);
viereck.update(getGraphics());
}
}
// rote bewegung
class Richtung2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
String richt = e.getActionCommand();
if (richt.equals("r")) {
arg9 += 20;
}
if (richt.equals("Hallo2")) {
arg9 -= 20;
}
if (richt.equals("o")) {
arg10 -= 20;
}
if (richt.equals("u")) {
arg10 += 20;
}
viereck2.setBounds(arg9, arg10, 60, 60);
viereck2.update(getGraphics());
}
}
public void ab() {
viereck.update(getGraphics());
this.getContentPane().update(getGraphics());
}
// Score
public void score() {
if (arg3<175) {
++score2;
goals1.setText("Spieler 1 hat " + String.valueOf(score1) + " Punkte");
goals2.setText("Spieler 2 hat " + String.valueOf(score2) + " Punkte");
if (score2>=1000) {
System.out.println("Spieler 2 hat gewonnen");
System.exit(0);
}
}
if (arg3>925) {
++score1;
goals1.setText("Spieler 1 hat " + String.valueOf(score1) + " Punkte");
goals2.setText("Spieler 2 hat " + String.valueOf(score2) + " Punkte");
if (score1>=1000) {
System.out.println("Spieler 1 hat gewonnen");
System.exit(0);
}
}
}
public static void main(String[] args) {
Viereck Fenster = new Viereck();
Fenster.pack();
Fenster.setVisible(true);
Fenster.setSize(1280, 720);
while (true) {
try {
arg3 += ver;
arg4 += ver2;
// wenn der Ball irgendwo gegen kommt
// von links gegen rechten ODER von rechts gegen linken
if ((arg3+20==arg9&&(arg4>=arg10&&arg4<=(arg10+60)))||
(arg3==(arg1+60)&&(arg4>=arg2&&arg4<=(arg2+60)))) {
ver *= -1;
}
// von rechts gegen links ODER von links gegen rechts
if ((arg3==(arg9+60)&&(arg4>=arg10&&arg4<=(arg10+60)))||
(arg3==arg1&&(arg4>=arg2&&arg4<=(arg2+60)))) {
ver *= -1;
}
// von oben
if (((arg4+20)==arg10&&(arg3>=arg9&&arg3<=(arg9+60)))||
((arg4+20)==arg2&&(arg3>=arg1&&arg3<=(arg1+60)))) {
ver2 *= -1;
}
// von unten
if ((arg4==(arg10+60)&&(arg3>=arg9&&arg3<=(arg9+60)))||
(arg4==(arg2+60)&&(arg3>=arg1&&arg3<=(arg1+60)))) {
ver2 *= -1;
}
// Oben und unten linien
if (arg4<=50) {
ver2 *= -1;
}
if (arg4>=540) {
ver2 *= -1;
}
// rechts und links
if (arg3==25) {
ver *= -1;
}
if (arg3==1075) {
ver *= -1;
}
Fenster.score();
Thread.sleep(5);
System.out.println(arg3);
ball.setBounds(arg3, arg4, 20, 20);
Fenster.ab();
} catch (InterruptedException e) {
System.out.println("fail!");
}
}
}
}
P.S.: ich weiß nicht wie man die Frage als beantwortet einträgt also schließt
|
|