Panda3D is a game engine which allows developers to make 3D games using Python.
The simplest game we can write using Panda3D is:
from direct.showbase.ShowBase import ShowBase class Game(ShowBase): """A Panda3D game.""" def __init__(self): """Constructor.""" ShowBase.__init__(self) if __name__ == '__main__': game = Game() game.run()
The above code will simply show an empty game window.
Start the game maximised
By default, Panda3D games start in a centered, 800×600 window. The following hack will make the window start maximised instead. However, it only works on Windows, and has only been tested in Windows 10.
First, import win32gui
and win32con
.
import win32gui, win32con
Add the following lines to the __init__
method of the Game
class, after the line ShowBase.__init__(self)
.
# Get window handle (Windows only). self.hwnd = win32gui.GetForegroundWindow() # Maximise window (Windows only). win32gui.PostMessage(self.hwnd, win32con.WM_SYSCOMMAND, win32con.SC_MAXIMIZE, 0)
The next time you start the game, the window should be maximised. At any time while the game is running, you can call the win32gui.PostMessage
function in the same way, and the window will be maximised again.
Start the game in borderless mode
Borderless mode consists on a window without borders or title bar, which has also been stretched to occupy the whole screen. Playing in borderless mode is exactly like playing in fullscreen mode, but it retains all the perks of playing in a window. For example, you can Alt-Tab into and out of the game instantanously, and possibly get improved performance.
To start the game in borderless mode, we first need a method to find the screen resolution. This can be achieved through the methods get_display_width()
and get_display_height()
in the pipe
class, which is in turn an attribute of our Game
class.
Create the following method inside the Game
class, which will return the screen resolution. The method includes a failsafe in case get_display_width()
or get_display_height()
fails and returns 0.
def get_resolution(self): """Returns the screen resolution.""" x, y = self.pipe.get_display_width(), self.pipe.get_display_height() return (x, y) if x > 0 and y > 0 else (800, 600)
Now we need to call this method to get the screen resolution. Insert the following method call into the __init__
method of our Game
class, after the line ShowBase.__init__(self)
.
# Get screen resolution. self.res_x, self.res_y = self.get_resolution()
Next, import loadPrcFileData
and WindowProperties
from panda3d.core
.
from panda3d.core import loadPrcFileData, WindowProperties
To remove the decorations (i.e. borders and title bar) from the window, add the following line, again in the __init__
method but before the line ShowBase.__init__(self)
.
loadPrcFileData('', 'undecorated 1')
And to stretch the window to fill the whole screen, add the following lines after the line ShowBase.__init__(self)
.
# Set window size to equal screen resolution. wp = WindowProperties() wp.setSize(self.res_x, self.res_y) self.win.requestProperties(wp)
If you run the game now, it will start in borderless mode.
You can also add the following method to the Game
class, which will allow you to activate borderless mode at any time while the game is running.
def go_borderless(self): """Makes window borderless during runtime.""" wp = WindowProperties() wp.setUndecorated(True) wp.setSize(self.res_x, self.res_y) self.win.requestProperties(wp)
If running the game under Windows, you can additionally maximise the window as shown in the previous section, as a redundant way to ensure that the window is always filling the whole screen.