Skip to content
Home » Panda3D Part 2 – A Basic Panda3D App

Panda3D Part 2 – A Basic Panda3D App

Spread the love

Most programming tutorials, regardless of which programming language or framework you are learning, begin with a very basic program where you can see what a program in that particular language or framework must contain to work at all. Let’s do the same here. What does a basic program in Panda3D look like? Here’s our basic Panda3D app.

A Basic Panda3D App

We’re going to write a basic Panda3D app that will display an empty window. It doesn’t sound very fascinating, but if the window is displayed, we will at least know that Panda3D has been installed correctly and everything works.

Create a folder

Anyway, our program is going to open the game window and that’s it for now. Before we write the code, let’s create a folder where we will save it. I’m going to create a new folder for each part in this series so that you can see how far you are in the project at each stage of the course, but you can just create one folder and work on it throughout the whole series. I will name the folders slugrace3D 3 for part 3, slugrace3D 4 for part 4 and so on. So, in slugrace3D 20 you will find the code as it was at the end of part 20 for example.

So, find a location for your project and create a new folder. I will name mine slugrace3D 2. Then open the folder in your editor or IDE. Here’s how to do it in Visual Studio Code:

basic panda3d app

My text editor of choice is Visual Studio Code and if I explain how to do something in the future, it should be understood how to do it in Visual Studio Code. If you are using a different editor, you surely know it quite well and will have no problem doing the same stuff in it.

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Create a new file

When you open your folder, you need a file to write the code to. This is going to be a regular Python file, so with the extension .py. You can name your file whatever you like, I’ll name mine test.py. This file is not going to be part of the project, I’ll just use it to test stuff, hence the name.

Here’s how you can create the file: When you hover your mouse over the name of your folder, a menu will a couple icons will appear. The first icon is the one you should click to create a new file:

new file

All you have to do is type in the name of the file:

create file

As soon as you confirm by hitting Enter, the new file will be listed in the folder (A) and it will open automatically in a new tab (B). This is how we are going to add new files in the future.

test file

Write the code of the Panda3D app

Now we are ready to write our code. Here it is:

# We need the ShowBase class, so let's import it.
from direct.showbase.ShowBase import ShowBase

# Here comes the application class. It inherits from ShowBase. 
class Slugrace3D(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

# And this is where we actually create and run the app.
app = Slugrace3D()
app.run()

So, first we have to import the ShowBase class. This is the class that loads other Panda3D modules that are necessary to execute the program. It also creates the game window and displays it.

Then we define our game class, which inherits from ShowBase.

Finally we create an instance of our application class and run it. The line of code where we call the run method on our app class should be the last line in the file. This method starts the main loop of the game, which renders a frame, handles all the tasks that we define in our code (we’re going to talk about tasks soon) and moves on to the next frame and so it continues.

So, with our basic code in place we are ready to run the app.

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Run Your Panda3D App

There are several ways you run a Panda3D app. We can use the Run Python File in Terminal button in the top right corner (A) to do that. Here’s what we get when we do that: the 3D window shows up with our application running in it (B) and the terminal opens at the bottom (C). For now we can only see a gray game window, but that’s what we expected at this stage.

This game is not playable now, but at least we know it works.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version of this article:


Spread the love

Leave a Reply