Panda3D tutorial

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.

Panda3D tutorial

Installing Arch Linux offline

Arch Linux is usually installed using an Internet connection. However, it is also possible to install it completely offline from the official ISO images. This article will guide you through that process.

Download the latest Arch Linux ISO image for your CPU architecture, be it 64-bit or 32-bit. Burn the image to a CD, or create a bootable USB stick using Rufus.

Before booting from the CD or USB stick, it is important to know the boot mode that your computer uses: BIOS or UEFI. Older computers can only boot in BIOS mode. Most modern computers can boot in both modes; but if yours can use UEFI, it is highly recommended that you use it.

If you decide to use UEFI, you must make sure to boot from the CD or USB stick in UEFI mode; otherwise you won’t be able to install the boot loader. To this end, access the UEFI menu and disable both the BIOS boot mode (or “legacy mode”) and Secure Boot.

Now boot from the CD or USB stick. In the screen that appears, select the option to boot Arch Linux, and press Enter. You will be presented with a command line.

Prepare the disk

Check the name and type of the disk:

fdisk -l

We will assume that the name of the disk is /dev/sda.

Next, we need to partition the disk. The procedure to do this will vary depending on whether the disk type is MBR or GPT, and also on whether you’re using the BIOS or UEFI boot mode.

The MBR disk type is usually used in combination with BIOS, whilst the GPT disk type is usually combined with UEFI. Other combinations are possible but not recommended, therefore we will only cover these two combinations here.

If using MBR, access the cfdisk partitioning tool to alter the disk partitions (if using GPT, use cgdisk instead):

cfdisk /dev/sda

Once inside the tool, delete all the existing partitions.

If using BIOS with MBR, create the following partitions:

  1. A root partition called /dev/sda1. Set its size to equal the total disk space minus 750 MB. Set its type to Linux filesystem. Create it as a primary partition, and then mark it as bootable.
  2. A swap partition called /dev/sda2. Set its size to 750 MB, and its type to Linux swap. Create it as a primary partition.

If using UEFI with GPT, create the following partitions:

  1. A root partition called /dev/sda1. Set its size to equal the total disk space minus 1024 MB. Set its type to Linux x86-64 root.
  2. A swap partition called /dev/sda2. Set its size to 750 MB, and its type to Linux swap.
  3. An EFI partition called /dev/sda3. Set its size to 274 MB, and its type to EFI system.

Finally, write the changes and quit the partitioning tool. You can type fdisk -l to see the new partitions.

Format the dev/sda1 root partition using the ext4 filesystem:

mkfs.ext4 /dev/sda1

If using UEFI, format the EFI partition using the FAT32 filesystem:

mkfs.fat -F32 /dev/sda3

Now, format and activate the dev/sda2 swap partition:

mkswap /dev/sda2
swapon /dev/sda2

Mount the dev/sda1 root partition on the /mnt directory:

mount /dev/sda1 /mnt

Install the Arch Linux base system

Copy the Arch Linux base system from the live environment to /mnt (the -x switch excludes some files that shouldn’t be copied):

cp -ax / /mnt

This may take a while depending on your computer.

Copy the kernel image to the new root:

cp -vaT /run/archiso/bootmnt/arch/boot/$(uname -m)/vmlinuz /mnt/boot/vmlinuz-linux

Generate an fstab file:

genfstab -U /mnt >> /mnt/etc/fstab

Next, change root into the new system:

arch-chroot /mnt

Restore the configuration of journald:

sed -i 's/Storage=volatile/#Storage=auto/' /etc/systemd/journald.conf

Remove a special udev rule created for the live environment:

rm /etc/udev/rules.d/81-dhcpcd.rules

Disable and remove services created for the live environment:

systemctl disable pacman-init.service choose-mirror.service
rm -r /etc/systemd/system/{choose-mirror.service,pacman-init.service,etc-pacman.d-gnupg.mount,getty@tty1.service.d}
rm /etc/systemd/scripts/choose-mirror

Remove special scripts of the live environment:

rm /root/{.automated_script.sh,.zlogin}
rm /etc/mkinitcpio-archiso.conf
rm -r /etc/initcpio

Import Arch Linux keys:

pacman-key --init
pacman-key --populate archlinux

Configure the new system

