2017-05-01 5 views
0

Ich versuche es "Game over" zu machen auszudrucken, wenn die Green Square (Cuboid) läuft über/in den blauen (CuboidKiller) ein.Detecting Kollision zwischen zwei gefüllten Rectangles

GAME Klasse:

package plugin.dev.wristz; 

import java.awt.Graphics; 
import java.awt.Image; 
import java.util.ArrayList; 
import java.util.Random; 

import javax.swing.JFrame; 

public class Game extends JFrame { 

    private static final long serialVersionUID = 294623570092988970L; 

    public static ArrayList<CuboidKiller> killers; 

    public static int h = 1024, w = 768; 

    public static Game game; 
    public static Graphics graphics, g2; 
    public static Image image; 

    public static Cuboid cuboid; 

    public Game(String title) { 
     setTitle(title); 
     setSize(1024, 768); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setResizable(true); 
     setVisible(true); 
     setLocationRelativeTo(null); 
     addKeyListener(new KeyHandler(cuboid)); 

     g2 = getGraphics(); 
     paint(g2); 
    } 

    public static void main(String[] args) { 
     cuboid = new Cuboid(); 
     Thread cubi = new Thread(cuboid); 
     cubi.start(); 

     killers = new ArrayList<CuboidKiller>(); 

     CuboidKiller a = new CuboidKiller(new Random().nextInt(h), new Random().nextInt(w), new Random().nextInt(50) + 20); 

     killers.add(a); 

     game = new Game("Killer Cuboids"); 
    } 

    @Override 
    public void paint(Graphics g) { 
     image = createImage(getWidth(), getHeight()); 
     graphics = image.getGraphics(); 
     paintComponent(graphics); 
     g.drawImage(image, 0, 0, this); 

    } 

    public void paintComponent(Graphics g) { 
     checkGameOver(); 

     cuboid.draw(g); 

     for (CuboidKiller killer : killers) 
      killer.draw(g); 

     repaint(); 
    } 

    public void checkGameOver() { 
     for (CuboidKiller killer : killers) 
      if (killer.isTouching(cuboid)) 
       System.out.println("Game over!"); 

    } 

    public int getH() { 
     return h; 
    } 

    public void setH(int wh) { 
     h = wh; 
    } 

    public int getW() { 
     return w; 
    } 

    public void setW(int ww) { 
     w = ww; 
    } 

} 

Cuboid Klasse:

package plugin.dev.wristz; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.KeyEvent; 

@SuppressWarnings("static-access") 
public class Cuboid implements Runnable { 

    private int x, y, xDirection, zDirection; 

    public Cuboid() { 
     this.x = 799; 
     this.y = 755; 
    } 

    public void draw(Graphics g) { 
     g.setColor(Color.GREEN); 

     g.fillRect(x, y, 25, 25); 
    } 

    public void move() { 

     x += xDirection; 
     y += zDirection; 

     if (x <= 10) 
      x = 0 + 10; 

     if (y <= 35) 
      y = 0 + 35; 

     if (x >= 1024 - 35) 
      x = 1024 - 35; 

     if (y >= 768 - 35) 
      y = 768 - 35; 

    } 

    public void keyPressed(KeyEvent ev) { 
     int keyCode = ev.getKeyCode(); 

     if (keyCode == ev.VK_LEFT) { 
      setXDirection(-5); 
     } 

     if (keyCode == ev.VK_RIGHT) { 
      setXDirection(5); 
     } 

     if (keyCode == ev.VK_UP) { 
      setZDirection(-5); 
     } 

     if (keyCode == ev.VK_DOWN) { 
      setZDirection(5); 
     } 
    } 

    public void keyReleased(KeyEvent ev) { 
     int keyCode = ev.getKeyCode(); 

     if (keyCode == ev.VK_LEFT) { 
      setXDirection(0); 
     } 

     if (keyCode == ev.VK_RIGHT) { 
      setXDirection(0); 
     } 

     if (keyCode == ev.VK_UP) { 
      setZDirection(0); 
     } 

     if (keyCode == ev.VK_DOWN) { 
      setZDirection(0); 
     } 

    } 

