1

パックマンに似たゲームを作りました。このコードを使用して、既にマップを表示しています。

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 = ドットを 0 = 草に変更した後、マップが自動的に更新されることを願っています。この行を使用します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 に答える