To set the time zone, first find your region and city in /usr/share/zoneinfo/. Assuming that your region/city is /America/Detroit, run the following command:

ln -sf /usr/share/zoneinfo/America/Detroit /etc/localtime

Set the hardware clock:

hwclock --systohc

To configure the locale, edit the file /etc/locale.gen and make sure the line en_US.UTF-8 UTF-8 is uncommented.

Then generate the new locales by typing:

locale-gen

If it doesn’t exist, create the /etc/locale.conf file with the following content:

LANG=en_US.UTF-8

Edit the /etc/hostname file with your desired hostname, for example:

mycomputer

Also edit the /etc/hosts file using the same hostname, as:

127.0.0.1   localhost
::1         localhost
127.0.1.1   mycomputer.localdomain  mycomputer

Create the root password by issuing the following command and then typing your desired password:

passwd

Create an initial ramdisk:

mkinitcpio -p linux

Finally, we need to install the GRUB boot loader. The procedure to do this varies for BIOS and UEFI. If using BIOS, simply run grub-install as follows:

grub-install /dev/sda

If using UEFI, create the /efi directory, mount the EFI partition on it, and finally run grub-install as follows:

mkdir /efi
mount /dev/sda3 /efi
grub-install --target=x86_64-efi --efi-directory=/efi --bootloader-id=GRUB

Generate the GRUB config file:

grub-mkconfig -o /boot/grub/grub.cfg

Exit arch-chroot:

exit

And that’s it! A minimal Arch Linux system has been installed on your disk. Reboot your computer to access it:

reboot
Installing Arch Linux offline

Git commands

Undo last commit, losing changes to files:

git reset --hard @~1

Undo last commit, keeping files untouched:

git reset --soft @~1

Edit last 2 commits:

git rebase -i @~2

Overwrite remote branch with local branch:

git push --force

Overwrite local branch with remote branch:

git fetch
git reset --hard origin/master
Git commands

Access Windows shared folder from Linux

First, you need to install cifs-utils (requires root):

pacman -S cifs-utils

Create a new directory called shr where the shared folder will be mounted:

mkdir shr

Mount the shared folder on shr (requires root):

mount.cifs //COMPUTER-NAME/folder shr -o user=USER

Enter the Windows password when prompted and press Enter.

You should now be able to see the contents of the shared folder under shr.

Access Windows shared folder from Linux

Using SSH with Git

Issue the following command to generate a public/private SSH key pair:

ssh-keygen

Press Enter to use the default directory and filename for your SSH key (e.g. C:\Users\YOURUSER\.ssh\id_rsa on Windows). Then enter a passphrase, and re-enter it. (You could also choose not to use a passphrase, but that’d be less secure.)

When ssh-keygen finishes running, the public and private keys should have been generated. In the .ssh directory, there should be two files: id_rsa and id_rsa.pub. These are the private and public keys respectively.

Finally, provide the remote server (e.g. GitHub) with the public key. To do this, log into the remote server (e.g. GitHub), and find the option to add a SSH public key. When prompted, copy-paste the contents of id_rsa.pub, the public key file.

Note that the private key file (id_rsa) should be kept secret! If a hacker gets access to it, he could use it to impersonate you and access the remote server. However, if you chose to set up a passphrase, the hacker would need to know it too to be able to impersonate you. As you can see, the passphrase acts like a second layer of security, and will protect you in case the private key gets compromised.

Using SSH with Git

Installing i3 on Arch Linux

First, the X Window System must be installed. To this end, install the packages xorg-server and xorg-xinit using pacman:

pacman -S xorg-server xorg-xinit

Also, you must install the appropiate video driver for your graphics card. You can identify your graphics card by typing lspci | grep -e VGA -e 3D. Type pacman -Ss xf86-video to see the list of drivers that you can install.

Assuming that you have an Intel graphics card, the appropiate driver to install would be xf86-video-intel:

pacman -S xf86-video-intel

Next, install i3-wm:

pacman -S i3-wm

Install the ttf-dejavu font:

pacman -S ttf-dejavu

Install i3status, a status bar for i3:

pacman -S i3status

Install dmenu, a menu to launch programs from the i3 desktop:

pacman -S dmenu

Create the ~/.xinitrc file with the following content:

exec i3

