官术网_书友最值得收藏!

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!

主站蜘蛛池模板: 开化县| 桃江县| 阜康市| 永城市| 甘肃省| 东乡族自治县| 临泉县| 板桥市| 夏河县| 开封市| 得荣县| 泰州市| 丘北县| 绍兴市| 惠来县| 松原市| 贵港市| 同德县| 宜兰县| 南部县| 遵义市| 汉源县| 石渠县| 田林县| 邵武市| 陇南市| 玛沁县| 宾阳县| 青川县| 鄂温| 曲周县| 嘉黎县| 新营市| 马龙县| 吉隆县| 宜川县| 马关县| 兴化市| 策勒县| 绥宁县| 晋中市|