我一直试图在我正在做的Frogger游戏中进行碰撞检测。似乎无法掌握它,我会感谢一些帮助它让它工作,我的代码如下:
我试图使用以下代码段进行碰撞检测(也见下文),但它并没有完全按照我的意愿行事。当其中一个移动圆达到'青蛙'的x值时,它只会使测试文本出现在屏幕上。
if ( xCoords[0] + radius == movx)
collision = true;
import java.applet.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
public class AppletMove extends Applet implements Runnable, KeyListener
{
int movx = 400;
int movy = 300;
int i = 400;
int y = 20;
int dx = 2;
int dy = 2;
int radius = 10;
int[] coOrds = {20,550};
private Image img;
private Graphics grph;
boolean collision = false;
@Override
public void init()
{
setSize(800,600);
addKeyListener(this);
}
@Override
public void start()
{
Thread t = new Thread(this);
t.start();
}
@Override
public void update(Graphics g) {
if ( img == null)
{
img = createImage(this.getSize().width, this.getSize().height);
grph = img.getGraphics();
}
grph.setColor(getBackground());
grph.fillRect(0, 0, this.getSize().width, this.getSize().height);
grph.setColor(getForeground());
paint(grph);
g.drawImage(img, 0, 0, this);
}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_LEFT:
movx -=20;
break;
case KeyEvent.VK_RIGHT:
movx +=20;
break;
case KeyEvent.VK_UP:
movy -=20;
break;
case KeyEvent.VK_DOWN:
movy +=20;
break;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
int[] xCoords = {20,280,50,250,80,220,110,190};
int[] yCoords = {20,50,80,110,140,170,200,230};
int[] xCoords2 = {140,150,170,110,200,80,230,50};
@Override
public void run() {
// TODO Auto-generated method stub
while(true)
{
if ( xCoords[0] + radius == movx)
collision = true;
if ( xCoords[0] + dx > this.getWidth())
xCoords[0] = 10;
xCoords[0] += dx;
if ( xCoords[1] + dx < 0)
xCoords[1] = 290;
xCoords[1] -= dx;
if ( xCoords[2] + dx > this.getWidth())
xCoords[2] = 10;
xCoords[2] += dx;
if ( xCoords[3] + dx < 0)
xCoords[3] = 290;
xCoords[3] -= dx;
if ( xCoords[4] + dx > this.getWidth())
xCoords[4] = 10;
xCoords[4] += dx;
if ( xCoords[5] + dx < 0)
xCoords[5] = 290;
xCoords[5] -= dx;
if ( xCoords[6] + dx > this.getWidth())
xCoords[6] = 10;
xCoords[6] += dx;
if ( xCoords[7] + dx < 0)
xCoords[7] = 290;
xCoords[7] -= dx;
if ( xCoords2[0] + dx > this.getWidth())
xCoords2[0] = 10;
xCoords2[0] += dx;
if ( xCoords2[1] + dx < 0)
xCoords2[1] = 290;
xCoords2[1] -= dx;
if ( xCoords2[2] + dx > this.getWidth())
xCoords2[2] = 10;
xCoords2[2] += dx;
if ( xCoords2[3] + dx < 0)
xCoords2[3] = 290;
xCoords2[3] -= dx;
if ( xCoords2[4] + dx > this.getWidth())
xCoords2[4] = 10;
xCoords2[4] += dx;
if ( xCoords2[5] + dx < 0)
xCoords2[5] = 290;
xCoords2[5] -= dx;
if ( xCoords2[6] + dx > this.getWidth())
xCoords2[6] = 10;
xCoords2[6] += dx;
if ( xCoords2[7] + dx < 0)
xCoords2[7] = 290;
xCoords2[7] -= dx;
//y += dy;
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void paint(Graphics g)
{
if(collision == true){
g.setColor(Color.BLACK);
g.drawString("Test", 50, 50);
}
g.setColor(Color.GREEN);
g.fillOval(xCoords[0] - radius, yCoords[0] - radius, radius * 2, radius * 2);
g.setColor(Color.RED);
g.fillOval(xCoords[1] - radius, yCoords[1] - radius, radius * 2, radius * 2);
g.setColor(Color.YELLOW);
g.fillOval(xCoords[2]- radius, yCoords[2] - radius, radius * 2, radius * 2);
g.setColor(Color.BLUE);
g.fillOval(xCoords[3] - radius, yCoords[3] - radius, radius * 2, radius * 2);
g.setColor(Color.PINK);
g.fillOval(xCoords[4] - radius, yCoords[4] - radius, radius * 2, radius * 2);
g.setColor(Color.BLACK);
g.fillOval(xCoords[5] - radius, yCoords[5] - radius, radius * 2, radius * 2);
g.setColor(Color.MAGENTA);
g.fillOval(xCoords[6] - radius, yCoords[6] - radius, radius * 2, radius * 2);
g.setColor(Color.GRAY);
g.fillOval(xCoords[7] - radius, yCoords[7] - radius, radius * 2, radius * 2);
g.setColor(Color.GREEN);
g.fillOval(xCoords2[0] - radius, yCoords[0] - radius, radius * 2, radius * 2);
g.setColor(Color.RED);
g.fillOval(xCoords2[1] - radius, yCoords[1] - radius, radius * 2, radius * 2);
g.setColor(Color.YELLOW);
g.fillOval(xCoords2[2] - radius, yCoords[2] - radius, radius * 2, radius * 2);
g.setColor(Color.BLUE);
g.fillOval(xCoords2[3] - radius, yCoords[3] - radius, radius * 2, radius * 2);
g.setColor(Color.PINK);
g.fillOval(xCoords2[4] - radius, yCoords[4] - radius, radius * 2, radius * 2);
g.setColor(Color.BLACK);
g.fillOval(xCoords2[5] - radius, yCoords[5] - radius, radius * 2, radius * 2);
g.setColor(Color.MAGENTA);
g.fillOval(xCoords2[6] - radius, yCoords[6] - radius, radius * 2, radius * 2);
g.setColor(Color.GRAY);
g.fillOval(xCoords2[7] - radius, yCoords[7] - radius, radius * 2, radius * 2);
g.setColor(Color.BLUE);
Graphics2D g2 = (Graphics2D) g;
g2.fill(new Ellipse2D.Double(movx, movy, 20, 20));
}
}