From ce9f646909696cbcbe84605db964a2bea6b6a7d2 Mon Sep 17 00:00:00 2001 From: 3009594 Date: Sat, 7 Sep 2024 00:14:08 +0200 Subject: [PATCH] Breakout --- .../MouseMotionListenerBeispiel.java | 1 - .../src/Übungen/BreakOut/GamePlay.java | 229 ++++++++++++++++++ .../src/Übungen/BreakOut/Window.java | 36 +++ 3 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 Programmierung2/src/Übungen/BreakOut/GamePlay.java create mode 100644 Programmierung2/src/Übungen/BreakOut/Window.java diff --git a/Programmierung2/src/GUIAnwendungen/EventsButton/MouseMotionListenerBeispiel.java b/Programmierung2/src/GUIAnwendungen/EventsButton/MouseMotionListenerBeispiel.java index 53f0c81..10903b3 100644 --- a/Programmierung2/src/GUIAnwendungen/EventsButton/MouseMotionListenerBeispiel.java +++ b/Programmierung2/src/GUIAnwendungen/EventsButton/MouseMotionListenerBeispiel.java @@ -33,7 +33,6 @@ 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); // هنا جعلنا النافذة مرئية diff --git a/Programmierung2/src/Übungen/BreakOut/GamePlay.java b/Programmierung2/src/Übungen/BreakOut/GamePlay.java new file mode 100644 index 0000000..eacd659 --- /dev/null +++ b/Programmierung2/src/Übungen/BreakOut/GamePlay.java @@ -0,0 +1,229 @@ +package Übungen.BreakOut; +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.util.Random; + +public class GamePlay extends JPanel implements Runnable,KeyListener,ActionListener { + + // Schläger + final int WIDTH = 80; + final int HEIGHT = 10; + int positionX = 700 / 2; + int positionY = 700 - HEIGHT - 50; + int bewegung = 0; + Rectangle schläger; + + // Ball + final int WIDTHball = 10; + final int HEIGHTball = 10; + int positionXball = 700 / 2; + int positionYball = 700 - HEIGHT - 90; + double bewegungxball = 1 ; + double bewegungyball = 1; + Rectangle ball; + + Random rand = new Random(); + + + int anzahlRects = 18; + int[][] spielField = new int[3][6]; + + // Startpositionen für die Blöcke + int posX = 60; + int posY = 60; + int width = 80; + int height = 10; + + + Rectangle schlägerGame; + + boolean gameOver = false; + + + GamePlay(){ + this.schläger = new Rectangle(positionX,positionY,WIDTH,HEIGHT); + this.ball = new Rectangle(positionXball,positionYball,WIDTHball,HEIGHTball); + fülleMitEinsen(); + schlägerGame = new Rectangle(posX,posY,width,height); + + + + this.addKeyListener(this); + this.setFocusable(true); + this.setFocusTraversalKeysEnabled(false); + + + + } + + public void fülleMitEinsen() { + + for (int i = 0; i < 3; i++) + for (int j = 0; j < 6;j++) + spielField[i][j] = 1; + } + + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + + // Screen + g.setColor(Color.BLACK); + g.fillRect(0, 0, 700, 700); + + // Schläger + g.setColor(Color.WHITE); + g.fillRect(schläger.x, schläger.y, schläger.width, schläger.height); + + // Ball + g.setColor(Color.WHITE); + g.fillOval(ball.x, ball.y, ball.width, ball.height); + + // Border + g.setColor(Color.YELLOW); + g.fillRect(0, 0, 700, 3); + g.fillRect(0, 0, 3, 700); + g.fillRect(680, 0, 3, 650); + + // Blöcke zeichnen + int initialPosX = 60; + int initialPosY = 60; + final int width = 80; + final int height = 10; + + posY = initialPosY; + + for (int i = 0; i < 3; i++) { + posX = initialPosX; + for (int j = 0; j < 6; j++) { + if (spielField[i][j] > 0) { + g.setColor(Color.RED); + g.fillRect(posX, posY, width, height); + } + posX += width + 10; // 10 ist für Spacing + } + posY += height + 10; + } + } + + public void mov() { + checkKollision(); + // MovRect + schläger.x += bewegung; + + if (schläger.x < 0) + schläger.x = 0; + + if (schläger.x > getWidth()) + schläger.x = 600; + + // MovBall + ball.x += bewegungxball; + ball.y -= bewegungyball; + + if (ball.x <= 0 || ball.x >= 670) + bewegungxball = -bewegungxball; + + if (ball.y <= 0 || ball.intersects(schläger)) + bewegungyball = -bewegungyball; + + + repaint(); + } + + public void setRandX(int x) { + bewegungxball = x; + } + + public void setRandY(int x) { + bewegungyball = x; + } + + public void checkKollision() { + // Kollision mit Blöcken überprüfen + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 6; j++) { + if (spielField[i][j] > 0) { + int blockX = 60 + j * (width); + int blockY = 60 + i * (height); + + Rectangle block = new Rectangle(blockX, blockY, width, height); + + if (ball.intersects(block)) { + spielField[i][j] = 0; + + if (ball.x + ball.width - bewegungxball <= block.x || ball.x - bewegungxball >= block.x + block.width) { + bewegungxball = -bewegungxball; + } else { + bewegungyball = -bewegungyball; + } + } + } + } + } + + repaint(); + } + + public void setBewegungRect(int x) { + bewegung = x; + } + + + + + @Override + public void run() { + try { + while (!gameOver) { + mov(); + Thread.sleep(2); + } + } catch (Exception e) { + System.err.println(e.getMessage()); + } + + } + + + @Override + public void keyTyped(KeyEvent e) { + // TODO Auto-generated method stub + + } + + + @Override + public void keyPressed(KeyEvent e) { + if (e.getKeyCode() ==KeyEvent.VK_A) + setBewegungRect(-1); + + if (e.getKeyCode() ==KeyEvent.VK_D) + setBewegungRect(+1); + + + } + + + @Override + public void keyReleased(KeyEvent e) { + if (e.getKeyCode() ==KeyEvent.VK_A || e.getKeyCode() ==KeyEvent.VK_D) + setBewegungRect(0); + + } + + + @Override + public void actionPerformed(ActionEvent e) { + // TODO Auto-generated method stub + + } + + + +} \ No newline at end of file diff --git a/Programmierung2/src/Übungen/BreakOut/Window.java b/Programmierung2/src/Übungen/BreakOut/Window.java new file mode 100644 index 0000000..6271ad5 --- /dev/null +++ b/Programmierung2/src/Übungen/BreakOut/Window.java @@ -0,0 +1,36 @@ +package Übungen.BreakOut; + +import java.awt.Color; + +import java.awt.*; +import javax.swing.*; + +public class Window extends JFrame { + + final int WIDTH = 700; + final int HEIGHT = 700; + GamePlay gamePlay; + + Window() { + gamePlay = new GamePlay(); + + this.setTitle("Breakout Game"); + this.setBounds(10, 10, WIDTH, HEIGHT); + this.setResizable(false); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setLocationRelativeTo(null); + this.getContentPane().setBackground(Color.BLACK); + this.setVisible(true); + + this.add(gamePlay); + } + + public static void main(String[] args) { + Window game1 = new Window(); + Thread gamePlay = new Thread(game1.gamePlay); + gamePlay.start(); +// +// Ball b1 = new Ball(); + } + +}