Book Image

Panda3D 1.7 Game Developer's Cookbook

Book Image

Panda3D 1.7 Game Developer's Cookbook

Overview of this book

Table of Contents (20 chapters)
Panda3D 1.7 Game Developer's Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Setting up the game structure


Through the course of this recipe you will learn the steps that are necessary to set up a very basic application structure to get your application going.

Getting ready

To follow the steps of this recipe you should have finished the first recipe in this chapter to have a properly configured development environment.

How to do it...

Follow these steps to create an empty project skeleton:

  1. Start NetBeans and click File | New Project… in the main menu.

  2. Select Python Project and click Next on the first screen of the New Project Wizard.

  3. On the New Python Project screen, set the Project Name and choose a Project Location. Also select Set as Main Project and Create Main File. Set the textbox to main.py, and check that the right Python Platform is active. Click Finish to proceed.

  4. Right-click the Project Name | Sources | Top Level item in the tree view in the Projects tab and select New | Empty Module.

  5. In the window that opens, set the File Name to Application and click Finish.

  6. Paste the following code into Application.py:

    from direct.showbase.ShowBase import ShowBase
    
    class Application(ShowBase):
        def __init__(self):
            ShowBase.__init__(self)
  7. The code that follows goes into main.py:

    from Application import Application
    
    if __name__ == "__main__":
        gameApp = Application()
        gameApp.run()
  8. Open your project directory in Windows Explorer and create folders called models and sounds next to the src folder. The folder structure should resemble the following screenshot:

  9. Open the Config.prc file as described in the prior recipe and add the following lines:

    model-path $MAIN_DIR/../models
    model-path $MAIN_DIR/../sounds
  10. Hit F6 to run the application.

How it works...

First, we start by creating a new project in NetBeans. It generally is a very good idea to name the main file that will be launched by the Python runtime main.py, so we are already set when we want to package our code and assets for redistribution later on.

The Application class, derived from ShowBase, is added as an abstraction of our game application. We must not forget to call the constructor of ShowBase in the constructor of Application or else there won't be a window opening when launching the program.

Because we do not want code files and assets to be scattered in a mess inside one single folder, we add folders dedicated to certain asset types. Depending on the type of project we intend to create, this setup may vary and we may wish to add additional folders. What's important about that is not to forget to add these extra folders to Panda3D's search paths too in Config.prc, just like the models and sounds folders!