Now you can launch i3 with the following command:

startx

Using i3

To open a terminal, press $mod+Enter. (By default, the $mod key is equal to the Windows key.)

To exit i3, press Shift+$mod+e. In the dialog that appears, click Yes, exit i3. Alternatively, to force-quit, kill the X server by typing pkill Xorg.

To restart i3 (e.g. after you’ve changed some settings), press Shift+$mod+r. Your open windows will be preserved.

To open dmenu (a menu that lets you easily launch programs) press $mod+d. Start writing the name of the program you want to launch, and dmenu will try to autocomplete it. To launch the selected program, press Enter. To close the menu instead, press Esc.

To make the current window go fullscreen, press $mod+f. Repeat to undo.

Press $mod+r to enter resize mode. In this mode, you can resize the current window using the $left, $down, $up, and $right keys. (By default these are the j, k, l and ; keys; you can also use the arrow keys.) Repeat $mod+r to exit resize mode.

Press $mod+Shift+Space to toggle floating mode, which is the opposite of tiling mode and will allow the windows to overlap.

Configuring i3

Copy /etc/i3/config to ~/.i3/config. From now on, you can edit ~/.i3/config to change the configuration of i3. You can then reload the config file from inside i3 by pressing Shift+$mod+c. If your config file is not being loaded, make sure that ~/.config/i3/config does not exist.

To set a bigger font for window titles and the status bar, modify the appropiate line in ~/.i3/config as follows:

font pango:DejaVu Sans Mono 12

By default, $mod+Enter launches i3-sensible-terminal, a basic and non-configurable terminal. To make it launch xterm instead, modify the appropiate line in ~/.i3/config as follows:

bindsym Mod1+Return exec xterm

To remove the title bars from all windows (leaving only a 1px border around them), add the following line to ~/.i3/config (e.g. at the end of the file):

default_border pixel 1

You will need to close and reopen the windows to see the changes. To change the color of the window borders, add the following lines to ~/.i3/config:

client.focused #69BBED #69BBED #69BBED #69BBED
client.focused_inactive #333333 #333333 #333333 #333333
client.unfocused #333333 #333333 #333333 #333333
client.urgent #FF0000 #FF0000 #FF0000 #FF0000

There are other settings which are controlled not by i3 but by the X Window System. Create now the ~/.Xresources file (if it doesn’t already exist). Next, add the following line to ~/.xinitrc, before the exec i3 line:

xrdb ~/.Xresources

From now on, you can edit ~/.Xresources to change the configuration of the X Window System. If needed, you can then make the X Window System reload the ~/.Xresources file by typing xrdb ~/.Xresources.

The xterm terminal defaults to black text on a white background. To reverse this (i.e. have white text on a black background), add the following line to ~/.Xresources:

XTerm*reverseVideo: true

To set a black border for xterm, add the following line to ~/.Xresources:

XTerm*borderColor: black

To set a bigger font for xterm, add the following line to ~/.Xresources:

XTerm*faceName: Liberation Mono:size=12:antialias=false

By default, xterm uses a separate clipboard from the system clipboard. To use the system clipboard instead, add the following line to ~/.Xresources:

XTerm*selectToClipboard: true

Note that in xterm, pasting is done with Shift+Insert instead of the classic Control+V. Text you select with the mouse is automatically copied.

Installing i3 on Arch Linux

Configuring Linux users

Create a new normal user named user1 (the -m option creates a home directory for him, and -g assigns him to the group users):

useradd -m -g users user1

To set the password for user1, type:

passwd user1

To set user1 to have no password, type:

passwd -d user1

To add sudo capabilities to user1, run the visudo command. In the text file that opens, add the following line in the corresponding section:

user1 ALL=(ALL) ALL

Log off current user:

exit

To temporarily switch to user user2, type:

su user2

Change ownership of the /home/user1/ directory to user1:

chown user1 /home/user1/

Delete the user user1:

userdel user1

To list all users, read the file etc/passwd:

less /etc/passwd

See the groups to which user1 belongs (or the current user, if user1 is ommited):

groups user1
Configuring Linux users

Arch Linux commands

Arch Linux’s package manager is called pacman. It can be used to install new packages, update them or remove them. All installation and removal of packages requires root access.

