Have some problems making pacman?

Edit: Totally forgot to mention what I code in Java

It is very difficult for me to make some kind of detection system or somehow make my pacman sprite / character smoothly move around my board in the game. I did not make the board its image.

I first tried color recognition, which worked best, but was not smooth and pretty choppy.

Then I tried to manually enter the input coordinates of the location, which cannot be entered. It also did not work out so well.

I am currently trying to use a program to detect color and check a separate invisible board to see if I am still on the way. It failed a lot more. It seems like he would be the smartest, but the corners are just crowded and difficult to fix by adjusting the images.

I am wondering what method you would suggest for this task.

+7
source share
1 answer

A typical approach to storing "old school" game boards is to use a multidimensional char or int array . Using Matt's great little graph , you can see that there are 21 by 21 squares on the board:

 int board[21][21] = {{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, {1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1}, /* ... and so on, for all 21 lines .. */ }}; 

It doesn't really matter which numbers you choose for walls and paths. The "pathway" positions initially contain the code for "contains a point". Since paccy consumes dots, save the new value on the board in position to indicate that the dot was consumed, but it is still the square of the path. Matt recommended -1 for walls, 0 for a point without a point, and 1 for a point - which is quite a plan, as it allows your wall collision routines to simply search

 if (board[pac.x][pac.y] > 0) { /* still in bounds */ } else { /* collided against a wall */ } 

The downside is -1 , which looks more uncomfortable in the array initializer.

If this were done in C, it would be quite simple to โ€œimproveโ€ it with char board[21][21] instead of int board[21][21] and save the playing field as a string C:

 char board[21][21] = " XXXXXXXXXXXXXXXXXXX " " XXX " " X XX XXX X XXX XX X " " XX " " X XX X XXXXX X XX X " " XXXXX " " XXXX XXX X XXX XXXX " " XXXX " "XXXXX X XXXXX X XXXXX" " XX " "XXXXX X XXXXX X XXXXX" " XXXX " " XXXX X XXXXX X XXXX " " XXX " " X XX XXX X XXX XX X " " XXXX " " XX XX XXXXX XX XX " " XXXXX " " X XXXXXX X XXXXXX X " " XX " " XXXXXXXXXXXXXXXXXXX"; 

This is a lot easier to read in the source code, takes up less memory, and your wall collision routines might look like this:

 if (board[pac.x][pac.y] == 'X') { /* collided with a wall */ } else { /* still in bounds */ } 

(Although the final NUL that the compiler will insert at the end of the line means that the lower right square can never be used for a path or wall - it can take a bit of work, but it's not so pretty.)

I donโ€™t remember enough Java to do this job in Java, but I'm sure you can figure out something if it looks convincing enough.

+5
source

All Articles