(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
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
10 on a graph, we can see the pattern:
This post goes into the details of how you can add a “save game” feature to your games. Python’s built-in shelve module makes this very easy to do, but there are some pitfalls and tips that you might want to learn from this post before trying to code it up yourself.
To give an example of adding a “save game” feature to a game program, I’ll be taking the Flippy program (an Othello clone) from Chapter 10 of “Making Games with Python & Pygame” (and Reversi from Chapter 15 of “Invent Your Own Computer Games with Python”.)
If you want to skip ahead and see the Flippy version with the “save game” feature added, you can download the source code and image files used by the game. You need Pygame installed to run Flippy (but not Reversi).
I made a twitter bot that checks every hour for someone who has asked the question, “Why do homeless people have dogs?” and automatically replies, “Because a dog will love you even though you are homeless.” It’s running right now at @YHobosHaveDogs.
Figuring out how to code this took a couple evenings and a little hair pulling, so I decided to document the process in this blog article to make it easier for the next programmer. This will be making a Twitter bot in Python using the python-twitter module (which runs on Python 2, not Python 3), and then running the bot from my Dreamhost server (but most likely any web host will work just fine. Or if you have a computer that is always online, you can run the bot from that). First we will run the bot from our machine to test it out, and then load it onto the Dreamhost web host. (I’m running a Windows box, but the steps should work on any OS.)
- Download the python-twitter module (I tried some of the other modules but didn’t like them as much.)
- Unzip this file and from the command line in the unzipped directory, run “python setup.py install” to install the twitter module.
Here’s the introductory chapter to my next book on Python and Pygame. It assumes you have some Python and programming experience (if you don’t, give a look to Invent with Python), and offers a quick view of all of the major parts of Pygame.
The chapter by itself is a good introduction to Pygame. If you’ve been meaning to give Pygame a look, then download the chapter. Please email in any questions or suggestions you have: email@example.com
Unfortunately, the links in the book aren’t working yet, but should be in a few days.
If you already know a bit of programming and Python, and want to get up to speed on the Pygame 2D game framework, here’s a cheat sheet that you can look over. It implements a very short Pygame program covering most of Pygame’s basic features.
View the Pygame Cheat Sheet.
You can also download the Pygame program itself, along with the cat.png and bounce.wav files it uses. You will need to install Python and Pygame first to run this program. The program looks like this when you run it:
Pygcurse (pronounced “pig curse”) is a curses library emulator that runs on top of the Pygame framework. It provides an easy way to create text adventures, roguelikes, and console-style applications. The mascot of Pygcurse is a blue pig with a skull tattoo on its butt.
Download Pygcurse and Demo Programs.
Read the Pygcurse tutorial.
View the Pygcurse homepage.
Pygcurse provides several benefits over normal text-based stdio programs:
- Color text and background.
- The ability to move the cursor and print text anywhere in the console window.
- The ability to make console apps that make use of the mouse.
- The ability to have programs respond to individual key presses, instead of waiting for the user to type an entire string and press enter (as is the case with input()/raw_input()).
- The ability to use any font and any character in those fonts.
- Since the console window that Pygcurse uses is just a Pygame surface object, additional drawing and transformations can be applied to it. Multiple surfaces can also be used in the same program.
Pygcurse also provides some additional features that curses normally doesn’t, such as tinting, shadows, textboxes, and line drawing functions.
Pygcurse requires Pygame to be installed. Pygame can be downloaded from http://pygame.org. Pygcurse can be used with either Python 2 or Python 3.