2014-04-01 3 views
-1

소규모 게임 사본을 만들려고하지만 두 개의 오브젝트 (공과 외륜)가 교차하는지 확인하는 데 문제가 있습니다.브레이크 아웃 게임, 직사각형 충돌 감지

나는 지금 충돌 감지이 방법이 있습니다

public static void handleCollisions() { 

    System.out.println(ball.getBounds());  // just to check that they     
    System.out.println(paddle.getBounds());  //are there and moving correct 

    if (ball.getBounds().intersects(paddle.getBounds())) { 
     System.out.println("collision"); 
    } 
} 

그러나 확실히 사각형이 어떤 점에서 교차에도 불구하고,이 방법은 아무것도 감지하지 않습니다. 나는 수업 중 일부에 뭔가 잘못했다고 가정합니다. 여기서 메인 직업

package assignment1; 

import java.awt.*; 
import java.awt.event.*; 
import java.util.Timer; 
import java.util.TimerTask; 
import java.util.Vector; 
import javax.swing.*; 
import javax.swing.plaf.basic.BasicBorders.RadioButtonBorder; 
import java.util.Random; 



public class Breakout extends TimerTask implements KeyListener {    

public static Vector bricks; 
public static Ball ball; 
public static Paddle paddle; 
public static PaintingPanel panel; 
public static boolean gameRunning; 
public static Breakout game; 

public Breakout() 
{ 

} 


public static void setupGame() 
{ 
    Paddle paddle = new Paddle(350, 790, 100, 10); //initial paddle creation       
    Ball ball = new Ball(393, 790, 7); 

    showGUI(); 
} 

public static void beginGame() 
{ 
    gameRunning = true; 

} 

public void run() 
{ 
    if(gameRunning == true) 
    { 


     Ball.move(); 
     Paddle.move(); 
     handleCollisions(); 




     if(Ball.x < 0 || Ball.x > 800 || Ball.y < 0 || Ball.y > 790){ 
      gameRunning = false; 
      System.out.println("Game over");  
     } 


    } 


} 

public static void stopGame() 
{ 

} 



public static void handleCollisions() 
{ 

    System.out.println(ball.getBounds()); 
    System.out.println(paddle.getBounds()); 

    if (ball.getBounds().intersects(paddle.getBounds())) 
     { 
    System.out.println("Yay"); 
     } 

} 


public static void main(String[] args) 
{ 
    Timer t = new Timer(); 
    t.schedule(new Breakout(), 0, 40); 

    panel = new PaintingPanel(PaintingPanel.contents); 
    setupGame(); 
    beginGame(); 





    while (gameRunning == true) 
    { 
      panel.repaint(); 
    }  

    // TODO a lot 
} 

private static void showGUI() 
{ 

    JFrame frame = new JFrame("Breakout"); 
    game = new Breakout();     

    frame.addKeyListener(game); 

    frame.add(panel); 

    frame.setSize(1000,1000); 
    frame.setLocationRelativeTo(null);       // create game window 
    frame.setVisible(true);         
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 


} 


public void keyPressed(KeyEvent e) { 
    Paddle.keyPressed(e); 
} 


public void keyReleased(KeyEvent e) { 
    Paddle.keyReleased(e); 
} 


public void keyTyped(KeyEvent e) { 
    // TODO Auto-generated method stub 

} 

}

package assignment1; 

import java.awt.*; 
import java.util.Vector; 
import javax.swing.*; 

public class PaintingPanel extends JPanel{ 





public static Vector contents; 


public PaintingPanel(Vector contents) 
{ 
    contents = new Vector(); 
    contents.addElement(Breakout.paddle); 
    contents.addElement(Breakout.ball); 



    System.out.println(contents); 

} 

public void paintComponent(Graphics g) 
{ 

    super.paintComponent(g); 
    Graphics2D g2d = (Graphics2D) g; 
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); 
    g2d.setBackground(Color.GREEN); 
    g2d.drawRect(2, 2, 800, 800); 
    Breakout.paddle.paintThis(g2d); 
    Breakout.ball.paintThis(g2d);  
} 

}

package assignment1; 

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