Update all the installed packages, including system packages:

pacman -Syu

It is recommended to run the above command frequently, to keep the system up-to-date. However, be warned that if you interrupt this command while it’s installing system packages, the operating system could be left in a broken state.

Search for “firefox” in the Arch Linux repositories:

pacman -Ss firefox

Install the firefox package:

pacman -S firefox

Check the version of firefox currently installed:

pacman -Q firefox

List the packages that need updating:

pacman -Qu

Remove the firefox package:

pacman -R firefox

Install w3m, a command-line web browser with vim key bindings:

pacman -S w3m

Install conky, a system monitor, similar to the Windows Task Manager:

pacman -S conky

List all the running processes:

ps -A | less

Install xfce, a desktop environment (requires X Windows System):

pacman -S xfce4

List all files in current directory, including hidden files (those that start with a dot):

ls -a

Read current time, according to the system clock:

timedatectl

Start NTP, a service to synchronise the system clock with internet time servers (must be run as root):

ntpd -u ntp:ntp

Activate NTP syncronisation of the system clock (must be run as root):

timedatectl set-ntp on

To control the audio volume, type alsamixer. Use the left and right keys to select an audio channel, j and k to decrease/increase the volume, and m to mute/unmute the channel. Press esc to exit the program.

To test the speakers, type speaker-test.

Create a symbolic link to /usr/lib32/libcurl.so.3 from /usr/lib/libcurl.so.3 (must be run as root, the -s switch stands for symbolic):

ln -s /usr/lib32/libcurl.so.3 /usr/lib/libcurl.so.3

Completely and securely wipe disk dev/sda (dangerous, will ovewrite the whole disk with random bytes):

dd if=/dev/urandom of=/dev/sda bs=4096 status=progress

Display total RAM size (see total column):

free -h

Display total and available disk space:

df -h

To display the screen resolution, first install xorg-xdpyinfo through pacman, and then type:

xdpyinfo | grep dimensions
Arch Linux commands

Using multiple Git remotes

First, check the list of all the remotes of the repository and their URLs:

git remote -v

Add the first remote (in this case a BitBucket remote):

git remote add bbucket https://bitbucket.org/YOUR/REPO

Add the second remote (in this case a GitLab remote):

git remote add gitlab https://gitlab.com/YOUR/REPO

If you make a mistake, you can remove a remote by typing git remote remove bbucket. Alternatively, you can edit directly the .git/config file in your repository folder instead of doing it through commands.

At this point, we can push to the two remotes by typing git push bbucket and git push gitlab. However, what if you want a single command to push to both remotes? There are two main methods you can use to achieve this: Creating a third remote, or creating an alias.

Creating a third remote

Add an origin remote that will serve to push to both BitBucket and GitLab with a single command:

git remote add origin https://bitbucket.org/YOUR/REPO
git remote set-url --add origin https://gitlab.com/YOUR/REPO</code>

Check the final state of your remotes by again typing git remote -v. Each remote should have two URLs (one fetch URL, one push URL), except origin, which should have three URLs (one fetch URL, two push URLs). If you made a mistake, you can delete an URL from a remote by typing git remote set-url --delete origin https://bitbucket.org/BAD/URL.

Push to the origin remote using the following command (the -u switch makes origin the default push/pull remote):

git push -u origin master

From now on, git push will push to both remotes, whilst git pull will pull from BitBucket. To push/pull to/from a specific remote, specify the name of the remote in the command, as in git push bbucket or git pull gitlab master.

Creating an alias

Alternatively, you can create a pushall alias by typing the following command:

git config --global --replace-all alias.pushall "!git remote | xargs -L1 git push --all"

Once the alias has been created, typing git pushall will push to all existing remotes, without the need to create any additional remotes. The drawback of this command is that you won’t be able to use more advanced git push options with it, such as specifying a branch to push.

Using multiple Git remotes

Installing Arch Linux

Download the latest Arch Linux ISO image for your CPU architecture, be it 64-bit or 32-bit. Burn the image to a CD, or create a bootable USB stick using Rufus. Then boot from the CD or USB stick.

In the screen that appears, select Boot Arch Linux and press Enter. You will be presented with a command line.

Connect to a wireless network

There are basically two ways in which you can connect to a wireless network.

The easy way

