Book Image

Python Game Programming By Example

Book Image

Python Game Programming By Example

Overview of this book

Table of Contents (14 chapters)
Python Game Programming By Example
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Adding a main menu


The Space Invaders version that we developed in the previous chapter starts a new game as soon as the application is loaded. In most games, an initial screen is displayed and the player can choose between different options apart from starting a new game, such as changing the default controls or taking a look at the high scores.

The cocos.menu cocos2d module offers a Layer subclass named Menu, which serves exactly this purpose. By extending it, you can override its __init__ method and set the style of the title, the menu items, and the selected menu item.

These items are represented as a list of MenuItem instances. Once this list is instantiated, you can call the create_menu method, which builds the final menu with the actions that are executed when a menu item is selected.

While the basic MenuItem only displays a static label, there are several MenuItem subclasses for distinct input modes:

  • ToggleMenuItem: Toggles a Boolean option

  • MultipleMenuItem: Switches between multiple...