0

我刚刚尝试制作一个地图上几乎没有边界的游戏,它是从文本文档生成的。文本文档有 1 和 0,其中 1 表示一堵墙。那我怎么做才能让角色停在墙前

我的代码:

主类:公共类 JavaGame {

public static void main(String[] args) {
    final Platno p = new Platno();
    final JFrame okno = new JFrame("Test");
    Mapa map = new Mapa();
    okno.setResizable(false);
    okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    okno.setSize(800, 600);
    okno.setVisible(true);

    map.nacti();
    okno.add(p);

    p.mapa = map;


    okno.addKeyListener(new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {
        }

        @Override
        public void keyPressed(KeyEvent e) {
          int kod = e.getKeyCode();
          if(kod == KeyEvent.VK_LEFT)
          {
              Platno.x -= 3;
              p.repaint();
          }
          else if(kod == KeyEvent.VK_RIGHT)
          {
              Platno.x +=3;
              p.repaint();
          }
          else if(kod == KeyEvent.VK_UP)
          {
              Platno.y -=3;
              p.repaint();
          }
          else if(kod == KeyEvent.VK_DOWN)
          {
              Platno.y +=3;
              p.repaint();
          }
        }

        @Override
        public void keyReleased(KeyEvent e) {
        }
    });

 /*   Timer = new Timer(1, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            Platno.y -=3;
            p.repaint();
        }
    }); */
}`

地图加载器类:

public void nacti()
{
    try (BufferedReader br = new BufferedReader(new FileReader("map1-1.txt")))
    {
        String radek;
        int cisloRadku = 0;

        while((radek = br.readLine()) != null)
        {
            for(int i = 0; i < radek.length(); i++)
            {
                char znak = radek.charAt(i);
                int hodnota = Integer.parseInt(String.valueOf(znak));
                pole[i][cisloRadku] = hodnota;
            }
            cisloRadku++;
        }
    }
    catch (Exception ex)
    {
        JOptionPane.showMessageDialog(null, "Error: "+ex.getMessage());
    }
}

public void vykresli(Graphics g)
{
    try {
        wall = ImageIO.read(ClassLoader.getSystemResource("Images/wall.gif"));
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "Error: "+ex.getMessage());
    }
    for(int i = 0; i < pole[0].length; i++)
    {
        for(int j = 0; j < pole.length; j++)
        {
            if(pole[j][i] == 1)
            {
                g.setColor(Color.RED);
                    //  g.fillRect(j*40, i*40, 40, 40);
                      g.drawImage(wall, j*40, i*40, null);
            }
        }
    }
}`

和英雄类:

public class Hero {
public int poziceX;
public int poziceY;
public static boolean upB = false;
public static boolean downB = false;
public static boolean rightB = false;
public static boolean leftB = false;
BufferedImage up;
BufferedImage down;
BufferedImage right;
BufferedImage left;

public Hero()
{
    try {
        up = ImageIO.read(ClassLoader.getSystemResource("Images/Hero_Up.png"));
        down = ImageIO.read(ClassLoader.getSystemResource("Images/Hero_Down.png"));
        right = ImageIO.read(ClassLoader.getSystemResource("Images/Hero_Right.png"));
        left = ImageIO.read(ClassLoader.getSystemResource("Images/Hero_Left.png"));
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "Error: "+ex.getMessage());
    }
}

public void vykreslit(Graphics g)
{
    if(upB == true)
    {
        g.drawImage(up, poziceX, poziceX, null);
    }
    else if(downB == true)
    {
        g.drawImage(down, poziceX, poziceX, null);
    }
    else if(leftB == true)
    {
        g.drawImage(left, poziceX, poziceX, null);
    }
    else if(rightB == true)
    {
        g.drawImage(right, poziceX, poziceX, null);
    }
}`

谢谢你的帮助 :)

4

1 回答 1

0

您可以计算移动的“未来位置”,并测试碰撞。如果发生碰撞,请不要移动,否则您可以移动。

看看这个逻辑是否可以帮助你:

public boolean willCollide(int row, int col, Board map)
{
    return map[row][col] == 1;
}

public void moveLeft(Hero hero, Board map)
{
  //watch for index out of bounds!
  int futureCol = hero.y - 1;
  if (! willCollide(hero.row, futureCol)
    hero.y = futureCol;
}
于 2013-08-07T17:56:13.360 回答