2 Game Programming with Python, Lua, and Ruby by Tom Gutschmidt
Total Page:16
File Type:pdf, Size:1020Kb
Game Programming with Python, Lua, and Ruby By Tom Gutschmidt Publisher: Premier Press Pub Date: 2003 ISBN: 1-59200-079-7 Pages: 472 LRN Dedication Acknowledgments About the Author Letter from the Series Editor Introduction What's in This Book? Why Learn Another Language? What's on the CD-ROM? Part ONE: Introducing High-Level Languages Chapter 1. High-Level Language Overview High-Level Language Roots How Programming Languages Work Low-Level Languages Today's High-Level Languages The Pros of High-Level Languages Cons of High-Level Languages A Brief History of Structured Programming Introducing Python Introducing Lua Introducing Ruby Summary Questions and Answers Exercises Chapter 2. Python, Lua, and Ruby Language Features Syntactical Similarities of Python, Lua, and Ruby Hello World Samples Summary Questions and Answers Exercises Part TWO: Programming with Python Chapter 3. Getting Started with Python Python Executables Python Debuggers Python Language Structure Creating a Simple User Interface in Python A Simple GUI with Tkinter Memory, Performance, and Speed Summary Questions and Answers 2 Exercises Chapter 4. Getting Specific with Python Games The Pygame Library Python Graphics Sound in Python Networking in Python Putting It All Together Summary Questions and Answers Exercises Chapter 5. The Python Game Community Engines Graphics Commercial Games Beyond Python Summary Question and Answer Exercises Part THREE: Programming with Lua Chapter 6. Programming with Lua Lua Executables and Debuggers Language Structure Memory, Performance, and Speed Summary Questions and Answers Exercises Chapter 7. Getting Specific with Games in Lua LuaSDL Gravity: A Lua SDL Game The Lua C API Summary Questions and Answers Exercises Chapter 8. The Lua Game Community Game Engines Graphics The Games Themselves Beyond Lua Summary Exercises Part FOUR: Programming with Ruby Chapter 9. Getting Started with Ruby Debuggers Language Structure Memory, Performance, and Speed Summary Questions and Answers Exercises Chapter 10. Getting Started with Ruby Games FXRuby 3 Ruby and OpenGL Ruby and SDL Summary Questions and Answers Exercises Chapter 11. The Ruby Game Community Ruby and Game Engines Ruby and Graphics Ruby and Games Beyond Ruby Summary Questions and Answers Exercises Part FIVE: The Wrap Up Chapter 12. Using Python, Ruby and Lua in Development High-Level Languages in the Development Cycle Extending Python, Lua, and Ruby Python versus Lua Versus Ruby Summary Exercises Appendix A. History of Computer Programming Appendix B. Meet the Family ABC Ada AFNOR C C++ Cobol Eiffel FORTRAN GNU Octave Java Icon Modula Pascal Perl PHP Prolog PureBasic Smalltalk Squeak 4 Dedication This book is dedicated to Hailey and Sidney, the two biggest game players in our household 5 Acknowledgments I would like to thank, in no specific order, the following individuals:André LaMothe and the staff at Premier Press–especially my editors Emi Smith, Mitzi Koontz, and Estelle Manticas. I would also like to thank my past editors Todd Johnson and Kieron Murphy. I want to thank my parents, Katherine and James, for being so supportive over the years. Thanks also to my sister Tanya and her husband David, as well as to the rest of my immediate family–Alex, Raleigh, Steve, Stephanie, May Lou, Dodie, Dick, Bobbie, and Sophie–for their encouragement. I want to especially thank my loving and wonderful wife Susan for putting up with my writing mood swings and geek chattering, and for being so kind when I was slumped over a desk for months in a dark office, furiously typing and staring into the cold blue monitor. Next year I'll try getting some sun. 6 About the Author Thomas Gutschmidt has been professionally involved in the computer industry for the past seven years and currently works for a large software company headquartered in Redmond, Washington. He has been a freelance author and writer for three years and has been involved in several open source game projects and module development projects. He currently lives in the Northwest with his wonderful wife, Susan, their four cats, two rats, several goldfish, and the occasional urban wildlife refugee. 7 Letter from the Series Editor Game development has reached a fever pitch in the past couple of years– photorealistic rendering, advanced physics modeling, a million-plus polygon worlds–and multiprocessor consoles and PCs are powering the revolution. At the same time, experimentation with scripting languages to help control the high-level aspects of games has gone from a convenience to an absolute necessity. No longer can game programmers think of something as absurd as writing a game in C/C++. Game engines may be written in C/C++, but games–no longer. Today's state-of-the-art games are controlled almost exclusively with scripting languages. In the past, scripting languages were custom-made or derivative works made up of the C/C++ compiler and creative use of the pre-processor. Times have changed, and today developers are faced with a number of potential scripting languages to use in their games. Notable players are Python, Lua, and Ruby. Each of these languages has advantages and disadvantages, but any of them can do the job. Game Programming with Python, Lua, and Ruby takes you on a tour and tutorial of each language, highlighting its strengths and weaknesses and offering you detailed examples of getting each language up and running and interfaced to your game and host languages, such as C/C++. With Game Programming with Python, Lua, and Ruby, you won't spend a lot of time learning irrelevant material–instead, you'll get just the information you need. Tom Gutschmidt delivers a non-biased view of each language and gets you up and running as soon as possible in each of the most popular scripting languages today–Python, Lua, and Ruby. Sincerely, André LaMothe Series Editor 8 Introduction This book is unusual because it covers game programming in three different scripting languages in three separate sections. Python, Lua, and Ruby are wonderful languages used all over the world to build efficient, flexible, scalable, and well-integrated programs and systems. For the same reasons that these languages have been great choices for other projects, Python, Lua, and Ruby also are great for making games. This discovery, in fact, was made over a decade ago. Chances are you've played a computer game that utilized one of these languages during development. You may be currently working through game levels that were designed with Lua, or playing on a graphics engine prototyped in Python, or using an Internet ladder developed with Ruby. 9 Introduction This book is unusual because it covers game programming in three different scripting languages in three separate sections. Python, Lua, and Ruby are wonderful languages used all over the world to build efficient, flexible, scalable, and well-integrated programs and systems. For the same reasons that these languages have been great choices for other projects, Python, Lua, and Ruby also are great for making games. This discovery, in fact, was made over a decade ago. Chances are you've played a computer game that utilized one of these languages during development. You may be currently working through game levels that were designed with Lua, or playing on a graphics engine prototyped in Python, or using an Internet ladder developed with Ruby. 10 Why Learn Another Language? Programming languages require a lot of discipline to learn. They each have their own set of formal specifications. They all have unique ways of handling data, data structures, reference mechanisms, and command flow. And underneath all this they each have their own design philosophy. So the question arises: "Why would anyone want to learn a new programming language, let alone three new programming languages?" Well, first of all, these three high-level languages are great starting places to learn programming. For the most part, they are cleanly designed, well documented, and very kind to new programmers. Despite this, they are not toys. They are flexible and powerful, suited for both large projects and classroom exercises. Second, every language has its own strength and weaknesses. The decisions you must make during software and game development become easier when more than one possible tool is available to you. In an ideal development environment, problems are solved in a general way and then the best language for a particular job is chosen. It may be difficult in tomorrow's job market for a programmer to get away with knowing only one or two languages well. Finally, these three languages are really very similar. Much of what you learn from one will be applicable to the others. The more languages you learn, the easier the next one will be to pick up. This compound learning curve eventually begins to work greatly to your advantage, and after enough experience you will get to the point where you can learn a new language in days, simply by relating what is in a given manual to what you already know. 11 What's on the CD-ROM? The CD that comes with this book is designed to launch automatically when inserted into a machine running the Windows operating system. On the CD is the source code for all of the samples and programs written in the book. These are separated into folders organized by chapter. The CD also contains the software necessary to install Python, Ruby, or Lua on your system. This software is also separated into different folders–a Python folder, a Ruby folder, and a Lua folder. Also on the CD are several open-source libraries and utilities that are either used for the source code samples or as examples in this book. These includes PythonWin, Distutils, Numeric Python, PAWS, Py2Exe, Pygame, PyOpenGL, Pyzzle, RubySDL, LuaSDL, and Clanruby. 12 Part ONE: Introducing High-Level Languages Welcome to the first part of this book! In this part, I'll be introducing high-level languages and covering some of their parallel features.