Pong Games
commit
2686aac7e3
|
@ -0,0 +1,13 @@
|
|||
package Algorithmus;
|
||||
|
||||
public class SwitchCases {
|
||||
|
||||
public static void main(String[] args) {
|
||||
/*
|
||||
* Die Daten Typen, die erlaubt sind:
|
||||
* - int, char, enum, String und die Wrapper-Typen (Integer, Character, etc.).
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package Algorithmus;
|
||||
|
||||
public class VariadischeFunktionen {
|
||||
|
||||
public static int sum(int... numbers) {
|
||||
int result = 0;
|
||||
for (int number : numbers)
|
||||
result += number;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(sum(1, 2, 3));
|
||||
}
|
||||
|
||||
}
|
|
@ -21,6 +21,8 @@ public class MouseMotionListenerBeispiel {
|
|||
public void mouseDragged(MouseEvent e) {
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -31,6 +33,7 @@ public class MouseMotionListenerBeispiel {
|
|||
});
|
||||
|
||||
frame.setSize(700, 700); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 250
|
||||
frame.setSize(700, 500); // هنا قمنا بتحديد حجم النافذة. عرضها 300 و طولها 250
|
||||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // هنا جعلنا زر الخروج من النافذة يغلق البرنامج
|
||||
frame.setLayout(new FlowLayout()); // لترتيب الأشياء التي أضفناها فيها FlowLayout هنا جعلنا النافذة تستخدم الـ
|
||||
frame.setVisible(true); // هنا جعلنا النافذة مرئية
|
||||
|
|
|
@ -1,246 +0,0 @@
|
|||
package Übungen;
|
||||
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
public class PongGame {
|
||||
|
||||
//Eigenschaften des Rechtecks1
|
||||
static JPanel rect1;
|
||||
static int xPositionRect1 = 300;
|
||||
static int yPositionRect1 = 550;
|
||||
|
||||
static int rectWidth = 100;
|
||||
static int rectHeight = 7;
|
||||
|
||||
static int daleyRect = 300;
|
||||
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 daleyKreis = 5;
|
||||
static int bewegungKreisX = 1;
|
||||
static int bewegungKreisY = 1;
|
||||
//---------------------------
|
||||
|
||||
//Window Eigenschaften
|
||||
static int widthWindow = 700;
|
||||
static int heihtWindow = 600;
|
||||
//------------------------------
|
||||
|
||||
//Eigenschaften des Rechtecks2
|
||||
static JPanel rect2;
|
||||
static int xPositionRect2 = widthWindow/2;
|
||||
static int yPositionRect2 = 5;
|
||||
static int rect2Width = 100;
|
||||
static int rect2Height = 7;
|
||||
static int daleyRect2 = 300;
|
||||
static int bewegungRect2 = 1;
|
||||
|
||||
Graphics g;
|
||||
public static void main(String[] args) {
|
||||
JFrame window = new JFrame();
|
||||
window.setSize(widthWindow, heihtWindow);
|
||||
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
window.setTitle("PongGame");
|
||||
window.setLayout(null);
|
||||
window.setResizable(false);
|
||||
|
||||
rect2 = new JPanel() {
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(0, 0, rect2Width, rect2Height);
|
||||
}
|
||||
|
||||
};
|
||||
rect2.setBounds(xPositionRect2,yPositionRect2,rect2Width,rect2Height);
|
||||
Timer timerRect2 = new Timer(daleyRect2, e -> {
|
||||
window.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_D) {
|
||||
bewegungRect2 = 1;
|
||||
if (xPositionRect2 >= widthWindow - 95)
|
||||
bewegungRect2 = 0;
|
||||
|
||||
else
|
||||
xPositionRect2 += bewegungRect2;
|
||||
|
||||
rect2.setBounds(xPositionRect2, yPositionRect2, rect2Width, rect2Height);
|
||||
// Fordere das Panel auf, sich neu zu zeichnen
|
||||
rect2.repaint();
|
||||
}
|
||||
|
||||
if (e.getKeyCode() == KeyEvent.VK_A) {
|
||||
bewegungRect2 = 1;
|
||||
if (bewegungRect2 <= 0)
|
||||
bewegungRect = 0;
|
||||
else
|
||||
xPositionRect2 -= bewegungRect2;
|
||||
|
||||
}
|
||||
rect2.setBounds(xPositionRect2, yPositionRect2, rect2Width, rect2Height);
|
||||
// Fordere das Panel auf, sich neu zu zeichnen
|
||||
rect2.repaint();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
timerRect2.start();
|
||||
|
||||
|
||||
|
||||
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(daleyKreis, e -> {
|
||||
// Erstelle Rechteck für Ball und Rechteck
|
||||
Rectangle ballBounds = new Rectangle(xPositioKreis, yPositionKreis, kreisWidth, kreisHeight);
|
||||
Rectangle rectBounds = new Rectangle(xPositionRect1, yPositionRect1, rectWidth, rectHeight);
|
||||
Rectangle rectBounds2 = new Rectangle(xPositionRect2, yPositionRect2, rect2Width, rect2Height);
|
||||
|
||||
// Wenn der Kreis die Oberkante erreicht, kehrt er die Richtung um
|
||||
if (ballBounds.intersects(rectBounds2))
|
||||
bewegungKreisY = -bewegungKreisY;
|
||||
|
||||
// if (yPositionKreis >= window.getHeight() - kreisHeight)
|
||||
// bewegungKreisY = -bewegungKreisY;
|
||||
//
|
||||
|
||||
|
||||
// Überprüfe Kollision zwischen Ball und Rechteck
|
||||
if (ballBounds.intersects(rectBounds)) {
|
||||
bewegungKreisY = -bewegungKreisY;
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
|
||||
|
||||
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, rectWidth, rectHeight);
|
||||
}
|
||||
};
|
||||
rect1.setBounds(xPositionRect1, yPositionRect1, rectWidth, rectHeight);
|
||||
|
||||
Timer timerRect = new Timer(daleyRect, e -> {
|
||||
window.addKeyListener(new KeyListener() {
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
|
||||
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();
|
||||
|
||||
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.
|
||||
* eingefügt werden. Die Methode getContentPane() gibt dieses ContentPane zurück, das dann bearbeitet werden kann.
|
||||
* Hintergrundfarbe setzen: Wenn du die Hintergrundfarbe des gesamten Fensters auf Schwarz setzen möchtest,
|
||||
* ohne die JPanel-Komponenten selbst zu beeinflussen, setzt du die Hintergrundfarbe des ContentPane.
|
||||
*/
|
||||
window.getContentPane().setBackground(Color.black);
|
||||
window.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -20,6 +20,7 @@ public class Ball extends JPanel implements Runnable {
|
|||
JLabel scoreSpieler1 = new JLabel ("Score: " + spieler1.getScore());
|
||||
JLabel scoreSpieler2 = new JLabel ("Score: " + spieler2.getScore());
|
||||
|
||||
|
||||
int delay = 4;
|
||||
double ybewegung;
|
||||
double xbewegung;
|
||||
|
@ -48,9 +49,6 @@ public class Ball extends JPanel implements Runnable {
|
|||
if (ball.y <= 0 || ball.y >= 650)
|
||||
ybewegung = -ybewegung;
|
||||
|
||||
|
||||
|
||||
|
||||
ball.y += ybewegung;
|
||||
ball.x += xbewegung;
|
||||
setBounds(ball.x, ball.y, ball.width, ball.height);
|
||||
|
@ -78,20 +76,19 @@ public class Ball extends JPanel implements Runnable {
|
|||
ybewegung = ybewegung + (rand.nextDouble() - 0.5) * 0.5;
|
||||
}
|
||||
|
||||
if (ball.x <= 0) {
|
||||
if (ball.x <= 0) { // Spieler 2 bekommt einen Punkt
|
||||
spieler2.setScore(spieler2.getScore() + 1);
|
||||
System.out.println("Spieler 2 Score: " + spieler2.getScore());
|
||||
resetBall();
|
||||
}
|
||||
|
||||
if (ball.x >= 680) {
|
||||
if (ball.x >= 680) { // Spieler 1 bekommt einen Punkt
|
||||
spieler1.setScore(spieler1.getScore() + 1);
|
||||
System.out.println("Spieler 1 Score: " + spieler1.getScore());
|
||||
resetBall();
|
||||
}
|
||||
updateScores();
|
||||
}
|
||||
|
||||
}
|
||||
public void resetBall() {
|
||||
ball.x = 700 / 2;
|
||||
ball.y = 700 / 2;
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
package Übungen.Ponggame;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Rectangle;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
|
||||
public class ComputerGegener extends JPanel implements Runnable {
|
||||
int id;
|
||||
final int width = 8;
|
||||
final int height = 60;
|
||||
int yPosition;
|
||||
int xPosition;
|
||||
int score;
|
||||
int ybewegung = 1;
|
||||
int dealy = 6;
|
||||
Rectangle schläger;
|
||||
|
||||
public ComputerGegener(int x, int y, int id) {
|
||||
this.xPosition = x;
|
||||
this.yPosition = y;
|
||||
this.id = id;
|
||||
this.score = 0;
|
||||
schläger = new Rectangle(x, y, width, height);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
g.setColor(Color.white);
|
||||
g.fillRect(schläger.x, schläger.y, schläger.width, schläger.height);
|
||||
|
||||
}
|
||||
|
||||
public void move() {
|
||||
schläger.y += ybewegung;
|
||||
if (schläger.y <= 0 ||schläger.y >= 600 )
|
||||
ybewegung = -ybewegung;
|
||||
|
||||
setBounds(schläger.x, schläger.y, schläger.width, schläger.height);
|
||||
repaint();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
move();
|
||||
Thread.sleep(dealy);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -32,15 +32,14 @@ public class GameWindwo extends JFrame {
|
|||
ball.scoreSpieler1.setBounds(10, 10, 100, 10);
|
||||
|
||||
ball.spieler2.setBounds(WINDOW_WIDTH - 40, WINDOW_HEIGHT / 2, 8, 60);
|
||||
ball.scoreSpieler2.setForeground(Color.white);
|
||||
ball.scoreSpieler2.setBounds(WINDOW_WIDTH - 100, 10, 100, 10);
|
||||
ball.spieler2.setForeground(Color.white);
|
||||
ball.scoreSpieler2.setBounds(600, 10, 100, 10);
|
||||
|
||||
this.add(ball);
|
||||
this.add(ball.spieler1);
|
||||
this.add(ball.scoreSpieler1);
|
||||
this.add(ball.spieler2);
|
||||
this.add(ball.scoreSpieler2);
|
||||
|
||||
this.add(blackScreen);
|
||||
getInputs();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue