Flylib.com

Books Software

 
 
 

- page 30

[ LiB ]

Summary

I've covered quite a bit in this short chapter. Before you move on to more specifics with Python in the next chapter, or the other languages later on, you'll want to be sure you are familiar with Boolean logic and general program flow and structure with conditional and iterative control constructs. Important points from this chapter:

  • Python, Lua, and Ruby organizations keep active lists of projects that are pretty extensive .

  • Math and algebra are handled very similarly in each language.

  • Boolean operators, Boolean comparisons, conditional control statements, and iterative control statements can all be used to control the flow of a program.

  • Lists, strings, and a number of other commands all look and are handled in a similar way in each language.

  • Implementing "Hello World" in a standard way in C takes more lines of code and more symbols than any of the other three languages.

[ LiB ]
[ LiB ]

Questions and Answers

1:

Q: Why are there more projects in Python and Lua than Ruby?

A:

A: Ruby is probably the most difficult of the three languages to find evidence of in the game industry. This is partly due to the language barrier (again, most modern Ruby development is in Japanese) and also because shops today tend to be using Ruby more for projects with the World Wide Web, XML integration, text processing, and general scripting. That doesn't mean Ruby isn't suited for game development; quite the contrary, as you will shortly see.

2:

Q: I already know how to program "Hello World", when do I get to write graphics and games ?

A:

A: You'll start writing much more in-depth code in the very next chapter.

[ LiB ]
[ LiB ]

Exercises

1:

Describe the difference between a conditional control statement and an iterative control statement.

2:

Boolean logic uses only two values. Which two values are they?

3:

Which else/if structure ( elseif , elsif , and elif ) goes with which language (Python, Lua, and Ruby)?

4:

When printing a simple statement (like "Hello World"), one of the three languages normally uses a puts command instead of a print command. Which one is it?

[ LiB ]
[ LiB ]

Part TWO: Programming with Python

The next three chapters are all about Python. This part of the book starts with an overview of the Python language and its syntax, then moves in to examine commonly used libraries for writing games in Python, including Pygame and PyOpenGL. Finally, a few real-world Python game projects are examined.


[ LiB ]
[ LiB ]

Chapter 3. Getting Started with Python

Latet anguis in herba

Virgil (70-19 BC), Roman poet, "Aeneid" (Translation: There's a snake hidden in the grass.)

Let's jump right into programming with Python. I'll start with an introduction to a few useful tools and then give you a speedy overview of the Python language.

[ LiB ]
[ LiB ]

Python Executables

You can execute .py files once Python is installed on your machine, but that doesn't make your Python game programs universally playable . You still need to convert your scripts into a bundled executable for whatever platform you want to run on. Luckily, there are a few resources for accomplishing just that.

Packaging Python Code

When modules are imported in Python by other modules, Python compiles the relevant code into byte-code, an intermediate, portable, closer-to-low-level binary language form. This byte-code is stored with the .pyc suffix, short for Python compiled , instead of the typical .py.

Python's .pyc files correspond roughly to DLLs (dynamically loaded libraries) used in C. Regular .py modules can be used dynamically, too, but the compiled Python code is tighter and Python interprets the code at runtime when the file is imported.

Precompiling scripts is one way to speed up Python programs that need to import many modules. You can minimize a program's startup time by making sure source code is kept in directories where Python will have access to writing .pyc files.

You can also ship Python programs as .pyc files rather than as .py scripts. Since .pyc files are binary, they cannot be run as scripts, but they can be sent to the Python interpreter; simply add the name of the .pyc file the next time you run Python, like this:

Python runme.pyc

In order to build a compiled Python file from the Python interpreter, import the compile function from py_compile and run the compile command, like this:

from py_compile import compile
compile("script_to_compile.py")

Freeze

Freeze is a system that takes Python script files and turns them into modules packaged into C files. Originally Freeze was used as one way to ship Python source, but it is now mostly defunct , although it will still be available in Version 2.3 for backwards -compatibility. The compiled script that Freeze generates allows a Python program to ship without the source code in plain view and without using .pyc files. The benefits to Freeze are that you can ship Python as two .c files and a makefile instead of as a .py, and you can make Python runable on platforms that do not have Python installed. The downside is that Freeze doesn't work well initially with Tkinter and other Windows GUIs.

ActiveState

ActiveState is a company that focuses on applied open source. It creates development packages for software developers and provides resources for Perl, Python, and PHP development. ActiveState currently has a Python distribution called ActivePython. It also supports creating Python RPM (Red Hat Package Managers) installers, Windows complete installers , and a Visual Studio .NET IDE plug-in for Python. These services (some are free, others not) are available at the ActiveState Python Website, at http://www.activestate.com/Solutions/Programmer/Python.plex.

py2exe

The py2exe extension is an open source utility that converts Python scripts into executable Windows programs. The software is copyrighted by Thomas Heller but is freely distributable, and you'll find a copy with the license on the accompanying CD under Python/py2exe.

The extension is still under development but has expanded recently to include the ability to turn Python scripts into Windows NT-like services; it has been used to create a number of popular Python applications, such as wxPython, Tkinter, and pygame (you'll get to know these applications a bit better in the next chapter).

py2exe is a Distutils (Python Distribution Utilities) extension, and relies on the work by Greg Ward to make Python programs distributable (see the Disutils Website at http://www.python.org/doc/current/dist/). The Distutils are necessary for py2exe to work and are also included on the Python folder in this book's CD.

[ LiB ]