我正在尝试制作突破游戏的副本,但我在检查两个物体(球和球拍)是否相交时遇到问题。
我现在有这种碰撞检测方法:
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
答案 0 :(得分:1)
Paddle类中的getBounds()
方法似乎是个问题。您将返回一个左上角坐标为paddleX
和paddleX
的矩形。我想你的意思
return new Rectangle(paddleX, paddleY, PADDLE_SIZE, PADDLE_THICKNESS);
代替。