Type the following command:

wifi-menu

And just select your network from the menu, and enter the password if prompted.

You can test your connection using ping:

ping google.com

The hard way

Find the name of your wireless interface:

ip link

From now on we will assume the name of your interface is wlan0.

Bring the interface up:

ip link set wlan0 up

Scan for available networks:

iw dev wlan0 scan

You will see a list of all available wireless networks. Take note of the SSID of the network you want to connect to.

Assuming that the network is protected by WPA, you will need to use wpa_supplicant to connect to it. First, specify the WPA passphase using the following command:

wpa_passphrase SSID >> /etc/wpa_supplicant.conf

In the prompt that appears, enter the WPA passphrase, and press Enter. The password will be saved to wpa_supplicant.conf.

Then, use the following command to connect to the network:

wpa_supplicant -B -D wext -i wlan0 -c /etc/wpa_supplicant.conf

To check that you’ve successfully connected to the network, use the following command:

iw dev wlan0 link

Next, obtain an IP address:

dhclient wlan0

To show your IP address, use the following command:

ip addr show wlan0

You should now be connected to the internet. You can test it using ping.

Prepare the disk

Check the name of the disk:

fdisk -l

Assuming the name of the disk is /dev/sda, access the fdisk tool to modify the partitions on that disk:

fdisk /dev/sda

While inside fdisk, delete all the existing partitions. Type p to see all the current partitions, each of which should have a number. To delete a partition, type d, press Enter, and specify the partition number. Repeat until there are no partitions left.

Next, we will create three partitions: a boot partition called dev/sda1, a swap partition called /dev/sda2, and a home partition called /dev/sda3.

To create each partition, type n, then assign a partition number (1, 2 and 3 respectively), and finally a number of sectors, which will define the partition size. For the boot partition, assign enough sectors for a size of around 1 GB. For the swap partition, assign enough sectors for a size of about twice your RAM. For the home partition, assign the rest of the sectors on the disk.

Then, type a and select the partition 1. This will make the first partition bootable.

Type p to see the final state of the partitions we just created. They should be called /dev/sda1, dev/sda2 and dev/sda3. Finally, type w, which will write the changes and exit the fdisk tool.

Next, format the dev/sda1 boot partition and the /dev/sda3 home partition using the ext4 filesystem:

mkfs.ext4 /dev/sda1
mkfs.ext4 /dev/sda3

Now, format and activate the dev/sda2 swap partition:

mkswap /dev/sda2
swapon /dev/sda2

Mount the dev/sda1 boot partition in the /mnt directory:

mount /dev/sda1 /mnt

Mount the dev/sda3 boot partition in the mnt/home directory (create it first):

mkdir /mnt/home
mount /dev/sda3 /mnt/home

Install the Arch Linux base system

Download and install the Arch Linux base system on /mnt.

pacstrap /mnt base

This may take a while depending on your Internet speed.

Generate an fstab file:

genfstab -U /mnt >> /mnt/etc/fstab

Finally, change root into the new system:

arch-chroot /mnt

Configure the new system

To set the Time Zone, first find your region and city in /usr/share/zoneinfo/. Assuming that your region/city is /America/Detroit, run the following command:

ln -sf /usr/share/zoneinfo/America/Detroit /etc/localtime

Set the hardware clock:

hwclock --systohc

To configure the locale, edit the file /etc/locale.gen and uncomment the line en_US.UTF-8 UTF-8. (Install vim using pacman -S vim if needed.)

Then, generate the new locales by typing:

locale-gen

Finally, create the /etc/locale.conf with the following line:

LANG=en_US.UTF-8

Edit the /etc/hostname file with your desired hostname, for example:

mycomputer

Also edit the /etc/hosts file using the same hostname, as:

127.0.0.1   localhost
::1         localhost
127.0.1.1   mycomputer.localdomain  mycomputer

Create the root password by issuing the following command and then typing your desired password:

passwd

Create an initial ramdisk:

mkinitcpio -p linux

Finally, install the GRUB boot loader:

pacman -S grub
grub-install /dev/sda
grub-mkconfig -o /boot/grub/grub.cfg

And that’s it! A minimal Arch Linux system has been installed in your hard drive. Reboot your computer to access it:

exit
reboot
Installing Arch Linux