Before you start, you should have our Raspberry Pi image installed on your Raspberry Pi, as shown in our setup guide. You can also read about the installation process from our documentation website.
If you prefer a video guide, the contents of this guide can be found in the video guide in YouTube.
Step 2: Install VSCode & Remote - SSHTo 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.
Step 3: Enable Raspberry Pi's SSH ConnectionNext 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:
- First log in with the default username
pi
and passwordcreator1337
- Change the default password to something you’ll remember by using the command
sudo passwd pi
and then typing a new password. - Use
sudo raspi-config
to open the Pi's utility tool. Inside the utility tool selectInterfaces -> 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.
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.
Step 5: Create a New GameNow 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.
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.
Step 7: 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.
Comments
Please log in or sign up to comment.