How Does Compression Work?

"Zip" programs that can compress multiple files into one smaller .zip file are fairly popular for downloads since the fewer bytes you have to download the faster it will download. But how do you compress files? Files are made up of ones and zeros, which can't be squished like clothes into a tight suitcase.

Think about file compression working like this: If someone asked you what the lyrics to "99 bottles of beer on the wall" were, you would tell them:

"The song goes '99 bottles of beer on the wall, 99 bottles of beer, take one down, pass it around, 98 bottles of beer on the wall', and then you repeat the lyrics with a number one less than the previous time."

The above sentence has 42 words in it. You wouldn't tell the person literally the entire song (which has 2,376 words in it). The reason you don't have to recite the full song is because of the song's repetition. Repetition makes compression possible. We've achieved a 98% compression rate (counting by the word amount) with the description of the lyrics over the literal lyrics (42 words vs. 2,376 words), even though both detail exactly the same song.
(more…)

Read More

Source Code Makeover: Square Shooter, Part 2

UPDATE: Felix, the original author of Square Shooter, has made some comments in the comment section that are pretty insightful. I recommend you check them out.

This is a continuation from Part 1, where I go through the source code of Square Shooter, an Asteroids clone, and try to redesign the code to be more readable. (Part 3 is also up.)

All of the changes I make can be viewed on github. You can also download the original source code for Square Shooter.
(more…)

Read More

Source Code Makeover: Square Shooter, Part 1

UPDATE: Felix, the original author of Square Shooter, has made some comments in the comment section that are pretty insightful. I recommend you check them out.

In this blog post, I'm taking a game off of Pygame.org and going through it to make it more readable and extend its functionality. You'll see an example of how to take code that works and changes to improve it's design (but more importantly, I explain why I make those changes). This is an intermediate level tutorial and assumes some familiarity with Python. This can be pretty helpful if you know programming basics but want to know, "How can I write better code?"

(Part 2 and Part 3 are now up as well.)

This blog post covers Square Shooter, made by Felix Pleșoianu. You can download the original source or view my changes on GitHub.

I only check in working code, so you can download the code at any point in history and see how it looks. I make a lot more check ins than I normally would, but I want you to see the gradual changes that I make.

(UPDATE: A note to everyone who mentions that I break PEP-8, see this link. Also, see this link.)

Let's play it. You'll need to have Python (a version 2, not version 3) and Pygame installed on your computer to run the square-shooter_original.py file. It looks like a standard Asteroids clone. I notice that the circles dont wrap around the map until the center of the circle goes past the edge. Also, the bullets don't wrap around either, they just stop once they get to the edge.

Now take a look at the original source code. Try to get an idea of what each of the parts do, but you don't need to fully understand everything before continuing. (I didn't understand all the code when I started cleaning it up.)

First we'll do just a code clean up, and then we'll look at changing the the game to improve play and user experience.

The computer doesn't care what your code looks like. It will run the most unreadable code just fine. But the purpose of cleaning up code to be readable is so that it is easier to understand for humans so they can easily change code either for new features or bug fixes. This becomes especially important if multiple people are working on the same source code.

(more…)

Read More

Need a Game Idea? A List of Game Mechanics and a Random Mechanic Mixer.

If you need an idea for a game, try this random game mechanic generator:

A full list is at the bottom of this page. I wanted to get fairly low level with these game mechanics, so I don't include things such as "first person shooter" or "puzzles" (since those are, in my opinion, more correctly called genres).

Just to prove to myself that this generator isn't worthless, I decided to make some small Python/Pygame games based off of it. These games are fairly rough since I wanted to rapidly produce them as proof-of-concepts. The mechanics that got selected were:

(more…)

Read More

Using Trigonometry to Animate Bounces, Draw Clocks, and Point Cannons at a Target

About Trigonometry

(Links to the Python programs in this tutorial. Requires Python and Pygame to be installed.)

Trigonometry is the branch of mathematics that studies triangles and the relationships between their sides and the angles between these sides. The sine, cosine, and tangent trigonometry functions are implemented as programming functions in most languages and given the names sin(), cos(), and tan(). In Python, these functions exist in the math module. These functions are very handy in games and graphical programs because of the smooth wave-like pattern their return values produce.

This tutorial shows how you can get a variety of neat animation behavior using these functions in your programs. (The animated gifs you see above were taken from the programs in this tutorial.) The code examples in this tutorial should work with both Python 2 and Python 3.

Trig Function Basics

You don't need to know how these functions work. Let's just treat these functions as black boxes: they take one number parameter in, and return a float value out. If you already know the mathematics of sine and cosine, then you can just skim this section.

In the interactive shell, let's see what math.sin() returns for some values:

>>> import math
>>> math.sin(1)
0.8414709848078965
>>> math.sin(2)
0.90929742682568171
>>> math.sin(3)
0.14112000805986721
>>> math.sin(4)
-0.7568024953079282
>>> math.sin(5)
-0.95892427466313845

It looks like math.sin() just spits out some random-looking float values. (Actually, it's the length of the opposite side divided by the hypotenuse of a right triangle with the given angle. But you don't need to know or understand this.) But if we graph the return values of the input arguments 1 through 10 on a graph, we can see the pattern:

(more…)

Read More