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?