gpt4 book ai didi

Java 矩形相交始终为真

转载 作者:行者123 更新时间:2023-12-02 04:38:06 26 4
gpt4 key购买 nike

我正在为学校开发一个项目,并实现了一种检查两个对象是否相交的方法。我已经定义了两个矩形,但是当它检查它们是否相交时,它总是正确的,即使它们位于 JFrame 的相对侧。 Boolean Collision 在开始时设置为 false,x 和 y 是两张照片在屏幕上的坐标。

public Rectangle Bounds() {
return (new Rectangle(x, y, 225, 225)) ;
}
public Rectangle TrollBounds() {
return (new Rectangle(x, y, 24, 24)) ;
}
Rectangle hitbox = Bounds() ;
Rectangle Hitbox2 = TrollBounds() ;

检查交叉点的部分

if (hitbox.intersects(Hitbox2)) {
collision = true ;
System.out.println("collision") ;
} else {
collision = false ;

全类同学

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Screen extends JPanel implements Runnable, KeyListener {

int health = 1200;

boolean collision = false ;

BufferedImage Troll ;
int cubex = 600 ;
int cubey = 100 ;

int cubexx = 200 ;
int cubeyy = 200 ;

public boolean running = true ;
BufferedImage Player;
int x = 100 ;
int y = 100 ;

boolean Jumping = false ;
int startJump = 10 ;

public void Jump() {

}

boolean up = false;
boolean down = false;
boolean left = false;
boolean right = false;

public Screen() {
loadImages();
Thread thread = new Thread(this);
thread.start();
}

private void loadImages() {
try {
Player = ImageIO.read(getClass().getResource("/sanic.png"));
Troll = ImageIO.read(getClass().getResourceAsStream("/Trollface.png")) ;
} catch (IOException e) {
e.printStackTrace();
}
}

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == KeyEvent.VK_W) {
up = true ;
}
if (e.getKeyCode() == KeyEvent.VK_A) {
left = true ;
}
if (e.getKeyCode() == KeyEvent.VK_D) {
right = true ;
}
if (e.getKeyCode() == KeyEvent.VK_S) {
down = true ;
}
if (e.getKeyCode() == 31) {
Jump() ;
}


}

public void keyReleased(KeyEvent e) {

if (e.getKeyCode() == 87) {
up = false ;
}
if (e.getKeyCode() == 65) {
left = false;
}
if (e.getKeyCode() == 68) {
right = false ;
}
if (e.getKeyCode() == 83) {
down = false ;
}

}

@Override
public void keyTyped(KeyEvent e) {


}

@Override
public void run() {

while(running) {
//gravity

//gravity
//Player movement
if (left) {
if (x <= -10) {
x = 1464 ;
}
x = x - 2 ;
}
if (up) {
if(y <= -112) {
y = 910 ;
}
y = y - 2 ;
}
if (right) {
if (x >= 1416) {
x = -24 ;
}
x = x + 2;
}
if (down) {
if (y >= 900) {
y = -10 ;
}
y = y + 2 ;
}
//Player movement

//ball movement
if (cubey > y) {
cubey-- ;
}
if(cubey < y) {
cubey++ ;
}
if (cubex > x) {
cubex-- ;
}
if (cubex < x) {
cubex++ ;
}
//ball movement

if (hitbox.intersects(Hitbox2)) {
collision = true ;
System.out.println("collision") ;
} else {
collision = false ;
}

if (collision) {
health -- ;
}
System.out.println(collision) ;

repaint() ;

try {
Thread.sleep(3) ;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//Graphical loop start
g.drawImage(Player, x, y, null) ;
g.drawImage(Troll, cubex, cubey, null) ;
g.drawString("SANIC", x, y) ;
g2d.fillRect(220, 200, health, 50) ;
//Graphical loop end

}

public Rectangle Bounds() {
return (new Rectangle(x,y,cubex+225,cubey+225)) ;
}
public Rectangle TrollBounds() {
return (new Rectangle(cubex,cubey,cubex+24,cubey+24)) ;
}
Rectangle hitbox = Bounds() ;
Rectangle Hitbox2 = TrollBounds() ;

}

最佳答案

如果此代码按照顺序运行,使 hitbox 和 Hitbox2 紧随其后,并且中间不更改 x 和 y,那么它应该返回 true。

它将创建一个从 x, y 到 255, 255 的矩形,以及另一个从 x,y 到 24,24 的矩形,并且这些矩形确实相交。

这是关于 Rectangle 的 JavaDoc & Shape.intersects()

关于Java 矩形相交始终为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30521135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com