java中的碰撞检测(平铺地图)

时间:2014-08-30 20:40:52

标签: java map collision-detection tile

这里我有一个代码,图片在地图上移动。

如何在与地图图块交互时使这张照片更清晰?我是java的初学者,所以我没有想法。非常欢迎任何帮助或建议。

代码:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.Timer;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

import org.omg.CORBA.portable.InputStream;

import java.io.Reader;

public class NewTest extends JFrame implements KeyListener {
/**
 * 
 */
private static final long serialVersionUID = 1L;
private Image TestImage;
private BufferedImage bf;
private BufferedImage bufferedImage;
private int cordX = 100;
private int cordY = 100;
public int mapy=25;
public int mapx=mapy;
 public int size= 20;
private boolean down, up, left, right;
private Image wall = Toolkit.getDefaultToolkit().getImage("image/Koala.jpg");
private Image no = Toolkit.getDefaultToolkit().getImage("image/house.jpg");

public static int[][] 
        map =               {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                             {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                            {0, 10, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 11},
                            {0, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 3, 0, 4, 0, 5, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
                            {0, 12, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 13}};

public NewTest() throws IOException {
    setTitle("Testing....");
    setSize(mapy*size+50,mapx*size+50);
    imageLoader();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

public void imageLoader() throws IOException {
    TestImage = Toolkit.getDefaultToolkit().getImage("image/Koala.jpg");

    addKeyListener(this);
}

public void update(Graphics g){
       paint(g);
}


public void paint(Graphics g){

    bf = new BufferedImage( this.getWidth(),this.getHeight(), BufferedImage.TYPE_INT_RGB);

try{
animation(bf.getGraphics());
g.drawImage(bf,0,0,null);
}catch(Exception ex){

}


}

public void animation(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D)g;
    for (int y = 0; y <= mapy; y++){
        for (int x = 0; x <= mapx; x++){

            int L = x * size;
            int U = y * size;
            int R = size;
            int D = size;
               /// g2d.rotate(Math.toRadians(-90));

            if (map[y][x] == 1){
                //no black wall
                g2d.setColor(Color.green);
                g2d.fillRect(L, U, R, D);


            }else if (map[y][x] == 2){
                //on left
                g2d.setColor(Color.green);
                g2d.fillRect(L, U, R, D);
                g2d.setColor(Color.black);
                g2d.fillRect(L-size/2 +2, U, 8, size);

            }else if (map[y][x] == 3){
                //on right
                g2d.setColor(Color.green);
                g2d.fillRect(L, U, R, D);
                g2d.setColor(Color.black);
                g2d.fillRect(L+size, U, 8, size);

            }else if (map[y][x] == 4){
                //on top
                g2d.setColor(Color.green);
                g2d.fillRect(L, U, R, D);
                g2d.setColor(Color.black);
                g2d.fillRect(L, U-size/2 + 2, size, 8);

            }else if (map[y][x] == 5){
                //on bottom
                g2d.setColor(Color.green);
                g2d.fillRect(L, U, R, D);
                g2d.setColor(Color.black);
                g2d.fillRect(L, U+size, size, 8);
            }else if (map[y][x] == 10){
                //on bottom
                g2d.setColor(Color.green);
                g2d.fillRect(L, U, R, D);
                g2d.setColor(Color.black);
                g2d.fillRect(L, U-size/2 + 2, size, 8);
                g2d.fillRect(L-size/2 +2, U, 8, size );
            }else if (map[y][x] == 11){
                //on bottom
                g2d.setColor(Color.green);
                g2d.fillRect(L, U, R, D);
                g2d.setColor(Color.black);
                g2d.fillRect(L, U+size, size, 8);
            }else if (map[y][x] == 12){
                //on bottom
                g2d.setColor(Color.green);
                g2d.fillRect(L, U, R, D);
                g2d.setColor(Color.black);
                g2d.fillRect(L, U+size, size, 8);
            }else if (map[y][x] == 13){
                //on bottom
                g2d.setColor(Color.green);
                g2d.fillRect(L, U, R, D);
                g2d.setColor(Color.black);
                g2d.fillRect(L, U+size, size, 8);
            }

        }

    }
    g.drawImage(TestImage, cordX, cordY,20 , 20, this);

   }


public static void main(String[] args) {
    try {
        new NewTest();
    } catch (IOException e) {

        e.printStackTrace();
    }
}

public void keyPressed(KeyEvent ke) {
    switch (ke.getKeyCode()) {
        case KeyEvent.VK_RIGHT:
            right = true;
            break;
        case KeyEvent.VK_LEFT:
            left = true;
            break;
        case KeyEvent.VK_DOWN:
            down = true;
            break;
        case KeyEvent.VK_UP:
            up = true;
            break;
    }
    updateState();
}

public void keyReleased(KeyEvent ke) {
    switch (ke.getKeyCode()) {
        case KeyEvent.VK_RIGHT:
            right = false;
            break;
        case KeyEvent.VK_LEFT:
            left = false;
            break;
        case KeyEvent.VK_DOWN:
            down = false;
            break;
        case KeyEvent.VK_UP:
            up = false;
            break;
    }
    updateState();
}

protected void updateState() {
    if (right) {
        cordX += 5;
    } else if (left) {
        cordX -= 5;

    }
    if (down) {
        cordY += 5;
    } else if (up) {
        cordY -= 3;
    }
    if (down && up) {
        cordY += 0;
        cordX += 0;
    }
    if (right && left) {
        cordX += 0;
        cordY += 0;
    }

    System.out.printf("X:");System.out.println(cordX);
    System.out.printf("Y:");System.out.println(cordY);
    repaint();
}

public void keyTyped(KeyEvent ke) {
}

}

谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

好的方法是分离视觉和现实模型。它意味着在程序的深处,你有“物理规则”和所有对象,并且基于该模型,你可以绘制它。

您可以从创建类

开始
public class ObjectInMyGame{
  private int posx;
  private int posy;
  private int width;
  private int height;
  private Image image;
  private int movingX;
  private int movingY;
  //Note that it is good to use Point or something like that, instead of posx, posy.
}

然后有一种“游戏控制器”是好的,它正在运行你的整个世界。

public class GameController{
  List<ObjectInMyGame> objects;

  public updateGame(){
    for (ObjectInMyGame object : objects){
      object.doSomething(); //sometimes it is good idea to let objects itself do something
    }
  }
}

例如,当您移动对象时,您可以检查是否发生碰撞然后决定要做什么。