Tuesday, November 26, 2013

2D Tiles Games - More Algorithms to Slice Tilesets

Prior to heading out to go fishing, I came across this nice post on slicing a tileset into rows and columns.  The example uses Surface.subsurface, which I haven't been using effectively.

The author wrote a more elaborate tutorial here.

The author of bitcraft-PyTMX used the code below to handle tilesets.

I plan to review the code snippets above in more detail after the fishing trip.

Sunday, November 24, 2013

Pygame on Android App Crashes on Start: FIXED

A good tileset in the wrong place at the wrong time
After catching Jack smelt in Capitola and seeing an amazing display of seals, dolphins, and school of tens of thousands of fish, my son worked on his 2D tile map game, Maze.  The Android app consistently crashed on start.  It would display the initial splash screen and then die, leaving no error message.

This has happened in the past with other games.  We usually solve the problem with adb, the Android debugger.  Today, the fix was easy.  However, since I know that it can be daunting to wade through the adb logcat, I'll provide a few tips specific to Python / Pygame Subset for Android debugging.

My son's game uses Python and pgs4a.  He also uses Tiled to export JSON map files. 

First, find adb on your system.  It's not something that python developers normally use from the command line.  adb is in the platform-tools sub-directory of the android-sdk main SDK directory.

 From the Linux command line, you can either just run adb logcat or you can run it through grep and look for references to python.


 A larger snippet of the log file is shown below.

 In this case, the error was an incorrect reference to the tileset graphic.

The error is caused by a relative reference to a non-existent file.  Although the program works on the desktop, it doesn't work on the phone.  The fix is to place the tileset files in the same directory or sub-directory of your main.py program and create a new map and tileset with tiled.  If you've already created an extensive map, you can also edit the json file with a text editor to have it reference the correct location.

In this example, the file is called grass_water_terrain.png.

In a previous game, my son had a similar problem with the app crashing when it tried to load a font file that was in the wrong location.  Fonts and graphics are the two most common problems.  If you have all your fonts and graphic files in the same directory and your program still doesn't work on Android, but works on your desktop, you may be trying to import a library another directory.  Everything needs to be in the directory where main.py is or in a sub-directory.

If the application works on your desktop and it builds and then loads onto your phone fine, but crashes on start, think through what files the application is trying to load on the Android phone.  Then, work backwards and think through the differences between the phone directory structure and your desktop.  Also, think about the obvious things.  Check that you imported the android library and you've initialized it android.init().

If the problem is not immediately obvious, fire up adb and look for references to python.


  • import android library
  • initialize android library
  • main game file is called, main.py
  • graphic files are in the main or sub-directory
  • font files are in the main or sub-directory
  • custom libraries that you wrote are in the main or sub-directory

Update: March 3, 2014
One of the viewers of my pychildren YouTube channel had a problem with sound.  The important section is to map pygame.mixer and android.mixer to mixer.

try:    import pygame.mixer as mixer except ImportError:    import android.mixer as mixer

The other problem with sound is that there may be problems with different types of sound formats.  I suggest you use .wav files.  Since sound is handled differently on Android and the desktop, a few problems occur.

Tuesday, November 5, 2013

Pygame Subset for Android Sprite Optimization with RenderUpdates

In order to get 2D map games written in python working faster on low-end Android phones, I experimented with optimizations. I purposely selected a complex background image in order to stress the application.

The first set of huge gains came from using the Surface.convert_alpha() method to optimize the images. This was easy and resulted in significant performance gains.

The trickier part was to update only the sections of the screen that had changed since the last cycle.

I decided to use sprites for this since many of the optimization examples I've seen use sprites.  Instead of using pygame.sprite.Group, I used pygame.sprite.RenderUpdates.  This is basically the same as the sprite group, but it as the enhanced ability to track the changed areas of the screen.

The images in the group are drawn to the screen with the standard sprite.Group.draw(SCREEN) method.  The cool little addition is the RenderUpdates.clear(SCREEN, background) method which clears the sprite by blitting a piece of the background over it.  Nice.

The final section is to update the display with a list of the rectangles, pygame.display.update(rectlist).

That's the main things to change.  I experimented with not ticking the pygame.time.Clock under android.   I'm not sure if it makes a difference, but the application appeared to me to run slower on android when I used the clock tick.

This is the first time that I've used this bit of code to pause the android device when the user hits things like the home button on the phone.

I was pretty happy that my alpha transparency worked with the sprite group.

I also experimented with different ways to optimize the events. However, I couldn't find anything useful.

Here's the full code.

Monday, November 4, 2013

Paint Program with Pygame

My fourteen year old son did a nice job with his paint program. This took two or three lessons. He's making rapid progress now. He's quite busy with school and sports and doesn't have enough time to work on his programming. Despite the limited time he spends on programming, his cognitive skills are improving rapidly and he's able to build usable programs in a short amount of time. He was also able to incorporate feedback from his 8 year old sister. She designed the color palette and put a feature request for the stamps. I think she might have preferred flowers to the stamps.
This program uses Pygame and pgs4a.
Stay tuned for his next application, a JSON 2D map loader and maze game.