-1
私は空から落ちてくる四角形をかわすゲームを作っています。プレーヤーと四角の衝突と交差
私は、プレーヤーが落ちている四角と交差したり衝突したりすると、プログラムが気付くようにしようとしています。
私は落ちてくる四角形とプレーヤーの境界線を取らなければならないことに気づいていますが、その方法はわかりません。どんな助けでも大歓迎です。
プレーヤークラス
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();
}
}
Rectangleクラス
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);
}
}
}
わかりません。現在の 'XLocation'、' YLocation'、およびsizeを知っているので、それらの境界を計算できるはずです。どうしたの? – ajb
私の四角形の配列の境界を取得する方法を知りません – Karavi
各 'Square'は' GetBounds'メソッドを持っています。これは機能していないのですか?あなたの配列の各正方形で 'GetBounds'を呼び出せないのはなぜですか? – ajb