Skip to content
Home » Panda3D Part 4 – Runtime Configuration Options

Panda3D Part 4 – Runtime Configuration Options

Spread the love

In the previous part of the series we ran our basic Panda3D app. What we got was an empty game window, pretty small. We will need a bigger window for our game. One option is to resize it each time you run the game, but it will be a different size each time. The way to go is to configure the app window so that it as big as we need when it opens. In this part we’ll see how to configure the app window so that it always is the same size. We’ll also have a look at a couple other runtime configuration options.

The WindowProperties Class

You can use the WindowProperties class to set all kinds of properties of the app window. These properties are set before the window opens. So, what properties can we set? Quite a few, let’s just have a look at those which are of interest to us.

Window Title

Let’s run the app. You can see the default title of the window:

Window Title

If you want to change it, you can use the setTitle method. Here’s the code:

from direct.showbase.ShowBase import ShowBase

# First, we need to import the WindowProperties class.
from panda3d.core import WindowProperties

class Slugrace3D(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

        # Let's create an instance of the WindowProperties class.
        props = WindowProperties()

        # Let's set the window title to 'Test Game'
        props.setTitle('Test Game')

        # Let's enforce the changes. You can use base to refer
        # to the ShowBase class.
        base.win.requestProperties(props)

app = Slugrace3D()
app.run()

If you now run the app, you will see the new title:

window title

Window Position

You can also set the position where the window should be. This is where the origin of the window will be, so its bottom-left corner. The position is measured in pixels, relative to the top-left corner. We must use the setOrigin method.

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).

So, let’s position the window near the bottom-left corner of the screen:

from direct.showbase.ShowBase import ShowBase
from panda3d.core import WindowProperties

class Slugrace3D(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        props = WindowProperties()
        props.setTitle('Test Game')

        # Let's position the origin of the window 100 px to the
        # right of the left border of the screen and 300 px above
        # the bottom border. 
        props.setOrigin(100, 300)
                
        base.win.requestProperties(props)

app = Slugrace3D()
app.run()

Now when you run the app, the window will be set at the specified location. After checking it out, let’s remove the code that sets the origin and play with the window size for a while.

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Window Size

And now let’s configure the size of the window. To this end we can use the setSize method. The method sets the size in pixels and it doesn’t include the decorations of the window, just the useful part of it. Let’s make the window square and pretty small:

from direct.showbase.ShowBase import ShowBase
from panda3d.core import WindowProperties

class Slugrace3D(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        props = WindowProperties()
        props.setTitle('Test Game')
        
        # Let's set the size of the window to 400 x 400 px.
        props.setSize(400, 400)

        base.win.requestProperties(props)

app = Slugrace3D()
app.run()

When you now run the app, you will see the window resized:

window size

And now let’s set it to 1200 x 675 px. This way we’ll maintain the 16:9 ratio:

...

class Slugrace3D(ShowBase):
    ...
        
        # Let's set the size of the window to 1200 x 675 px.
        props.setSize(1200, 675)

        ...

I’m using three dots in code snippets to omit parts of the code that are not relevant in a given context, just for clarity.

If you now run the app, you will see the window sized as expected:

window resized

You can still resize it manually.

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.

Cursor Visibility

When you run the app and hover your mouse over the app window, you will still be able to see the cursor. If you want to hide it when over the window, just use the setCursorHidden method with the argument True to hide it:

...

class Slugrace3D(ShowBase):
    ...
        props.setSize(1200, 675)

        # Hide the mouse cursor when hovering over the app window.
        props.setCursorHidden(True)        

        base.win.requestProperties(props)

...

This time you won’t see the mouse cursor.

We’re done with the runtime configuration for now. Now, with our window configured, let’s add something to it. The gray background is fun, but we want something more. In the following couple posts in this Panda3D series we’ll be modeling the characters and the environment.

Here’s the video version of this article:


Spread the love

Leave a Reply