O'Reilly logo

Beginning Lua Programming by Kurt Jung, Aaron Brown

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 17. Programming Games with Lua

Although Lua was not designed for video game programming in particular, it is quite popular with game programmers—in fact, they were the first programming subculture in which Lua became a household word.

This chapter is only a short introduction to a large topic. It talks about why and when you would want to use Lua in a game, but most of it is occupied with an example: a simple 2-D action game. In this chapter you learn how to do the following:

  • Program with the SDL library

  • Define sprites

  • Make the sprites move, either automatically or in response to user input

  • Detect collisions between sprites

Understanding Why and When to Use Lua

It is possible to write games entirely in Lua, but generally there's a division of labor between Lua and at least one other language. This is for two reasons:

  • There are some things (graphics, sound, and interaction with non-character-oriented hardware) that Lua simply cannot do on its own.

  • As languages of its type go, Lua is very fast, but some games, because of their real-time nature, have speed demands that can only be satisfied by less flexible languages like C and C++.

In parts of a game (or any program, really) where either of those reasons apply, you should use another language. In parts where they don't apply, you can use Lua and benefit from its strong points (that generally aren't shared by languages that beat it in the preceding two departments). The benefits of Lua include the following:

  • Lightweight compilation: In ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required