Monthly Archives: June 2013

Blit/Draw order in 2d games



  • If you have done graphical programming, you know how important it is to blit images in an order that makes sense.

    Each frame, your game has to blit, or draw, images that will be displayed on the screen. With SDL and with pygame (which uses SDL), each new image is drawn on top of the previous ones that were drawn that frame. Usually you want to have the hero be drawn last, to make sure that the player will be able to see their character. Once in a while there is an exception to this idea, like if you ducked for a few seconds on a white rectangle in Mario 3, you would be drawn before the foreground was drawn.

    In a third person view style game, like Chrono Trigger, the hero isn’t always drawn last. If you stand underneath a tree, the leaves of the tree are often drawn after the hero. Other atmospheric effects are also drawn after the hero, like smoke or clouds. Perhaps the most interesting thing about the draw order in Chrono Trigger though is that the sprites are not always drawn in the same order.

    If you have two sprites standing in roughly the same location but sprite A’s feet are a few pixels higher than sprite B, the sprite whose feet are closest to the top of the screen ought to be drawn before the sprite whose feet are on the bottom of the screen. Got it?

    Luckily, in pygame this is very easy to implement. Put all of the sprites in a list that is sorted by their y positions from lowest to highest. Then use a for loop to draw the sprites in order.

    Thank you for reading. To contact me, email plays_strats@yahoo.com

    Hacking Binary Files



  • Have you ever opened up a binary data file for a game, an image, or an executable program with a text editor?

    Go ahead and try it, you’ll find a whole ton of weird looking characters and numbers. The reason that they seem so random is that the file basically contains a bunch of numbers, and notepad is reading each one of these numbers and trying to turn it into the character of that ASCII value. For instance, if it reads the hex value 4D from the file, it tries to turn it into an uppercase ‘M’.

    If you open one of these files and try to edit it in something like Microsoft Notepad, You could very easily corrupt the file, because anything that you type is converted from ASCII into hex numbers. So, if you open up a game save file, figure out where your strength score is stored, and change it to 99, you are actually inserting the hex values 39 39.

    So, one night I was poking around in some data files for the game Baldur’s Gate, and I found that the text editor vim had a nifty command that converts a binary file into editable, more readable text. In normal mode, type in :%!xxd to change the file into a hex dump.

    Wow! I did it and stuff happened… now what the hex does :%!xxd mean???

    In vim, the % symbol means that we’re doing something with all of the lines in the file. Another example of this would be global replace: :%s/oldstring/newstring/g

    The “!” (pronounced “bang” by linux geeks) tells vim to run a command in your shell. For instance, if you are in the middle of editing a file and you want to check what your computer’s hostname is, go into normal mode and type :!hostname .

    xxd is a program that does a hex dump of binary data.

    Once you have done your :%!xxd command, you’ll see several columns of data. Think of the leftmost column as xxd’s fancy line numbering. The middle columns are where all the good stuff is, it’s a bunch of hex data that you can now edit. The rightmost column contains an attempt to put the hex data from the middle columns into ASCII values. While the column on the right is handy for a reference, editing it will not edit any hex data.

    IMPORTANT! Last thing… after you are done tinkering with your data in hex dump mode, do the following command:
    :%!xxd -r
    This reverts your edited hex dump back into binary data.

    Two more warnings:
    1. Back up your data files. Especially before you edit their hex code.
    2. While it is very fun to hack these files and to play for a few minutes with godlike powers, it can totally ruin a game if you take the challenge out of it.

    Thank you for reading. To contact me, email plays_strats@yahoo.com

    Python Tricks for Linux Users



  • If you want to be able to run your python code without having to type python myfilename.py, there’s a couple of simple steps to take to remedy your situation.

    First, let’s use your terminal to figure out where your python executable is:
    seth@ventolin:~$ which python
    /usr/bin/python

    In my case, the path is /usr/bin/python. Now, I need to make a new line at the top of the python code. The line is called a “shebang”. It starts with a pound symbol and an exclamation mark, also known as a bang. It’s a special kind of comment that needs to be placed at line 1 of your source. It lets you specify which executable to use to interpret the file. Since my path is/usr/bin/python, my shebang line looks like this:
    #!/usr/bin/python

    Next, lets check the permissions on your file. We need to make sure that you can execute it. I always recommend using, understanding and loving your terminal. cd to the directory that has your code. Use ls -l to list the files and their permissions:
    seth@ventolin:~/pyexample$ ls -l
    total 4
    drwxr-xr-x 1 seth seth 4096 Mar 10 05:34 data
    -rw-rw-r-- 1 seth seth 37 Mar 10 05:34 picard.py
    -rwxrw-r-- 1 seth seth 42 Mar 10 05:34 worf.py

    The rw, and x characters indicate if a file is readable, writable, and/or executable. The first character in that column is a d if the given path is a directory or a - if it’s a file. In our situation, data is a directory, picard.py and worf.py are files. Since the fourth character for data and worf is an x, the user who owns the file can execute the file. picard.py however, is not executable. If you need to make your file executable, use chmod 700:
    seth@ventolin:~/pyexample$ chmod 700 picard.py
    Now, check the permissions again:
    seth@ventolin:~/pyexample$ ls -l
    total 12
    drwxr-xr-x 2 seth seth 4096 Mar 10 05:49 data
    -rwx------ 1 seth seth 37 Mar 10 05:48 picard.py
    -rwxrw-r-- 1 seth seth 42 Mar 10 05:49 worf.py

    Now the file is executable on its own. You might be able to just type the filename at this point to run the file. If that doesn’t work, make sure that you are in the directory with your code, and that your present working directory is in your $PATH. Your $PATH is an environment variable that tells your system where to look for any commands that you type. First use pwd:
    seth@ventolin:~/pyexample$ pwd
    /home/seth/pyexample
    Now, check your path:
    seth@ventolin:~/pyexample$ echo $PATH
    /home/seth/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/seth/bin

    You can see that in my example, /home/seth/pyexample is not in my $PATH. To append that directory to my path, I’d do this:
    seth@ventolin:~/pyexample$ export PATH=$PATH:/home/seth/pyexample

    I can run my code from the command line now!
    seth@ventolin:~/pyexample$ picard.py It works!

    I know I covered a bunch of stuff here. If you are very new to linux, you can get alot from this article. I hope this was helpful. One final thing to address: as soon as you close your shell and reopen it, it will forget that you had added that directory to your path. If you open the file .bashrc in your home directory, you can add that export PATH command to the file, and it will add that directory every time that you log in.

    Thank you for reading. To contact me, email plays_strats@yahoo.com

    Found Time Games



  • After spending several hours in the coffee shop, playing domain name tennis with godaddy, I finally came up with a name that felt right. The name, “Found Time Games” is inspired by a few things. Jon Blow, one of my heroes, says that games could/should be relevant to the human condition, and not destroy time.