Command Line
USAGE: Turbo <Command>
INIT
Initializes a new Turbo project in Rust
RUN
Runs a Rust Turbo project
build
Builds a Rust Turbo project
export
Exports a Rust Turbo project in web format
connect
Connects to a Turbo host
host
Runs a Turbo host
help
Print a list of commands
Randomness
Simple Randomness
// get a random number
let n = rand();
// get a random// get a random number between 1-100
let n = 1 + (rand() % 100);
// simulate a “coin flip” by checking if a number is even
let is_even = rand() % 2 == 0;
// simulate a “coin flip” by checking for odd number
let is_odd = rand() % 2 != 0;
Simple Randomness
// Get a number between 1 and 100
let n = 1 + (rand() % 100);
match n {
// Any number 1-10 (10% chance)
1..=10 => { ... }
// Any number 11-50 (40% chance)
11..=50 => { ... }
// Any number 11-50 (50% chance)
_ => { ... }
}
Tick
//get the current game tick
let t = tick();
Shortcuts
USAGE: Turbo <Command>
cmd+r
Reset your game state on mac/linux
ctrl+r
Reset your game state on windows
cmd+2
Inspect your sprite sheet on mac/linux
ctrl+2
Inspect your sprite sheet on windows
cmd+1
Take a screenshot on mac/linux
ctrl+1
Take a screenshot on windows
Debugging
// log a static message
log!("hello!");
// log game state
let state = GameState::load(); log!("My game state = {:?}", state);
Shapes
// Draw 8px diameter magenta circle
circ!(
d = 16,
x = 120,
y = 64,
color = 0x000000ff
);
// Draw a 20x40px blue rectangle
rect!(
w = 20,
h = 40,
x = 70,
y = 70,
color = 0x0000ffff
);
Text
// basic text
text!("Hello, world!");
// customized text with specified position, color, and font size
text!(
"Greetings, earthlings >:3",
x = 30,
y = 40,
color = 0x00ff00ff,
font = Font::S
);
Game State Initialization
// Define game state with turbo::init!
turbo::init! {
// Define the GameState struct.
struct GameState {
screen: enum Screen {
Title,
Level,
},
x_position: i32,
y_position: i32,
} = {
// Set the initial value.
Self {
screen: Screen::Title,
x_position: 30,
y_position: 40,
}
}
}
Game Loop
// Implement the game loop using the turbo::go! macro
turbo::go! {
// 1. Load State
// This hydrates state from the previous loop
// The state's initial value will load on the first loop
let mut state = GameState::load();
// 2. Update State
// Your game's logic goes here. Mutate state as-needed...
// The final thing to do each loop is save your game state
state.save();
}
Camera
// set the camera position
set_cam!(
x=50,
y=100
)
// move the camera position
move_cam!(
x=1,
y=1
);
Gamepad Controls
Getting Gamepads
// Get player 1 gamepad
let p1_gamepad = gamepad(0);
// Get player 2 gamepad
let p2_gamepad = gamepad(1);
Checking gamepad button state
let gp = gamepad(0);
if gp.up.just_pressed() {
// the button is JustPressed
}
if gp.down.pressed() {
// the button is JustPressed or Pressed
}
if gp.a.just_released() {
// the button is JustReleased
}
if gp.start.released() {
// the button is JustReleased or Released
}
Gamepad
Gamepad
up
W / ↑
down
A / ↓
left
S / ←
right
D / →
start
Spacebar
Gamepad
Gamepad
A
Z
B
X
X
C
y
V
select
Enter
Sprites
// a sprite named "goblin" with 180 degrees of rotation
sprite!(
"goblin",
x = 120,
y = 50,
rotate = 180
);
Canvas Size
// Get the canvas size
let [canvas_width, canvas_height] = canvas_size!();
Solana
Enabling Solana Features
// Enable the solana feature in Cargo.toml
turbo = {
version = "*",
package = "turbo-genesis-sdk",
features = ["solana"]
}
// Add patches for jobserver and cc
[patch.crates-io] jobserver = {
git = "https://github.com/jozanza/jobserver-rs.git",
branch = "main"
}
cc = {
git = "https://github.com/jozanza/cc-rs.git",
branch = "1.0.94"
}
Configuring RPC URLs
turbo::cfg!{r#"
[solana]
http-rpc-url = "http://localhost:8899"
ws-rpc-url = "ws://localhost:8900"
"#}
Set the Signer
TURBO_SOL_SIGNER=<insert base58 private key> turbo run -w path/to/my/project