public class Paddle { 



public static int PADDLE_SIZE = 100; 
public static int PADDLE_THICKNESS = 10; 
public static int centreX = 400;        // X Centre of the paddle 
public static int centreY = 795;        // Y Centre of the paddle 
public static int paddleX = centreX - (PADDLE_SIZE/2);   // Top left X coordinate 
public static int paddleY = centreY -(PADDLE_THICKNESS/2);  // Top left Y coordinate  
public static int dirX; 


public Paddle(int paddleX, int paddleY, int PADDLE_SIZE, int PADDLE_THICKNESS) 
{ 
    this.paddleX = paddleX; 
    this.paddleY = paddleY; 
    this.PADDLE_SIZE = PADDLE_SIZE; 
    this.PADDLE_THICKNESS = PADDLE_THICKNESS; 

    System.out.println("Paddle created at " + paddleX + ", " + paddleY + ", " + PADDLE_SIZE + ", " + PADDLE_THICKNESS); 


} 

public static void paintThis(Graphics g) 
{ 
    g.setColor(Color.BLACK); 
    g.fillRect(paddleX, paddleY, PADDLE_SIZE, PADDLE_THICKNESS); 
    g.drawRect(paddleX, paddleY, PADDLE_SIZE, PADDLE_THICKNESS); 

} 

public static Rectangle getBounds() 
{ 
    return new Rectangle(paddleX, paddleX, PADDLE_SIZE, PADDLE_THICKNESS); 

} 

public static void move() 
{ 
    paddleX = paddleX + dirX; 
} 

public static void keyPressed(KeyEvent e) 
{ 

    if (e.getKeyCode() == KeyEvent.VK_LEFT) 
    { 
     dirX = -10; 
    } 
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) 
    { 
     dirX = 10; 
    } 
    else {} 
} 

public static void keyReleased(KeyEvent e) 
{ 
    dirX = 0; 
} 

}

package assignment1; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Rectangle; 
import java.util.Random; 

public class Ball { 

public static int x; 
public static int y; 
public static int radius; 
public final static Color colour = Color.RED; 
public static double dirX; 
public static double dirY; 
public static int speed = 0; 


public Ball(int x, int y, int radius) 
{  
    this.x = x; 
    this.y = y; 
    this.radius = radius; 
    dirX = 0; 
    dirY = -Math.sqrt(1 - dirX*dirX); 
    System.out.println("Ball created at " + x + ", " + y + ", " + dirX + ", " + dirY); 
} 


public static void paintThis(Graphics g) 
{ 
    g.setColor(colour); 
    int width = radius*2; 
    g.fillOval(x, y, width, width); 
    g.drawOval(x, y, width, width); 
} 


public static Rectangle getBounds() 
{ 
    return new Rectangle(x, y, radius*2, radius*2);      
} 



public static void move() 
{ 
    x = (int) (x-dirX*speed);   //check for inaccuracy 
    y = (int) (y-dirY*speed); 
    System.out.println("Moved, New X = " + x + ", new Y = "+ y); 
} 




public void reflect(boolean horizontal, boolean vertical) 
{ 
    if (vertical == true) 
    { 
     dirX = 0-dirX; 
    } 
    if (horizontal == true) 
    { 
     dirY = 0-dirY; 
    } 
} 


public boolean intersects(Paddle paddle) { 
    if (getBounds().intersects(Paddle.getBounds())){ 
     return true; 
    } 
    else return false; 
} 

}

https://dl.dropboxusercontent.com/u/65678182/assignment1.rar

+1

'getBounds()'와'intersects()'에 대한 코드를 게시하십시오. – csmckelvey

+1

[가능한 직사각형 사이의 교차를 사용하여 충돌 검사를하는 방법] (http://stackoverflow.com/questions/22777498/how-to-make-my-collision-check-with-intersect-between- rectangle-to-work) – csmckelvey

+0

나는 전체 코드에 대한 링크를 게시 했으므로 이제 모든 내용을 확인할 수 있습니다. – user3483682

답변

1

패들 클래스의 getBounds() 메서드가 문제인 것 같습니다. 왼쪽 상단 좌표가 paddleX이고 paddleX 인 사각형을 반환합니다. 대신 return new Rectangle(paddleX, paddleY, PADDLE_SIZE, PADDLE_THICKNESS); 을 의미한다고 생각합니다.

+0

Omg, 나는 바보 같은 실수로 여러 시간 동안 앉아 있었다 .... 감사합니다 : D – user3483682

+0

아무런 문제가 없기 때문에 기꺼이 도와 드리겠습니다. – turingcomplete

+0

사각형이 공을 때리는 쪽을 결정하는 방법에 대한 헤더 객체? 명백하게 외륜과의 교차에 대한 필요성이 아니라 벽 + 벽돌을 위해 그들이 어느 쪽이 그들이 타격을가하는지 알 필요가있다. 그래서 나는 공을 정확한 길을 반영한다. 위선적 인 말투는 아직 이것에 대한 좋은 자습서를 찾는 것 같습니다 : P – user3483682

관련 문제