·9 min read

Same game difficulty on different screen sizes

In Echo Bat a tablet saw further ahead than a phone, and my test bot scored half again as much. A fixed design frame gives every screen the same view ahead.

gamedevdifficultyandroidtesting

Contents

Echo Bat is a one-tap cave flyer. The bat flies right through the dark, a sonar ping lights the rock for a moment, and you steer through whatever gap you saw. How far ahead you can see is most of the difficulty. In my simulated runs every single death had the same cause: the next gap became visible too late to reach.

So the screen's shape was a difficulty setting I had never chosen. I found out from a simulated player: a bot that flies only on what is lit on screen and reacts after a delay, run over 40 seeded caves. On a screen 295 cave units wide its median score was 95. On a screen 400 units wide, same caves, same bot, the median was 142.

A 10:16 tablet held upright was 400 units wide under the rule the game used then, a little more once the ad banner took its strip. A tablet player was getting a different, easier game than a phone player.

This post is how I made the difficulty the same on different screen sizes: a fixed design frame that every device shows whole, with the extra space put behind the player, where it cannot help.

The old rule: fit the height, keep whatever width you get

The game measures everything in cave units. The screen always showed 640 of them top to bottom, and the width was whatever the screen's shape left over:

// Before (simplified): the height is fixed, the width follows the screen.
const widthInUnits = box.width * (640 / box.height);

The bat sat 18% of the way across the screen. Everything to its right was the view ahead. Here is what that rule gave just before I changed it, worked out from the code rather than measured (game area only, no ad banner):

Screen Width in units Ahead of the bat
9:20 tall phone 288 about 236
9:16 phone 360 about 295
10:16 tablet, upright 400 about 328

The tall phone got the least warning. The tablet got about 40% more. The simulation above says that gap is not cosmetic.

Four ways to fit a game to any screen

The first three options I had on the table were a fixed play window with decorative panels down the sides, keeping the old rule, and keeping it but capping the width. I turned all three down. Looking at them again, two don't fix anything: the old rule is the problem, and a cap only moves the point where the tablet starts to pull ahead. The panels do fix it, by putting fake scenery around a game that is supposed to be a cave.

The next round started from a different question: not how to fill extra space, but what stays fixed. Every stage would be designed inside one frame, 360 × 640 cave units, the shape of a 9:16 phone. Then:

Option Fixed Varies Look-ahead
A. Fit the frame, extra behind the bat the whole frame is always visible extra space above and below, or behind identical
B. Fit the width 360 units across visible height; a tablet sees only 480 identical
C. Fit the frame, extra on all sides the whole frame extra split ahead and behind about +60 on a tablet
D. Constant visible area width × height both, a little varies a little

B keeps the look-ahead fair but crops the tablet top and bottom, so the camera has to chase the bat more and everything is drawn bigger. C looks the most natural and gives the tablet back part of its advantage. D makes nothing exact.

I picked A.

Option A: the frame's right edge is the screen's right edge

The frame is scaled as large as it can be while staying whole. Whatever the screen has to spare shows more cave, never bars:

  • A screen taller than 9:16 fits the width. The spare height shows more rock above and below.
  • A screen wider than 9:16 fits the height. The spare width goes on the left, behind the bat, where it shows cave you have already flown through.

The bat cannot fly backwards. Cave behind it is free to show because nobody can use it. The right edge of the frame is pinned to the right edge of the screen, so the distance from the bat to the edge is the same number on every device.

In code it is a few lines. This is a simplified version, not the game's file:

// Simplified: one design frame, shown whole on every screen.
const FRAME = { width: 360, height: 640 }; // cave units, a 9:16 phone
const BAT_SHARE = 0.18;                    // the bat sits 18% into the frame

function fitScreen(pixelWidth, pixelHeight) {
  const scale = Math.min(pixelWidth / FRAME.width, pixelHeight / FRAME.height);
  return { width: pixelWidth / scale, height: pixelHeight / scale };
}

// The frame is pinned to the right, so this leaves the same distance
// ahead on every screen: FRAME.width * (1 - BAT_SHARE), about 295 units.
const batX = (screen) => screen.width - FRAME.width * (1 - BAT_SHARE);

Two smaller things follow. Stage shapes are now written as shares of the frame, so a tunnel half the screen tall is 320 units everywhere. And the camera keeps the frame where it always put it and centres the rest of the screen around it, so the spare height splits evenly above and below.

The bot had to learn the new rule too. It now treats the spare height as visible, because a player on a tall phone sees it. Anything the screen shows, the bot may use. That is the same rule it already followed for rock beyond the right edge.

The ad banner is part of the screen size

A banner ad along the bottom changes the shape of the game area by a few percent, and it can appear or disappear in the middle of a run. If the frame were fitted to the whole display, the banner would sit on top of the bottom of the cave.

The game's layout already stops above the banner, so the frame is fitted to what is left. When the banner arrives or goes, only the spare space changes size. The frame stays where it was, because the top of the view moves by half the change in height.

Testing different screen sizes without a shelf of devices

The unit tests now run five screen shapes, a tablet on its side included, from pixel sizes in, cave units out. Each one must show the whole frame and the same distance ahead. Another test makes the banner appear and disappear and checks the frame does not move. A sideways screen is worth covering even in a portrait game: Android 16 lets a large-screen device ignore an app's orientation lock.

The first run had one failure, in the tablet-on-its-side case, and it was mine:

AssertionError: expected 295.33333333333337 to be 295 // Object.is equality

I had kept a Math.round on the bat's position from the old code. Rounded, the sideways tablet saw a third of a unit further ahead than the phone, which is exactly the kind of difference the test was there to forbid. Dropping the rounding and comparing with toBeCloseTo fixed it.

A few older tests also broke for a better reason. They used screens 290 to 300 units wide, narrower than the frame, which is no longer a screen that can exist. They now pass a real phone size in pixels through fitScreen instead of a width in units. All 459 tests passed after that.

Then the emulator. The Pixel Tablet emulator would not install the build:

android.os.ParcelableException: java.io.IOException: Requested internal only, but not enough space

Rather than delete things off it, I shut it down and reshaped the phone emulator into a tablet instead:

adb shell wm size 1600x2560
adb shell wm density 320

Undo it afterwards with adb shell wm size reset and adb shell wm density reset. The same trick is in my post on making the Android emulator faster, for a different reason.

Screenshots from a run on each shape:

Tall phone (1280×2856) Tablet shape (1600×2560)
The frame fills the width the height
Spare space shows more rock above and below cave already flown, behind the bat
Tunnel at half the frame 320 units 320 units
Distance ahead of the bat about 295 units about 300 units

The arithmetic says 295.2 for both. Both screenshot readings are within five units of it.

What this does not settle yet

I have not re-run the bot across screen sizes since the change, so I cannot tell you the medians now match. At the time the cave was a plain straight tunnel with no rocks in it, which gives the bot nothing to die on. The claim I can make is narrower: every screen now shows the same distance ahead, and the earlier simulation says that distance is what moved the score.

Two things are open. The spare height on a tall phone shows more of the cave above and below, and in a cave that climbs and dives that might be worth something. I have not measured it. And phones changed too. A phone that was 300 units wide used to see about 246 units ahead; it now sees about 295, and draws everything about a sixth smaller. By the simulation that is the easier direction, so the difficulty curve needs tuning again, once, on one frame, instead of once per screen shape.

That last part is the real gain. Before this, "how hard is the game" had a different answer on every screen. Now there is one answer, and I can tune it.

More posts

New writing when there is something worth saying. By email or by RSS, whichever you prefer.