Saturday 16 January 2016

Playing with... MonkeyRunner

A few days ago, Stefano was playing with our Samsung Galaxy Tab 2.

He usually enjoys playing a plethora of MineCraft clones on it, and he was playing with a game with the well-known blocky graphic style (I have already purchased the XBox360 version, and I don't want to spend other money for the same game on a different platform... this actually already happened some months ago, but this is another story  ).
I enjoy watching him play video games because, in general, children have different and way-more-creative ways to play than adults.
I think this is one of the reasons for Minecraft's global success.
Apart from the graphic style, the game was not a sandbox this time.

PickCrafter is a very simple arcade where your only aim is to collect as many blocks and "picks" as possible.

All you have to do is shake your device, mimicking mining with a pickaxe (the only tool of the game) or tapping as fast as you can on the screen. Moving your device allows you to collect "picks" faster than tapping.
You can buy enhancements by spending your "picks" in the game shop. Enhancements allow you to collect picks faster.

While helping Stefano tap furiously on the poor tablet screen, I thought the game was an excellent target for the Android SDK MonkeyRunner utility  .

MonkeyRunner is a tool that helps the developer to simulate some user events by writing a Python program using the MonkeyRunner API.

For me, it was also an occasion to work with Python.


Ok, let's start then from the Python program.
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import time
# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()

points = [{"x":300, "y":900},
          {"x":500, "y":300},
          {"x":400, "y":700},
          {"x":600, "y":600}]

while True:
  for p in points:
    device.touch(p["x"], p["y"], MonkeyDevice.DOWN_AND_UP)

    time.sleep(.05)


This program defines a list of points (technically a list of dictionaries) and then sends them indefinitely to the device.

After a few attempts, I found the screen coordinates to get the best from the game.
I also found that a small sleep is necessary after each touch event; otherwise, the game will hang.

To find the exact screen coordinates, I enabled the option "Show touches" and "Show pointer location" in the developer options menu of the device settings.

Remember to disable both settings afterwards because they will slightly slow the device considering that we send a touch event every 50 ms.



It is time to run MonkeyRunner: save the code in a file (e.g. send_touch_events.py) and open the Console link you can find in the root folder of the Android SDK installation.
As MonketRunner sends the touch events to the foreground activity, you should open the game on your device before starting the script. 

On the Command Prompt type
monkeyrunner SendTouchEvents.py

and hit return.

If the device is connected correctly and you have authorized the USB debug connection in the device settings, the game now should receive the touch events.
You will then quickly collect a large amount of "picks" and blocks without risking damaging your device.


 
PickCrafter during a MonkeyRunner session

Issues

- During the first few seconds of the "fire time", you will still need to tap on the screen. I don't know exactly why, and I don't want to spend more time finding it.- The script cannot catch the "creep" that randomly appears on the screen. Without knowing the game code, we will need a complete AI and a more sophisticated system.

 
Enjoy! :)


No comments:

Post a Comment