1

I created a game similar to pacman. I already displayed the map by using this code.

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);
      }
  }

And in my Maze class:

    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++;
        }
    }

I can successfully detect whether the character intersects with a dot image by using key events and the following code.

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);
 }

The problem is that I cannot get to update my map in my paint method to change the dot image to a grass image indicating that the character intersects the dot image. Just like pacman.

I'm using actionPerformed

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

Hoping that the map will automatically update after I change 1 = dot to 0 = grass. Using this line leveldata1[playerpos-1] = 0. Any ideas?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Luke Villanueva
  • 2,030
  • 8
  • 44
  • 94
  • 3
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) *"Any ideas?"* Ask a (more specific) question. – Andrew Thompson Nov 28 '12 at 03:08
  • 3
    Some related [sscce](http://sscce.org/)s may be found in the answers to this [question](http://stackoverflow.com/q/11553461/230513). – trashgod Nov 28 '12 at 03:15
  • Luke V. (@ljpv14), post your running [SSCCE](http://sscce.org/) for us to better assist you in this matter. Regarding the Java game development, you may want to search for references like [this](http://www.amazon.com/Beginning-Java-Programming-Second-Edition/dp/1598634763) to assist you with the coding and stuff. Hope this helps. One more thing, its better to use switch-case on the drawing the corresponding image rather than if-else. – David B Dec 05 '12 at 07:56

2 Answers2

2

I think you should call revalidate() before repaint() This might resolve your problem:

public void actionPerformed(ActionEvent e) {
    revalidate();
    repaint(); 
}
Community
  • 1
  • 1
mostafa.S
  • 1,452
  • 4
  • 16
  • 27
0

Do not know what your level parameter exactly is. But maybe you have a reference problem with the array: Java: How to pass byte[] by reference?

Community
  • 1
  • 1
Hendrik Ebbers
  • 2,570
  • 19
  • 34