碰撞和玩家与正方形的交集

问题描述:

我正在制作一个游戏,玩家必须躲避正在从天空坠落的正方形。碰撞和玩家与正方形的交集

我试图让玩家与下降的广场相交或碰撞时,程序会注意到。

我意识到我必须得到下降的格子和球员的界限,但我不知道如何去做。任何帮助深表感谢。

Player类

import javax.swing.*; 
import java.awt.*; 
import java.awt.Rectangle; 


public class Player extends JPanel { 
    private int XLocation; 
    private int squareSize; 
    private int YLocation ; 
    private int MoveSpeed; 
    int xMouse, yMouse; 


public int XLocation(){ 
    return XLocation = 300; 
} 

public int YLocation(){ 
    return YLocation = 750; 
} 

public int SquareSize(){ 
    return squareSize = 20; 
} 

public void paint(Graphics g){ 
    g.setColor(Color.GRAY); 
    g.fillRect(XLocation,YLocation,squareSize,squareSize); 
} 

public Rectangle bounds(){ 
return(new Rectangle(XLocation,YLocation,20,20)); 
} 

public int MoveSpeed(){ 
    return MoveSpeed = 23; 
} 

public Player(){ 
    XLocation(); 
    SquareSize(); 
    MoveSpeed(); 
    YLocation(); 
} 


    public void PlayerMove(java.awt.event.MouseEvent evt){ 
     XLocation = evt.getX(); 
     YLocation = evt.getY(); 
    } 



} 

矩形

import java.awt.*; 
import java.awt.geom.Rectangle2D; 
import java.util.Random; 

public class Square { 

private int XLocation; 
private int Size; 
private int YLocation = -Size; 
private int fallSpeed = 1; 
Random rand = new Random(); 



public int generateRandomXLocation(){ 
    return XLocation = rand.nextInt(Game.WINDOW_WIDTH - Size); 
} 


public int generateRandomSquareSize(){ 
    return Size = rand.nextInt((30-17)+1)+17; 
} 



public int generateRandomFallSpeed(){ 
    return fallSpeed = rand.ints(3, 3, 8).findFirst().getAsInt(); 
} 


public void paint(Graphics g){ 
    g.setColor(Color.BLACK); 
    g.fillRect(XLocation,YLocation,Size,Size); 
} 


public Square(){ 
    generateRandomXLocation(); 
    generateRandomSquareSize(); 
    generateRandomFallSpeed(); 
} 



public void update(){ 


    if(YLocation >= Game.WINDOW_HEIGHT){ 
     generateRandomXLocation(); 
     generateRandomFallSpeed(); 
     generateRandomSquareSize(); 
     YLocation = -Size; 
    } 

    //moves the square down if the square is inside the window 
    if(YLocation <= Game.WINDOW_HEIGHT){ 
     YLocation += fallSpeed; 
    } 
} 
public void GetBounds(){ 
    Rectangle rectangle = new Rectangle(XLocation,YLocation,Size,Size); 
} 
} 

类游戏类

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.MouseEvent; 
import java.awt.image.BufferedImage; 

public class Game extends JPanel { 

public static final int WINDOW_WIDTH = 600; 
public static final int WINDOW_HEIGHT = 900; 


Square[] squareArray = new Square[20]; 

Player thePlayer = new Player(); 


public Game() { 

    //initializes square objects 
    for (int i = 0; i < squareArray.length; i++) 
     squareArray[i] = new Square(); 


} 

public void paint(Graphics graphics) { 


    graphics.setColor(Color.WHITE); 
    graphics.fillRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); 

    //paints square objects to the screen 
    for (Square aSquareArray : squareArray) { 
     aSquareArray.paint(graphics);  
    } 
    thePlayer.paint(graphics); 

} 

public void update() { 
    for (Square aSquareArray : squareArray) aSquareArray.update(); 
} 



    private void mouseMove(MouseEvent evt) { 
     thePlayer.PlayerMove(evt); 
} 


public void collision(){ 
    Rectangle rectangle1 = thePlayer.bounds(); 
    } 


public static void main(String[] args) throws InterruptedException { 

    Game game = new Game(); 
    Player thePlayer = new Player(); 

    JFrame frame = new JFrame(); 
    frame.add(game); 
    frame.setVisible(true); 
    frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); 
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
    frame.setTitle("Raining Multiple Squares"); 
    frame.setResizable(false); 
    frame.setLocationRelativeTo(null); 

    BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); 


    Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
cursorImg, new Point(0, 0), "blank cursor"); 


    frame.getContentPane().setCursor(blankCursor); 


      frame.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { 
     public void mouseMoved(java.awt.event.MouseEvent evt) { 
      mouseMove(evt); 
     } 

     private void mouseMove(MouseEvent evt){ 
      game.mouseMove(evt); 
     } 
    }); 

    while (true) { 
     game.update(); 
     game.repaint(); 
     Thread.sleep(10); 
    } 
} 





} 
+0

不知道我明白。你知道当前的'XLocation','YLocation'和size,所以你应该能够计算出这些边界。有什么问题? – ajb

+0

我不知道如何获得我的方阵数组的边界 – Karavi

+0

每个'Square'都有一个'GetBounds'方法。这不正常吗?为什么你不能在数组中的每个方块上调用'GetBounds'? – ajb

可以测试碰撞要绘制的平方以同样的方式。

public void collision(){ 
    Rectangle rectangle1 = thePlayer.bounds();//player rectangle 
    for (Square square: squareArray) { 
     if(square.GetBounds().intersects(rectangle1)){//testing all squares vs player 
      //if you are here a collision happened 
      //you can remome square add score ... 
     } 
    } 
} 

intersects方法测试this矩形反对票要传递到,如果两个rectagles相交的返回true rectagle。

并且不要忘记在游戏循环的每次迭代中调用collision方法一次。