PongGame
parent
4052a1392b
commit
6f4f781a92
|
@ -19,17 +19,18 @@ public class MouseMotionListenerBeispiel {
|
|||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
mousePosition.setText(" Mouse Position: (" + e.getX() + ", " + e.getY() + ")");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseMoved(MouseEvent e) {
|
||||
mousePosition.setText(" Mouse Position: (" + e.getX() + ", " + e.getY() + ")");
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
frame.setSize(300, 250); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 250
|
||||
frame.setSize(700, 500); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 250
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
|
||||
frame.setLayout(new FlowLayout()); // لترتيب الأشياء التي أضفناها فيها FlowLayout هنا جعلنا النافذة تستخدم الـ
|
||||
frame.setVisible(true); // هنا جعلنا النافذة مرئية
|
||||
|
|
|
@ -7,107 +7,154 @@ import java.awt.event.KeyListener;
|
|||
|
||||
public class PongGame {
|
||||
|
||||
static int xRect1 = 10;
|
||||
static int yRect1 = 10;
|
||||
static int bewegung = 1;
|
||||
//Eigenschaften des Rechtecks
|
||||
static JPanel rect1;
|
||||
static int xPositionRect1 = 300;
|
||||
static int yPositionRect1 = 490;
|
||||
|
||||
public static void main(String[] args) {
|
||||
static int rectWidth = 100;
|
||||
static int rectHeight = 7;
|
||||
|
||||
static int bewegungRect = 1;
|
||||
//--------------------------
|
||||
|
||||
//Eigenschaften des Kreises
|
||||
static JPanel kreis;
|
||||
static int xPositioKreis = 300;
|
||||
static int yPositionKreis = 400;
|
||||
|
||||
static int kreisWidth = 10;
|
||||
static int kreisHeight = 10;
|
||||
|
||||
static int bewegungKreisX = 1;
|
||||
static int bewegungKreisY = 1;
|
||||
//---------------------------
|
||||
|
||||
//Widow Eigenschaften
|
||||
static int widthWindow = 700;
|
||||
static int heihtWindow = 500;
|
||||
//------------------------------
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame window = new JFrame();
|
||||
window.setSize(700, 700);
|
||||
window.setSize(widthWindow, heihtWindow);
|
||||
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
window.setTitle("PongGame");
|
||||
window.setLayout(null);
|
||||
window.setResizable(false);
|
||||
|
||||
|
||||
|
||||
kreis = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.white);
|
||||
g.drawOval(0, 0, kreisWidth, kreisHeight);
|
||||
|
||||
}
|
||||
};
|
||||
kreis.setBounds(xPositioKreis, yPositionKreis, kreisWidth, kreisHeight);
|
||||
|
||||
|
||||
Timer timerKreis = new Timer(5, e -> {
|
||||
|
||||
// Wenn der Kreis die Oberkante erreicht, kehrt er die Richtung um
|
||||
if (yPositionKreis == 0)
|
||||
bewegungKreisY = -bewegungKreisY;
|
||||
|
||||
// if (yPositionKreis >= window.getHeight() - kreisHeight)
|
||||
// bewegungKreisY = -bewegungKreisY;
|
||||
//
|
||||
// Erstelle Rechteck für Ball und Rechteck
|
||||
Rectangle ballBounds = new Rectangle(xPositioKreis, yPositionKreis, kreisWidth, kreisHeight);
|
||||
Rectangle rectBounds = new Rectangle(xPositionRect1, yPositionRect1, rectWidth, rectHeight);
|
||||
|
||||
// Überprüfe Kollision zwischen Ball und Rechteck
|
||||
if (ballBounds.intersects(rectBounds)) {
|
||||
bewegungKreisY = -bewegungKreisY; // Ball prallt ab
|
||||
}
|
||||
|
||||
|
||||
if (xPositioKreis <= 0)
|
||||
bewegungKreisX = -bewegungKreisX;
|
||||
|
||||
if (xPositioKreis >= window.getWidth() - kreisWidth)
|
||||
bewegungKreisX = -bewegungKreisX;
|
||||
|
||||
yPositionKreis -= bewegungKreisY;
|
||||
xPositioKreis += bewegungKreisX;
|
||||
|
||||
kreis.setBounds(xPositioKreis, yPositionKreis, kreisWidth, kreisHeight);
|
||||
kreis.repaint();
|
||||
|
||||
});
|
||||
timerKreis.start();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
JPanel rect1 = new JPanel() {
|
||||
rect1 = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g); // Sicherstellen, dass das Panel korrekt gezeichnet wird
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(0, 0, 10, 90);
|
||||
g.fillRect(0, 0, rectWidth, rectHeight);
|
||||
}
|
||||
};
|
||||
rect1.setBounds(xRect1, yRect1, 10, 90);
|
||||
rect1.setBounds(xPositionRect1, yPositionRect1, rectWidth, rectHeight);
|
||||
|
||||
|
||||
|
||||
Timer timer = new Timer(710, e -> {
|
||||
window.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
Timer timerRect = new Timer(710, e -> {
|
||||
window.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
if (e.getKeyCode() ==KeyEvent.VK_DOWN) {
|
||||
// Bewege das Rechteck
|
||||
yRect1 += bewegung ;
|
||||
|
||||
// Aktualisiere dann die Position des Panels
|
||||
rect1.setBounds(xRect1, yRect1, 10, 90);
|
||||
// Fordere das Panel auf, sich neu zu zeichnen
|
||||
rect1.repaint();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
// Starte den Timer
|
||||
timer.start();
|
||||
|
||||
|
||||
|
||||
// Setze den Fokus auf das Fenster, um die KeyListener zu aktivieren
|
||||
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
|
||||
bewegungRect = 1;
|
||||
if (xPositionRect1 >= widthWindow - 95)
|
||||
bewegungRect = 0;
|
||||
|
||||
else
|
||||
xPositionRect1 += bewegungRect;
|
||||
|
||||
rect1.setBounds(xPositionRect1, yPositionRect1, rectWidth, rectHeight);
|
||||
// Fordere das Panel auf, sich neu zu zeichnen
|
||||
rect1.repaint();
|
||||
}
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
|
||||
bewegungRect = 1;
|
||||
if (xPositionRect1 <= 0)
|
||||
bewegungRect = 0;
|
||||
else
|
||||
xPositionRect1 -= bewegungRect;
|
||||
|
||||
}
|
||||
rect1.setBounds(xPositionRect1, yPositionRect1, rectWidth, rectHeight);
|
||||
// Fordere das Panel auf, sich neu zu zeichnen
|
||||
rect1.repaint();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
timerRect.start();
|
||||
|
||||
window.setFocusable(true);
|
||||
window.requestFocusInWindow();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
JPanel rect2 = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(0, 0, 10, 90);
|
||||
}
|
||||
};
|
||||
rect2.setBounds(650, 10, 10, 90);
|
||||
|
||||
window.add(rect1);
|
||||
window.add(rect2);
|
||||
window.add(kreis);
|
||||
|
||||
/*
|
||||
* JFrame besteht aus mehreren Schichten: Eine davon ist das ContentPane, welches der Bereich ist, in den die meisten Komponenten wie JPanel, JButton, etc.
|
||||
|
|
Loading…
Reference in New Issue