为什么我的applet游戏中的碰撞算法无法正常工作?

时间:2014-06-17 14:24:27

标签: java applet awt collision-detection

这是我的游戏项目。这是“Flappy Bird”的非常简单的版本,我对碰撞算法的工作方式有一些严重的问题。我为wall1和wall2写了2个单独的代码片段用于碰撞。当球试图通过一个洞时,问题就开始了,因为程序正在检测与墙的碰撞。我几乎肯定碰撞算法写得正确,因为我整天都在检查它。

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Game extends Applet implements KeyListener, Runnable {

    Image bground;
    Random generator = new Random();
    int r1;
    int wall1x;
    int wall1y;
    int wall1long;
    int wall2x;
    int wall2y;
    int wall2long;
    Timer timer;

    private Image i;
    private Graphics doubleG;
    int r2;
    int blok_x1 = 800;
    int blok_y1;
    int blok_x = 800;
    int blok_y = 0;
    int blok_x_v = 2;
    int ballX = 20;
    int ballY = 20;
    int dx = 0;
    int dyclimb = 1;
    int dyrise = 1;

    double gravity = 3;
    double jumptime = 0;
    int FPS = 100;
    public int tab[];
    public boolean grounded = true, up = false;
    boolean OVER = false;

    @Override
    public void init() {
        bground = getImage(getCodeBase(), "12.png");
        this.setSize(600, 400);
        tab = new int[100];
        for (int t = 0; t < 100; t++) {
            tab[t] = generator.nextInt(380) + 1;
        }
        addKeyListener(this);
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            ballX -= 10;

        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            ballX += 10;

        }
        if (e.getKeyCode() == KeyEvent.VK_UP) {
            ballY -= 10;

            up = true;
        }
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            ballY += 10;

        }

    }

    @Override
    public void paint(Graphics g) {
        g.drawImage(bground, 0, 0, this);
        if (OVER == false) {

            for (int i = 0; i < 100; i++) {
                g.setColor(Color.green);
                wall1x = blok_x + i * 400;
                wall1y = blok_y;
                wall1long = tab[i];

                g.fillRect(wall1x, wall1y, 20, wall1long);
                g.setColor(Color.green);
                wall2x = blok_x1 + i * 400;
                wall2y = tab[i] + 60;
                wall2long = 400 - tab[i];
                g.fillRect(wall2x, wall2y, 20, wall2long);
                g.setColor(Color.green);

            }
            g.setColor(Color.red);
            g.fillOval(ballX, ballY, 20, 20);
        } else {
            g.drawString("GAME OVER", 300, 300);
        }
    }

    @Override
    public void update(Graphics g) {
        if (i == null) {
            g.setColor(Color.green);
            i = createImage(this.getSize().width, this.getSize().height);
            doubleG = i.getGraphics();
            g.setColor(Color.green);
        }
        doubleG.setColor(getBackground());
        g.setColor(Color.green);
        doubleG.fillRect(0, 0, this.getSize().width, this.getSize().height);
        doubleG.setColor(getForeground());
        g.setColor(Color.green);
        paint(doubleG);
        g.drawImage(i, 0, 0, this);

    }

    @Override
    public void run() {
        int time = 10;

        while (true) {
            if (up == true) {

                ballY -= dyclimb;
                time--;
            } else {
                ballY += dyrise;
            }
            if (time == 0) {
                time = 10;
                up = false;
            }

            blok_x--;
            blok_x1--;

            if (ballX > 600 || ballX < 0 || ballY > 400 || ballY < 0) {
                OVER = true;

            }

            for (int i = 0; i < 100; i++) {                // collision algorithm
                wall1x = blok_x + i * 400;
                wall1y = blok_y;
                wall1long = tab[i];

                if (ballX + 20 >= wall1x && ballX <= wall1x + 20 && ballY <= wall1y + wall1long && ballY >= wall1x - 20) {  //wall1
                    OVER = true;

                }

            }
            for (int i = 0; i < 100; i++) {
                wall2x = blok_x1 + i * 400;
                wall2y = tab[i] + 60;
                wall2long = 400 - tab[i];

                if (ballX + 20 >= wall2x && ballX <= wall2x + 20 && ballY <= wall2y + wall2long && ballY >= wall2x - 20) {  //wall2
                    OVER = true;

                }

            }
            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException ex) {
                Logger.getLogger(NewApplet.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    @Override
    public void start() {
        Thread thread = new Thread(this);
        thread.start();

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

}

1 个答案:

答案 0 :(得分:0)

使用Rectangle类。有一种名为Intersect或Intersection的方法或类似的东西。

假设你有一个物体移动。创建一个Rectangle以匹配位置上的对象(基本上是对象的隐形覆盖)。

与另一个对象做同样的事情。

当两者都要碰撞时,使用交叉方法通过使用矩形来检查碰撞。

这些可能会有所帮助 http://docs.oracle.com/javase/7/docs/api/java/awt/Rectangle.html

Java method to find the rectangle that is the intersection of two rectangles using only left bottom point, width and height?

java.awt.Rectangle. intersection()