Building a Whack-A-Mole Game With rn-sprite-sheet

A walk through building this classic game with a new twist

Tamas Szikszai
Sep 22 · 9 min read

Today I’m going to recreate another classic game in React Native: Whack-A-Mole. The mechanical version of this game was an arcade fan-favourite in the late ‘70s and early ’80s. Since then numerous clones on various devices have been made.

TL;DR #1: Watch the video:

TL;DR #2: Grab the source code from GitHub

The gameplay is super simple. On the screen we display 12 slots. Moles will pop out of these slots and then disappear again at a rapid pace — the players’ goal is to hit as many of them on the head as possible. As a twist to the original, I want to introduce three different kinds of moles:

  • “Neutral”: this mole deals no damage if not “whacked”, but when hit the player will still receive points.
  • “Feisty”: This mole attacks the player if not whacked in time, dealing damage.
  • “Healer”: If whacked it will restore the player’s health a little bit.

The original game is more aimed at children — I think the introduction of different moles will spice things up just enough to broaden its appeal!

The Setup

We’ll require some animations. In my opinion, the best way to animate things in React Native is by using the rn-sprite-sheet library. You can install it by running the following:

This library is amazing at defining and running small animations from a single image file containing equal-sized frames.

For example, if you have six image frames for an animation, each of them 40x50 pixels, you can create a single 240x50 image, paste your frames and you’re ready to go.

However, in real-life your frames won’t be equal-sized. I spent a good couple of hours creating my sprite sheet by hand and got annoyed — to the point that I just had to create a python script to do the work for me. You can grab the script from my GitHub.

Anyway, I ended up with these image assets for our game:

As per usual, let’s pop them into an Image.js file for convenience:

Setting up the base design is long and boring. So instead of writing hundreds of lines of explanation, just click here and see our starting position. There’s nothing special about it. The only thing I would highlight is two lines from Constants.js

Our background.png image is a static 650x1024px png. We are stretching this image to cover the full screen of the user’s device. The chances are the device’s screen size won’t be 650x1024, so the image will be slightly distorted, which is not a big deal. The problem, however, is that later we’ll need to position elements on this background at exact locations. This is why we have to set up the XR and YR constants. They represent the X and Y ratio between the background image size and the user’s screen size. For example, if the screen’s width is 1300, the XR value will be 2. If we want to position something 20px from the left on the original background we can just say Constants.XR * 20

This is our clean slate


Time to add our moles to the screen. Our base Mole.js will look something like this:

The interesting part is the <SpriteSheet> component. Since our sprites.png has six images in a row, and we have eight rows, we pass in columns={6} and rows={8}

Then we need to define the animations. Each animation has a name (for example “appear”) and a list of frame indexes. Each frame in our sprite is assigned an index, starting from zero at the top left and increasing by one as it goes left to right and top to bottom.

Adding the Moles to the Screen

In our App.js we need to import our Mole component:

We probably want to keep a reference to each mole so let's modify our constructor as follows:

Finally, create a 3x4 grid of moles in our playArea:

Excellent, we now have 12 holes on our screen that do nothing.

Pop Those Moles

We would like to pop moles at a random position periodically. In order to achieve this, we need to expose a pop() method in our Mole.js

When we call this method plays the “appear” animation, waits a second then plays the “hide” animation. Notice the this.props.onFinishPopping call? This will get called when the whole sequence is finished.

In our App.js we should modify the Mole definition:

Then we add the logic that sets up two loops: one for popping moles and one more for decreasing the timer:

OK, there’s a lot to unpack here:

  • On componentDidMount we call setupTicks (after setting some base state).
  • setupTick will set up the two intervals described above.
  • We keep track of how many moles are “popping” at any given moment in the molesPopping variable.
  • When a mole finishes popping onFinishPopping is called and we reduce this variable.
  • popRandomMole will select a random slot which is not currently popping, but only if we have less than three active moles at a time (or things would get too chaotic).

It’s looking good, but not exactly exciting…

Whack Those Moles

As mentioned in the first paragraph of this post, I’d like to add a twist to the original game and introduce different kinds of moles.

By default each mole will be “Neutral” but they will have a 40% chance to become “Feisty” and 5% chance to became “Healers”.

Based on their types we will play different animation sequences. We should also handle the whacking. A TouchableWithoutFeedback is already defined and pointed to our Mole’s whack method that we need to fill. Our Mole.js will look something like this:

As you may notice we used three new callbacks:

  • this.props.onDamage
  • this.props.onHeal
  • this.props.onScore

We should define these in App.js. First, let’s change the mole’s definition:

Next, we define these three methods:

None of these methods are rocket science:

  • onHeal adds ten health points to the state (if it’s not already full).
  • onScore adds one point to the state.
  • onDamage takes away 10 health points. If health falls below 0 it clears the intervals and displays the game over screen.

Finally, we have to hook up some methods for our interface. We need to define:

  • reset(), which is called when the user hits the reset button on any of the popup screens
  • nextLevel(), which is called when the user hits the “play” button on the Cleared screen
  • pause(), when the user hits the pause button in the top right corner
  • resume(), when the user hits the play button on the Pause screen.

They’re pretty straight-forward and should look something like this:

And there you have it! We’ve successfully built a working Whack-A-Mole game in React Native, which is actually surprisingly fun to play!

I hope you enjoyed this tutorial.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade