Surrogate.tv Guide: How to Create a Custom Game

by SurrogateTV in Circuits > Raspberry Pi

753 Views, 1 Favorites, 0 Comments

Surrogate.tv Guide: How to Create a Custom Game

custom_game_guide_cover.png

In this tutorial I show how to setup a remote SSH connection to the Raspberry Pi through Visual Studio Code. I will also guide you on how to create, run and modify your first custom game on Surrogate.tv.

Supplies

  • Raspberry Pi single board computer, for example model 3B+, 3A, or 4B
  • A 16GB+ micro SD card
  • An SD card reader, either built in to your computer or as an adapter.
  • An official Raspberry Pi Camera or some USB camera (UVC compliant)

Before You Start

How to CREATE YOUR OWN GAME on Surrogate.tv!

Before you start, you should have our Raspberry Pi image installed on your Raspberry Pi, as shown in the video tutorial above. There is also a written tutorial on our documentation website about the installation process.

Install VSCode & Remote - SSH

01_remote_plugin.png

To get started, go to the Visual Studio Code website to download and install the program. We will use it for creating and editing the game code later. You could use any other code editor, but we chose VSCode as it has a really convenient official extension for remotely modifying files directly on the Raspberry Pi.

After the installation has finished, open VSCode and then click on the Extensions button on the left sidebar as shown in the picture to open extensions menu. Search for “Remote SSH” and install the extension shown in the image.

Now you should have a green arrow symbol on the bottom left corner of your VSCode window.

Enable Raspberry Pi's SSH Connection

Next you should enable ssh connection to your Raspberry Pi. This can be done by connecting your Pi to a monitor and a keyboard and then following the instructions on the documentation page:

  1. First log in with the default username `pi` and password `creator1337`
  2. Change the default password to something you’ll remember by using the command `sudo passwd pi` and then typing a new password.
  3. Use `sudo raspi-config` to open the Pi's utility tool. Inside the utility tool select Interfaces -> SSH -> enable to enable the ssh connection.

After enabling the ssh connection, you can use the command `hostname -I` to show your IP address. Alternatively the same IP address can be found from your games dashboard by clicking the controller's name as described in our written documentation. Copy or write down this address, as you will use it in the next step to connect to your Pi.

Connect to Your Raspberry Pi

02_connect.png
03_add_new_host.png
04_host_added.png
05_open_folder.png
06_open_surrortg.png

After you have your VSCode and Raspberry Pi with our image ready, create a game as shown in our getting started video (in Step 1). You can also follow the written game creation tutorial on our documentation page.

Then go to the bottom left corner of VSCode, and click the green arrow symbol which says “Open a remote window” (shown in the pictures above). Then click “Connect to Host...”, "Add New SSH Host..." and type `pi@[IP_ADDRESS_OF_YOUR_PI]`, replacing [IP_ADDRESS_OF_YOUR_PI] with your Pi's IP address that you wrote down in the previous step. Press Enter, then select the save location and click "Connect". Then click "Continue" and enter your Pi's password, which you just selected in the previous step. The first connect can take a while as VSCode installs the necessary packages to your Pi, but soon you should see the Pi's IP address in the green arrow symbol on the bottom right corner of your VSCode.

Then on the left, open the Explorer panel and click "Open folder", select the folder named "surrortg-sdk", click "Ok" and enter your Pi's password again.

Now you should have the VSCode connected to your Raspberry Pi and you can edit the files and run commands remotely from your computer. When you connect to your Pi later, its IP address should show up next to "Add New SSH Host" in the connection menu.

Create a New Game

Now you can open the "games" folder and create a new folder called “mygame”. Inside that create a “game.py” file. This is the file where you can start writing your game logic.

Let’s start by copying a sample game code, which just logs the incoming user inputs from WASD keys. Copy the code below and paste it to the new "game.py" file. Press Ctrl+S to save the file.

import logging
from surrortg import Game
from surrortg.inputs import Joystick


class MyJoystick(Joystick):
    async def handle_coordinates(self, x, y, seat=0):
        logging.info(f"\tx:{x}, y:{y}")

    async def reset(self, seat=0):
        logging.info("reset")

class MyGame(Game):
    async def on_init(self):
        self.io.register_inputs({"joystick_main": MyJoystick()})


MyGame().run()

To make the Raspberry Pi use this code instead of the current one we must open a "controller-rpi.service" file from the "scripts" folder and change the `Environment=GAME_MODULE=` to `games.mygame.game`.
Press Ctrl+S to save this file. As a result the contents of the file should look like the one shown below.

[Unit]
Description=Surrogate robot control software
After=network.target pigpiod.service
Wants=pigpiod.service
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=10
Environment=GAME_MODULE=games.mygame.game
WorkingDirectory=/home/pi/surrortg-sdk
ExecStart=/usr/bin/python3 -m $GAME_MODULE
[Install]
WantedBy=multi-user.target

Then from VSCode's menu bar click "Terminal" > "New Terminal" and in the new terminal run the command `sudo scripts/setup-systemd.sh` to set the new game file we just created to be executed by the controller.

See the Logs From the Game

Now you can run `sudo journalctl -fu controller` to see the current logs from the game. When you play your game through Surrogate.tv and press WASD keys, you should see some logs in the terminal.

You can exit the logs by pressing Ctrl+X.

Finished!

This was just a quick tutorial on how to get started with the game creation.

In the future tutorials we are going to go more in depth on the game logic, and learn how to connect some hardware to the Raspberry Pi.