2014-04-01 176 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-矩形對工作) – csmckelvey

+0

我沒有發佈一個鏈接到整個代碼,所以現在你可以檢查所有 – user3483682

回答

1

Paddle類中的getBounds()方法似乎是問題所在。您將返回左上角座標爲paddleXpaddleX的矩形。我想你的意思是代替 return new Rectangle(paddleX, paddleY, PADDLE_SIZE, PADDLE_THICKNESS);

+0

Omg,我一直坐在這樣一個愚蠢的錯誤許多小時....謝謝你:D – user3483682

+0

沒問題,很高興我可以幫助:) – turingcomplete

+0

任何標題如何確定球將擊中矩形的哪一側一個東西?顯然不需要與槳的交點,但對於牆+磚我需要知道他們擊中哪一側,所以我以正確的方式反射球。似乎找不到一個很好的教程:P – user3483682