Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python Game Programming By Example

You're reading from   Python Game Programming By Example A pragmatic guide for developing your own games with Python

Arrow left icon
Product type Paperback
Published in Sep 2015
Publisher
ISBN-13 9781785281532
Length 230 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

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...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime