-1

I am learning game programming. I am trying to make a game similar to Pacman. I have made a grid with x[] and y[] (no using tiled). when sprite is in a specific grid I want to destroy its food item which is an image within the grid and add 1 to score. I tried to use image.destroy(); method but it didn't worked.

I tried, instead of destroying the image to change its location.

image.drawImage(image,x+25,y+25); it only created a new object for as long as I was inside the specific grid.
The Game loop would not let me destroy the image or move it outside the screen.

I also tried

if(sprite is in grid){score=score+1;}

So once the sprite is in grid the score keeps on incrementing. I just want increment of "1" once my sprite is in specific grid.

Any help would be highly appreciated. Thanks.

Arslan
  • 1
  • 1

1 Answers1

0

Normally your grid[y][x] would contain values indicating what's there? This can be an enum with values EMPTY, WALL or FRUIT.

So when the player enters a cell with grid[player.getY()][player.getX()] == FRUIT, clear the cell to EMPTY, add 1 to the score, and invalidate that rectangle of the grid on screen.

The render() method will then check grid[], find it is empty.. and not draw any fruit.

You should have a single fruit image or ImageBuffer -- this doesn't have any specific position, but acts as an off-screen source for the render() function to draw it whereever it is needed. This doesn't represent any single fruit, so it doesn't get destroyed -- nor does it have a position.

Unlike sprites (for monsters/ player etc), fruit are not expected to be individually animated/ positioned around the screen.

Thomas W
  • 13,940
  • 4
  • 58
  • 76