O'Reilly logo

Learn Game Programming with Ruby by Mark Sobkowicz

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

Use Physics to Move a Character

The hero, Chip, is an object in the physics space as well. He can be knocked off course by boulders and will be stopped by walls, platforms, and the floor. Chip will also run and jump when you press the arrow keys and spacebar. You move him by exerting forces on him while the keys are pressed.

Create a new file to hold the Chip class, chip.rb. In initialize, you first create Chip’s physics properties, @body and @shape. Chip’s mass will be 50, which is one-eighth the mass of a boulder.

When Chip runs, you want him to look like he is running. To achieve this, you use a sprite sheet for his image, much like the one you used to animate the explosions in Make Animated Explosions. This sprite sheet has eight images ...

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