2017-05-01 124 views
0

我試圖讓它打印出「遊戲結束」的時候,綠化廣場(長方體)運行在/成藍色(CuboidKiller)之一。檢測用碰撞填充的矩形

遊戲類:

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; 
    } 

} 

長方體類:

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; 
    } 

} 

長方體殺手:

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

歡迎來到StackOverflow。請閱讀[mcve]。我有這樣的印象,你可以縮小你的引用一點,以專注於實際的矩形問題。例如。減少到用兩個矩形的基本屬性調用一個函數的程序,預測布爾返回值。也許你可以更好地熟悉StackOverflow提供的格式選項,以提高可讀性。 – Yunnosch

回答

0

嗯,有兩種方法。要麼你自己寫,要麼只是使用Java 8提供的東西。

這傢伙如何兩個矩形之間的碰撞檢測一個很好的解釋:Java check if two rectangles overlap at any point

但是,如果我是一個寫它,我只想有兩個類都包含一個Rectangle對象(http://docs.oracle.com/javase/8/docs/api/java/awt/Rectangle.html),只是請撥打Rectangle提供的intersects()功能。 :-)

+0

明天我會試試這個。非常感謝你。 – TehBunk

+0

該解決方案的工作原理是驚人的。非常感謝你。 – TehBunk

+0

很高興我可以幫助:) – Thoma