2017-05-01 4 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]를 읽으십시오. 나는 당신이 실제 사각형 질문에 초점을 맞추기 위해 인용문을 약간 축소시킬 수 있다는 인상을 가지고있다. 예 : boolean 반환 값을 expexting하여 두 직사각형의 기본 속성을 가진 함수를 호출하는 프로그램으로 줄이십시오. 그리고 가독성을 높이기 위해 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