From e14c22976cd304bcfc00402e125f841204cae6b3 Mon Sep 17 00:00:00 2001 From: Obai Albek Date: Sun, 18 May 2025 23:26:01 +0200 Subject: [PATCH] My pong_game --- MyPong_Game/.gitignore | 3 + .../org.eclipse.core.resources.prefs | 2 + .../.settings/org.eclipse.jdt.core.prefs | 11 ++ MyPong_Game/src/module-info.java | 9 + MyPong_Game/src/pong_game/Ball.java | 42 +++++ MyPong_Game/src/pong_game/GamePanle.java | 166 ++++++++++++++++++ MyPong_Game/src/pong_game/GamePlay.java | 8 + MyPong_Game/src/pong_game/GameWindow.java | 19 ++ MyPong_Game/src/pong_game/Schläger.java | 62 +++++++ 9 files changed, 322 insertions(+) create mode 100644 MyPong_Game/.gitignore create mode 100644 MyPong_Game/.settings/org.eclipse.core.resources.prefs create mode 100644 MyPong_Game/.settings/org.eclipse.jdt.core.prefs create mode 100644 MyPong_Game/src/module-info.java create mode 100644 MyPong_Game/src/pong_game/Ball.java create mode 100644 MyPong_Game/src/pong_game/GamePanle.java create mode 100644 MyPong_Game/src/pong_game/GamePlay.java create mode 100644 MyPong_Game/src/pong_game/GameWindow.java create mode 100644 MyPong_Game/src/pong_game/Schläger.java diff --git a/MyPong_Game/.gitignore b/MyPong_Game/.gitignore new file mode 100644 index 0000000..3eaf567 --- /dev/null +++ b/MyPong_Game/.gitignore @@ -0,0 +1,3 @@ +/bin/ +/.classpath +/.project diff --git a/MyPong_Game/.settings/org.eclipse.core.resources.prefs b/MyPong_Game/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/MyPong_Game/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/MyPong_Game/.settings/org.eclipse.jdt.core.prefs b/MyPong_Game/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..6f94d6a --- /dev/null +++ b/MyPong_Game/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,11 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=23 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=23 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=23 diff --git a/MyPong_Game/src/module-info.java b/MyPong_Game/src/module-info.java new file mode 100644 index 0000000..69fcbbc --- /dev/null +++ b/MyPong_Game/src/module-info.java @@ -0,0 +1,9 @@ +/** + * + */ +/** + * + */ +module MyPong_Game { + requires java.desktop; +} \ No newline at end of file diff --git a/MyPong_Game/src/pong_game/Ball.java b/MyPong_Game/src/pong_game/Ball.java new file mode 100644 index 0000000..d651b98 --- /dev/null +++ b/MyPong_Game/src/pong_game/Ball.java @@ -0,0 +1,42 @@ +package pong_game; + +import java.awt.*; +import java.util.Random; + +public class Ball extends Rectangle { + + int yBewegung; + int xBewegung; + int speed = 4; + Random random = new Random(); + + Ball(int x, int y, int width, int height) { + super(x, y, width, height); + xBewegung = random.nextInt(10) - 5; + if (xBewegung == 0) + xBewegung++; + xBewegung(xBewegung * speed); + + } + + public void draw(Graphics g) { + g.setColor(Color.green); + g.fillOval(x, y, width, height); + + } + + public void xBewegung(int x) { + xBewegung = x; + } + + public void yBewegung(int y) { + yBewegung = y; + } + + public void move() { + x += xBewegung; + y += yBewegung; + + } + +} diff --git a/MyPong_Game/src/pong_game/GamePanle.java b/MyPong_Game/src/pong_game/GamePanle.java new file mode 100644 index 0000000..ad7b27a --- /dev/null +++ b/MyPong_Game/src/pong_game/GamePanle.java @@ -0,0 +1,166 @@ +package pong_game; + +import java.awt.*; +import java.awt.event.*; +import java.util.*; +import javax.swing.*; + +public class GamePanle extends JPanel implements Runnable { + + final static int WIDTH_WINDOW = 1000; + final static int HEIGH_WINDOW = 700; + Dimension fensterSize = new Dimension(WIDTH_WINDOW, HEIGH_WINDOW); + + final static int WIDTH_RECT = 20; + final static int HEIGHT_RECT = 100; + + final static int WIDTH_BALL = 20; + final static int HEIGHT_BALL = 20; + + Schläger spieler1, spieler2; + Ball ball; + Random random; + Thread game; + int scoreSpieler1 = 0; + int scoreSpieler2 = 0; + int speed = 2; + + GamePanle() { + zeichneRects(); + this.setFocusable(true); + this.setPreferredSize(fensterSize); + this.addKeyListener(new AL()); + random = new Random(); + game = new Thread(this); + game.start(); + + } + + public void zeichneRects() { + spieler1 = new Schläger(0, (HEIGH_WINDOW / 2) - (HEIGHT_RECT / 2), WIDTH_RECT, HEIGHT_RECT, 1); + spieler2 = new Schläger(WIDTH_WINDOW - WIDTH_RECT, (HEIGH_WINDOW / 2) - (HEIGHT_RECT / 2), WIDTH_RECT, + HEIGHT_RECT, 2); + ball = new Ball(WIDTH_WINDOW / 2, 100, WIDTH_BALL, HEIGHT_BALL); + } + + public void paint(Graphics g) { + super.paint(g); + + // Screen farbe + g.setColor(Color.BLACK); + g.fillRect(0, 0, WIDTH_WINDOW, HEIGH_WINDOW); + g.setColor(Color.GRAY); + g.fillRect(WIDTH_WINDOW / 2, 0, 5, HEIGH_WINDOW); + + spieler1.draw(g); + spieler2.draw(g); + ball.draw(g); + + g.setFont(new Font("Arial", Font.BOLD, 30)); + g.setColor(Color.WHITE); + g.drawString(scoreSpieler1 + "", WIDTH_WINDOW / 2 - 50, 35); + + g.setColor(Color.WHITE); + g.drawString(scoreSpieler2 + "", WIDTH_WINDOW / 2 + 50, 35); + + g.setFont(new Font("Arial", Font.BOLD, 20)); + g.setColor(Color.YELLOW); + g.drawString("Obai", WIDTH_WINDOW / 2 - 120, 35); + + g.setFont(new Font("Arial", Font.BOLD, 20)); + g.setColor(Color.YELLOW); + g.drawString("ComputerGegener", WIDTH_WINDOW / 2 + 100, 35); + + } + + public void mov() { + + spieler1.move(); + spieler2.movegegener(); + ball.move(); + + } + + public void checkRänder() { + if (spieler1.y <= 0) + spieler1.y = 0; + + if (spieler1.y >= HEIGH_WINDOW - HEIGHT_RECT) + spieler1.y = HEIGH_WINDOW - HEIGHT_RECT; + + if (ball.x > WIDTH_WINDOW / 2 && ball.y < spieler2.y) + spieler2.setBewegungGegener(-10); + + else if (ball.x > WIDTH_WINDOW / 2 && ball.y > spieler2.y) + spieler2.setBewegungGegener(10); + + else + spieler2.setBewegungGegener(0); + + if (spieler2.y <= 0) + spieler2.y = 0; + + if (spieler2.y >= HEIGH_WINDOW - HEIGHT_RECT) + spieler2.y = HEIGH_WINDOW - HEIGHT_RECT; + + if (ball.intersects(spieler1)) { + int bewegexRandom = random.nextInt(8) + 4; + ball.xBewegung(bewegexRandom); + int bewegeyRandom = random.nextInt(5) - 2; + ball.yBewegung(bewegeyRandom); + } + if (ball.intersects(spieler2)) { + + int bewegexRandom = random.nextInt(8) + 4; + ball.xBewegung(-bewegexRandom); + int bewegeyRandom = random.nextInt(5) - 2; + ball.yBewegung(bewegeyRandom); + + } + if (ball.x <= 0) { + scoreSpieler2++; + zeichneRects(); + } + + if (ball.x >= WIDTH_WINDOW) { + scoreSpieler1++; + zeichneRects(); + } + + if (ball.y >= HEIGH_WINDOW - HEIGHT_BALL) + ball.yBewegung = -ball.yBewegung; + + if (ball.y <= 0) + ball.yBewegung = -ball.yBewegung; + + } + + @Override + public void run() { + try { + while (true) { + + mov(); + checkRänder(); + repaint(); + Thread.sleep(5); + } + } catch (Exception e) { + System.err.println(e.getMessage()); + } + + } + + public class AL extends KeyAdapter { + public void keyPressed(KeyEvent e) { + spieler1.keyPressed(e); + spieler2.keyPressed(e); + } + + public void keyReleased(KeyEvent e) { + spieler1.keyReleased(e); + spieler2.keyReleased(e); + } + } + +} diff --git a/MyPong_Game/src/pong_game/GamePlay.java b/MyPong_Game/src/pong_game/GamePlay.java new file mode 100644 index 0000000..f82c15d --- /dev/null +++ b/MyPong_Game/src/pong_game/GamePlay.java @@ -0,0 +1,8 @@ +package pong_game; + +public class GamePlay { + public static void main(String[] args) { + new GameWindow(); + + } +} diff --git a/MyPong_Game/src/pong_game/GameWindow.java b/MyPong_Game/src/pong_game/GameWindow.java new file mode 100644 index 0000000..618e718 --- /dev/null +++ b/MyPong_Game/src/pong_game/GameWindow.java @@ -0,0 +1,19 @@ +package pong_game; +import javax.swing.*; + +public class GameWindow extends JFrame { + + GamePanle game; + + GameWindow() { + game = new GamePanle(); + this.add(game); + this.setTitle("Pong Game"); + this.setResizable(false); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.pack(); + this.setLocationRelativeTo(null); + this.setVisible(true); + } + +} diff --git a/MyPong_Game/src/pong_game/Schläger.java b/MyPong_Game/src/pong_game/Schläger.java new file mode 100644 index 0000000..2aba8a2 --- /dev/null +++ b/MyPong_Game/src/pong_game/Schläger.java @@ -0,0 +1,62 @@ +package pong_game; + +import java.awt.*; +import java.awt.event.KeyEvent; + +public class Schläger extends Rectangle { + + int id; + int yBewegung; + + int gegnerBewegungy; + int speed = 10; + + Schläger(int x, int y, int width, int height, int id){ + super(x,y,width,height); + this.id = id; + } + + public void move() { + y += yBewegung; + } + + public void movegegener() { + y += gegnerBewegungy; + } + + + public void keyPressed(KeyEvent e) { + if (id == 1) { + if (e.getKeyCode() == KeyEvent.VK_W) + setBewegung(-speed); + + if (e.getKeyCode() == KeyEvent.VK_S) + setBewegung(speed); + } + } + + public void keyReleased(KeyEvent e) { + if (id == 1) { + if (e.getKeyCode() == KeyEvent.VK_W ||e.getKeyCode() == KeyEvent.VK_S) + setBewegung(0); + } + } + + public void setBewegung(int bewegung) { + yBewegung = bewegung; + } + + public void setBewegungGegener(int bewegung) { + gegnerBewegungy = bewegung; + } + + public void draw(Graphics g) { + if (id == 1) + g.setColor(Color.RED); + else + g.setColor(Color.BLUE); + + g.fillRect(x,y,width,height); + } + +}