1

我创建了一个类似于 pacman 的游戏。我已经使用此代码显示了地图。

int leveldata1[] =
    { 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
      40, -1,  1,  1,  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 40,
      40, 40,  40,  40,  40, 40, 40, 40, 40, 40, 40, 0, 0, 0, 40, 
      40, 0,  1,  1,  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 40, 
      40, 0, 40, 40, 40, 40, 40, 40,  40, 40, 40, 40, 40, 40, 40,
      40, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 40, 
      40, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0,  40, 
      40, 40, 40, 40, 40, 40, 40, 0, 0, 40, 40, 40, 40, 40, 40,
      40, 40, 40, 40, 40, 40, 40, 0, 0, 40, 40, 40, 40, 40, 40,
      40, 1, 1, 1, 1, 1, 1, 0, 0, 40, 40, 40, 40, 40, 40, 
      40, 1, 1, 1, 1, 1, 1, 0, 0, 40, 40, 40, 40, 40, 40,
      40, 1, 1, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
      40, 0, 0, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1,  40,
      40, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 5, 40,
      40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 };

40 = wallblock
1 = dots
0 = grass
5 = finish
2 = enemy

public void paint(Graphics g)
{  
  super.paint(g);
if (ingame == false)
  {
      MainMenu mm = new MainMenu();
      Graphics2D g2d = (Graphics2D) g;
      mm.displayMainMenu(g2d);
  }
  else if(ingame== true)
  {
      if(level == 1)
      {
          Graphics2D g2d = (Graphics2D) g;
          Maze mz1 = new Maze();
          mz1.drawMaze(g2d, level, lives);
          pl1.drawCharacter(g2d);
          DrawScore(g2d);
      }
  }

在我的迷宫课上:

    life = new ImageIcon(MainMenu.class.getResource("../images/life.png")).getImage();
    wallBlock = new     ImageIcon(MainMenu.class.getResource("../images/Wall.png")).getImage();
    grass = new ImageIcon(MainMenu.class.getResource("../images/Grass.png")).getImage();
    dots = new ImageIcon(MainMenu.class.getResource("../images/Dots.png")).getImage();
    enemy = new ImageIcon(MainMenu.class.getResource("../images/Enemy.png")).getImage();
    finish = new ImageIcon(MainMenu.class.getResource("../images/Finish.png")).getImage();

    if (level == 1) 
    {
        for(int i = 0;i<leveldata1.length;i++)
        {   
            if(super.leveldata1[i] == 40)
                g2d.drawImage(wallBlock, mapx,mapy, null);
            if(super.leveldata1[i] <= 5)
                g2d.drawImage(grass, mapx,mapy, null);
            if(super.leveldata1[i] == 1)
                g2d.drawImage(dots, mapx,mapy, null);
            if(super.leveldata1[i] == 5)
            {
                g2d.drawImage(finish, mapx,mapy, null);
            }
            if(super.leveldata1[i] == 2)
            {
                g2d.drawImage(enemy, mapx,mapy, null);
                enemyx[0] = mapx;
                enemyy[0] = mapy;
            }

            if(mapx < 460)
            {
                mapx += 33;
            }
            else
            {
                mapy += 33;
                mapx = 0;
            }

        }
        while(j < lives)
        {
            g2d.drawImage(life, x,0, null);
            x += 40;
            j++;
        }
    }

我可以通过使用关键事件和以下代码成功检测字符是否与点图像相交。

if(leveldata1[playerpos-1] != 40)
{
    if(leveldata1[playerpos-1] == 1)
    {
        leveldata1[playerpos-1] = 0;
        score += 5;
        level1totalpoints -=1;
    }
    if(leveldata1[playerpos-1] == 5)
    {
        if(level1totalpoints == 0)
        {
            level = level + 1;
        }
    }
    playerpos = playerpos -1;
    pl1.moveCharacter(-33, 0);
 }

问题是我无法在我的绘制方法中更新我的地图以将点图像更改为指示角色与点图像相交的草图像。就像吃豆子一样。

我正在使用 actionPerformed

public void actionPerformed(ActionEvent e) {
    repaint(); 
}

希望在我将 1 = dot 更改为 0 = Grass 后地图会自动更新。使用这条线leveldata1[playerpos-1] = 0。有任何想法吗?

4

2 回答 2

2

我认为您应该在 repaint() 之前调用 revalidate()这可能会解决您的问题:

public void actionPerformed(ActionEvent e) {
    revalidate();
    repaint(); 
}
于 2012-11-28T06:43:44.043 回答
0

不知道您的级别参数到底是什么。但也许你对数组有引用问题:Java: How to pass byte[] by reference?

于 2012-11-28T09:21:38.410 回答