An android app to play Pac-Man

Posted on May 10, 2021

For this week, an android application to validate our skills in java android (not kotlin).

The context of the project

It was a project that I had to complete by the end of my second year of university, we had something like two months to work on it.

We had a very small introduction to the language as far as I remember, and we were expected to figure out the rest by ourselves.

That’s why the rules of the project were pretty well-defined in order to guide us.

We had the choice of the project we could do, each of a different level of dificulty, the pac-man being the hardest. The other two were :

  • a falling bricks game (not like tetris), there were blocks of four different colors and when you clicked on one, all the others of the same color that were connected disappeared too, and you had to clear the whole screen.
  • the second I don’t remember sorry ¯\_(ツ)_/¯

I had a lot of fun and ambitions on this project but ended up wanting too many functionalities, producing an unmaintainable and lacking on the design part of the project.

I remember being pretty jealous of my colleagues that managed to make pretty apps.

The instructions and my interpretations

The instructions were composed of three main parts with initial restrictions. The main restriction was to not use webviews, allowing the use of webviews would’ve meant that we could do our project in javascript, host it and simply show it in our app, clearly not the objective.

The project must’ve been composed of :

  • a main activity for the game
  • a class for the pac-man
  • a generic class for the ghosts
  • many classes for the ghosts with different behaviors

First step : game grid

For the first big step we had to implement de game grid, the levels had to be stored in text files with a structure like so :

6,19
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
5,6,6,6,6,6,6,6,6,6,6,4,6,6,6,6,6,6,5
5,6,5,5,5,5,6,5,5,6,5,5,5,5,6,6,5,6,5
5,2,6,5,5,6,6,5,5,6,5,5,5,5,6,5,5,6,5
5,1,6,6,6,6,6,6,6,6,6,6,6,6,6,6,3,6,5
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5

Representing the following in-game level :

level-one

(That’s the final in-game rendering, not very pretty isn’t it)

With our gridView, each cell containing an ImageView, an arraylist to contain the sprites (game assets), and an adapter to make the link between each element.

The first step was planned to help us to understand how to structure our levels and create the game ‘area’.

Second step : moving the pac-man

For this step we implement most of the game mechanics, four buttons to move the pac-man, the pac-man eats the pac-gums while moving and prevent the pac-man to go through walls.

There was a small subtlety here, if the player press the button to turn before being able to, we had to register the input and make the player turn when able too.

Nothing too hard here.

Third step : the scary ghosts

At first, we had to implement the ghost with random movements. All ghosts had to be in their own thread using the Handler and runnable classes.

Then we had to modify the behaviors of the ghosts to be a little more challenging.

I ended up implementing three different IAs :

  • The blue ghost wander aimlessly, not very different from the default one
  • The yellow one kept going forward until hitting a wall, turning right and continuing so
  • The red one was a beast, he follows pac-man through an optimal route determined with an A* algorithm by the famous Djikstra

The first two one are pretty dumb, but I was proud of the red one, it was the first time I implemented A* and always wanted to do it.

Fourth step : the interactions between the pac and the ghosts

This part was simply to add the win/lose mechanics when the pac is over a ghost making sure the game doesn’t end when two ghosts are over each other.

I actually remember strugling on this part to make sure one of the ghosts doesn’t get deleted when two ghosts are on the same square. I’m pretty sure that if three ghosts are overlapping each other in the current app one will disappear, I was so done with this app at this point, completely burnt out after the A* implementation and my colleages doing dumb but shiny apps.

Fifth step : the final game mechanics

Implementing score, speed increasing, many levels, a screen to show the best scores and anything to make the app playable…

Pretty easy step.

Sixth step : Bonus

  • Phone orientation : not implemented, the phone orientation is stuck in portrait mode because I never managed to make the grid view responsive, nor anything else, now with my web developer knowledge everything makes so much more sense.
  • language : only english is available
  • menu : low effort design, couldn’t provide much more at the time
  • music : actually found a decent soundtrack that I had obtained with a humble bundle of game assets, there is a different music when winning, losing and in the menu.

And voilà ! Some screenshots of the app :

The main menu

main-menu

The highscore screen

highscore-screen

In-game screen

in-game-screen

Win-lose screen

win-lose-screen

What I did to make it work

I installed Android studio and simply opened the project and that’s it !

The full project is accessible here.