    @Override 
    public void run() { 
     try { 
      while (true) { 
       move(); 
       Thread.sleep(5); 

      } 
     } catch (Exception e) { 
      System.err.println(e.getMessage()); 
     } 
    } 

    public int getX() { 
     return x; 
    } 

    public void setX(int x) { 
     this.x = x; 
    } 

    public int getY() { 
     return y; 
    } 

    public void setY(int y) { 
     this.y = y; 
    } 

    public int getXDirection() { 
     return xDirection; 
    } 

    public void setXDirection(int xDirection) { 
     this.xDirection = xDirection; 
    } 

    public int getZ() { 
     return y; 
    } 

    public void setZ(int z) { 
     this.y = z; 
    } 

    public int getZDirection() { 
     return zDirection; 
    } 

    public void setZDirection(int zDirection) { 
     this.zDirection = zDirection; 
    } 

} 

Cuboid Killer:

package plugin.dev.wristz; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.util.HashMap; 

public class CuboidKiller { 

    private int x, y, radius; 

    private HashMap<Integer, Integer> points; 

    public CuboidKiller(int x, int y, int radius) { 
     this.points = new HashMap<Integer, Integer>(); 
     setPoints(); 
     this.x = x; 
     this.y = y; 
     this.radius = radius; 
    } 

    public void draw(Graphics g) { 
     g.setColor(Color.blue); 

     g.fillRect(x, y, radius, radius); 
    } 

    public void setPoints() { 
     this.points.put(x, y); 
     this.points.put(x + radius, y); 
     this.points.put(x + radius, y - radius); 
     this.points.put(x, y - radius); 
    } 

    public boolean isTouching(Cuboid cuboid) { 
     boolean result = true; 

     //int a = cuboid.getX(), b = cuboid.getZ(); 

     result = true; 

     return result; 
    } 

    public int getRadius() { 
     return radius; 
    } 

    public void setRadius(int radius) { 
     this.radius = radius; 
    } 

    public int getY() { 
     return y; 
    } 

    public void setY(int y) { 
     this.y = y; 
    } 

    public int getX() { 
     return x; 
    } 

    public void setX(int x) { 
     this.x = x; 
    } 

    public HashMap<Integer, Integer> getPoints() { 
     return points; 
    } 

    public void setPoints(HashMap<Integer, Integer> points) { 
     this.points = points; 
    } 

} 
+0

Willkommen bei StackOverflow. Bitte lesen [mcve]. Ich habe den Eindruck, dass Sie Ihre Zitate etwas verkleinern könnten, um sich auf die eigentliche Rechteckfrage zu konzentrieren. Z.B. Reduziere auf ein Programm, das eine Funktion mit den Basisattributen der zwei Rechte aufruft, was einen booleschen Rückgabewert voraussetzt. Und vielleicht können Sie sich besser mit den Formatierungsmöglichkeiten von StackOverflow vertraut machen, um die Lesbarkeit zu verbessern. – Yunnosch

Antwort

0

Nun, es gibt zwei Ansätze. Entweder schreibst du es selbst oder du verwendest einfach das, was Java 8 bietet.

Dieser Typ hat eine sehr schöne Erklärung, wie eine Kollision zwischen zwei Rechtecken erkennen: Java check if two rectangles overlap at any point

Aber wenn ich das einem Schreiben ihm wäre, würde ich nur beiden Klassen enthalten ein Rectangle Objekt (http://docs.oracle.com/javase/8/docs/api/java/awt/Rectangle.html), und nur Rufen Sie die intersects()-Funktion an, die von Rectangle bereitgestellt wird. :-)

+0

Ich werde das morgen versuchen. Vielen Dank. – TehBunk

+0

Diese Lösung funktioniert erstaunlich. Vielen Dank. – TehBunk

+0

Froh, dass ich helfen könnte :) – Thoma