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 ...

Get Learn Game Programming with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.