Friday, December 21, 2012

Swarm, Sprites, Opacity, and Zombies

The winds are whipping around outside my window.  Although we planned to go surfing in Santa Cruz today, the rain and wind are making our surf outing look grim.

I decided to help my son work on his new game, Swarm.  Wow, surfing or Python programming... Well, he's a typical teenage boy, he'd rather go surfing.  But, we can't control the weather, so he's stuck with Pygame.

The concept of Swarm is to have a player face increasingly larger waves of swarming zombies.  It's a classic game with the ability to move and shoot in four directions.  The game is designed to work on his LG Optimus S Android phone with a screen size of 480x320.

The left thumb controls movement.  The right thumb controls firing.  There are unlimited bullets in the current version.  He takes a great deal of satisfaction is blasting through 20 zombies.

This is the first time we've used transparency.  For the controls, we learned that the transparency can be handled with Gimp.  In the layers window, there is a slider bar for Opacity.  For other surface objects, we're using the set_alpha method of the Surface class.

The grass and trees are made from 32x32 pixel tiles.  They're not set in a map right now.  My son is planning the game to involve going through a series of rooms.  He doesn't need to put the tiles into a map right now.

The swarm algorithm for the zombies to chase the player is pretty basic.  We're simply tracking the position of the player and move the zombies 1 pixel toward the player in both the x and y axis for every frame.  The frame rate is currently set to 30 frames per second.

 The trickier bit was how to handle the zombie crowding problem. We've got it set now to simply check for zombies colliding with each other using pygame.sprite.spritecollideany.


  1. Hello, I really pleasing your tutorials, I want to ask you about the "transparency" Because I can not get to my work properly when I run my games on android.
    Here I leave the link of my game on the PC if it works the way I want android but transparency will be lost.

  2. I'm reading this on my phone now and can't see the code. I'll try download it later. I've had problems with transparency in the past as well. If you're using an image, grab the color in the upper left corner with get_at ((0, 0)) and then use set_colorkey () to set the background color as transparent. I wrote another blog post a month ago on sprite optimization that shows transparency with the alpha channel. That example shows the controls like colored glass.

  3. Jose, I looked at your code. I couldn't see where you set the alpha channel on the surface with pygame.Surface.set_alpha() For example, Imagenes.set_alpha(30)

    Look at the pygame.Surface documentation for additional methods.

    BTW, your game looks pretty cool. Keep at it and post a link to a video of your game working when you finish it.

  4. Thanks for answering.
    After much trial and error, since I found the difference why my program does not funcionava well in android.
    Declaring the surface to function as screen:

    PANTALLA = pygame.display.set_mode((480,272),1,16)
    Thus transparency in android not working and some more errors appear.

    PANTALLA = pygame.display.set_mode((480,272))
    Undeclared flags or depth the program works fine in android