Yes, yes, I know it’s been a while since I posted. I am sorry about that. I will try to be more diligent from now on :). Today, we’ll start my series on making a platforming game. The end result will hopefully be something close to the commercial games of Minecraft or Terraria.

We’ll start out with our basic Wall class. This is just a basic class that stores x and y coordinates, but it brings forth some important concepts. Let’s look at it now:

As you can see, this is a very basic class. However, it’s a very good starting point for those new to Pygame.

On line one, we define a class. For those familiar with Python, this will be easy to understand. But for those among us who are not so snake-savvy, let’s go through this line:

It starts with the class statement. This will be the case for ANY python class that you create.

We then specify the name of the class. Again, any python class will have this feature.

Next, there is the words pygame.sprite.Sprite in parentheses. This indicates that the Wall class inherits the pygame.sprite.Sprite class. If you are not familiar with inheritance, google it. For more info on pygame.sprite.Sprite, see the documentation at http://pygame.org/docs/ref/sprite.html.

Then, we define the __init__ method. You can easily see the syntax for functions in Python, but just to be kind, here it is in easy-to-read format:

def <function name>(<parameter>, <parameter>, …)

Now, in this particular function, you will notice that the last parameter has an asterisk (*) before it. This means that any arguments after this point will be considered part of the groups list. For more on python lists, google ‘python list’. These are very versatile data structures, and a key part of the Python language.

I should also mention that __init__ is the python equivalent of New or Wall{} in other languages. It is simply the subroutine for creating a new object. To make a new wall, you would use w = Wall(<parameters>). Also, all subs in a class will have their first parameter as self. This is similar to Me in VB or this in C++. It references back to the instance of the object. DO NOT count this when calling the sub. Just skip it.

The first line of __init__ calls the Sprite initialization routine to get some basic variables into it. This can be ignored for now, but you should learn more about this when you can. For you C++oders or Javaguys out there, this is kind of like super().

Now, we load the image and rectangle using my basics library. Here are the functions used:

def rectFromImage(img, x, y):

“”” Creates a sprite rectangle from the given image at the given location. “””

rect = img.get_rect() # Create a rectangle with the size of the image.

rect = rect.move([x, y]) # Move the rectagle into position.

return rect

def load_image(name, colorkey, x, y, game):

“”” Loads a image into memory. “””

fullname = os.path.join(‘data’, game, name) # Get the full path of the image.

try:

img = image.load(fullname) # Load the image.

except error, message:

print ‘Cannot load image:’, fullname # Print an error message.

raise SystemExit, message # Exit the game.

img = img.convert() # Convert the image into a proper format.

if colorkey is not None: # If a color key has been given…

if colorkey is -1: # If we have been told to autodetect the colorkey…

colorkey = img.get_at((0,0)) # Automatically set the colorkey.

img.set_colorkey(colorkey, RLEACCEL) # Set the colorkey.

return img, rectFromImage(img, x, y)

All these do is simply load the image and get its rectangle.

The next lines in __init__ will declare its private instance variables. Unlike most languages, this is done in __init__, not in the class definition itself.

That’s all for today. If you have any questions, feel free to comment!

Rate this:

The Defenestrator has returned! I will now be posting at least once a week, probably with three columns: Python Tuesdays, VB Wednesdays, and Friday Funnies! My good friend, Nick, may be posting periodically on Ubuntu as well. As for today, I will be doing a make-up post for all of the time I’ve been gone, describing some of my current projects. Also, there is a Defenestration Coding logo in the works!