---
title: Your first game
description: Build your first Tide game with components, systems, input and
  drawing, then host a multiplayer match and invite another player.
source_url: https://tide-engine.dev/guide/first-game
source_file: docs/guide/first-game.md
source_revision: 36904d8e07dd044bd665de823e6b8022ed4f4248
source_worktree: modified
documentation_sha256: ea38a553efadfaf8a016c33e947825b8c4d3d2005abcf32eb4f4a64094909e97
documentation_scope: Website source checkout; not matched to an installed Tide
  release. Check tide version and the project toolchain.
---

<a id="your-first-game"></a>

# Your first game

This page builds a small game: a ball bouncing around, and a player for everyone who joins, which they steer with the keyboard. It takes a few minutes, and shows most of what a Tide game is made of.

You'll need `tide` [installed](https://tide-engine.dev/guide/install.md). [Set up your editor](https://tide-engine.dev/tools/editors.md)
for highlighting, completion and diagnostics before writing your first file.
Working with an AI coding agent? Use the [Tide overview and starter prompt](https://tide-engine.dev/guide/ai.md)
to give it the language context and validation commands.

<a id="a-folder-of-files"></a>

## A folder of files

A game is a folder of `.tide` files: every `.tide` file in it and its subfolders, but for hidden ones, whose names start with a dot. Every declaration is visible from every file, with no imports. Make a new folder, and a file in it called `game.tide`.

<a id="data-components"></a>

## Data: components

Entities are made of **components**, plain data with fields. Fields can have defaults, and start at zero otherwise.

```csharp
component Body
{
    float2 position;
    float2 velocity;
    float radius = 20;
}

component Ball { }
```

`Ball` has no fields: it marks which bodies are balls.

<a id="where-it-starts-the-main-scene"></a>

## Where it starts: the Main scene

The game starts in the scene called `Main`. When it loads, it gets the built-in `Spawned` event, and a handler of that event sets it up:

```csharp
scene Main { }

event(Spawned) Setup(with Main)
{
    Spawn(Body { velocity = float2(160, 120) }, Ball);
}
```

`Spawn` makes an entity from a list of components. `Body { velocity = ... }` sets one field and leaves the others at their defaults, and a bare `Ball` has all of them at their defaults.

<a id="behavior-systems"></a>

## Behavior: systems

A **system** runs every tick, once for every entity that has the components it takes. Parameters are read-only, unless they're `mut`. `with Ball` only picks entities that have a `Ball`, without reading it.

```csharp
system Move(mut Body body, Time time)
{
    body.position += body.velocity * time.dt;
}

system Bounce(mut Body body, with Ball)
{
    if (Math.Abs(body.position.x) > 160) body.velocity.x = -body.velocity.x;
    if (Math.Abs(body.position.y) > 160) body.velocity.y = -body.velocity.y;
}
```

`Time` is a built-in singleton: there's one per world, and `time.dt` is the length of a tick in seconds. Systems run in the order they're written, and always in the same order on every machine.

<a id="drawing-views"></a>

## Drawing: views

A **view** looks like a system, but it runs once per frame instead of once per tick, and it can only read the match. `Draw` functions only work in views.

```csharp
view DrawBalls(Body body, with Ball)
{
    Draw.Circle(body.position, body.radius, Color.red);
}
```

The camera starts at the origin, one world unit per pixel, with `y` up.

<a id="run-it"></a>

## Run it

In the folder:

```sh
tide run --stats
```

`tide` compiles the game and opens a window with the ball bouncing. It keeps its work in a hidden `.tide` folder, which you can delete any time. Whenever you save, `tide` rebuilds the game and the window picks up the new code (see [hot reload](https://tide-engine.dev/tools/cli.md#hot-reload)).

This is the game so far, built for the web and running in the page:

**The game so far** ([Run example](https://tide-engine.dev/examples/first-ball/?stats))

```tide
component Body
{
    float2 position;
    float2 velocity;
    float radius = 20;
}

component Ball { }

scene Main { }

event(Spawned) Setup(with Main)
{
    Spawn(Body { velocity = float2(160, 120) }, Ball);
}

system Move(mut Body body, Time time)
{
    body.position += body.velocity * time.dt;
}

system Bounce(mut Body body, with Ball)
{
    if (Math.Abs(body.position.x) > 160) body.velocity.x = -body.velocity.x;
    if (Math.Abs(body.position.y) > 160) body.velocity.y = -body.velocity.y;
}

view DrawBalls(Body body, with Ball)
{
    Draw.Circle(body.position, body.radius, Color.red);
}
```



The next part adds players.

<a id="players-and-input"></a>

## Players and input

Each player's **input** is what the network sends of them, once per tick. It's declared once per game, and its `Sample` method fills it on the player's own machine, from this machine's `Devices`:

```csharp
input Controls
{
    float2 move;

    Sample(Devices devices)
    {
        var keys = devices.keyboard;
        if (keys.d.pressed) move.x += 1;
        if (keys.a.pressed) move.x -= 1;
        if (keys.w.pressed) move.y += 1;
        if (keys.s.pressed) move.y -= 1;
    }
}
```

Keys are named by their position on a US keyboard, so `WASD` is in the same place on every layout.

When a player joins, the world gets the built-in `PlayerJoined` event. This handler gives them a body, with an `Owner`, a built-in component that ties an entity to a player:

```csharp
event(PlayerJoined joined) AddPlayer()
{
    Spawn(Body { radius = 16 }, Owner { player = joined.player });
}
```

A system that takes the input gets the input of the player who owns the entity it's running for:

```csharp
system Steer(Controls input, mut Body body, with Owner)
{
    body.velocity = Math.Normalize(input.move) * 250;
}
```

Put `Steer` before `Move`, so bodies move the tick they're steered. Last, draw the players. Views can read `Session`, where this machine stands, so yours can be yellow and the others gray:

```csharp
view DrawPlayers(Body body, Owner owner, Session session)
{
    var color = owner.player == session.player ? Color.yellow : Color.gray;
    Draw.Circle(body.position, body.radius, color);
}
```

A view that's only for your own player, like a camera that follows them, picks them with a filter instead: `with ownership` (see [Your own entities](https://tide-engine.dev/game/views-and-the-match.md#your-own-entities)).

Save, then type `r` and press Enter in `tide`'s terminal, and steer with `WASD`. The game carried over to the new code when you saved, but your player had joined before `AddPlayer` existed, so they had no body yet. Starting over makes them join again.

<a id="play-together"></a>

## Play together

Single-player was already a match, on a server this machine ran for itself. So the same game plays with others. In one terminal:

```sh
tide run --host --stats
```

And in another, on this machine or another one on the network:

```sh
tide run --connect localhost --stats
```

Use the host's address instead of `localhost` from another machine, like `tide run --connect 192.168.1.5 --stats`. The host takes players on UDP port 7777; `--host 8000` picks another. Each player steers their own yellow body, with no input delay.

Where nobody is there to steer (a test, a build server, an agent), `tide run --players 2 --frames 300 --capture match.png --stats` plays both at once with no window, and writes a picture from each machine: `match-1.png` and `match-2.png` (see [Several players](https://tide-engine.dev/tools/cli.md#several-players)).

No input delay means a machine doesn't wait to hear what the other players did: it guesses, by repeating their last input for a moment, and puts the match right when the server's answer arrives. That moves their body in one frame, each time they start or stop, by up to what they move in 0.15 seconds (see [What other machines guess](https://tide-engine.dev/game/input.md#what-other-machines-guess)). Mark the position, and views draw the correction over a few frames instead:

```csharp
component Body
{
    [Position] float2 position;
    float2 velocity;
    float radius = 20;
}
```

Only what's drawn changes: the match is put right at once, the same on every machine (see [correction smoothing](https://tide-engine.dev/game/views.md#provisional-correction-smoothing)).

Here are two machines in one match, each running [the whole game](https://tide-engine.dev/guide/first-game.md#the-whole-game): the first hosts it, and the second joined its room, as `tide run --join` does with a room's code (see [Matches](https://tide-engine.dev/multiplayer/matches.md#rooms)). Click a frame so it has the keyboard, and steer with `WASD`: its player is yellow there, and gray in the other frame.

**Two players, in one match** ([Run example](https://tide-engine.dev/examples/first-game/?stats))


<a id="build-it-for-the-web"></a>

## Build it for the web

```sh
tide run --web --stats
```

This builds the game as a web page, which draws with WebGPU, and opens it in your browser. `tide` serves the page on your machine and keeps running until you stop it with Ctrl+C. Saving a file reloads the game in the page. For a page to put online:

```sh
tide build --release --web
```

It goes in `.build/web/`, a folder to put online as it is: `index.html`, which shows the game downloading, and the game's files beside it, which browsers keep between visits. To try that page on your own machine, serve the folder (`npx serve .build/web`): a page opened from disk can't download its files. `tide build --release --web --single-file` makes one `.html` file with the game inside instead, which opens straight from disk. Web and desktop players play together in rooms, which they join by a code (see [Matches](https://tide-engine.dev/multiplayer/matches.md#rooms)).

<a id="the-whole-game"></a>

## The whole game

Click the game so it has the keyboard, and steer with `WASD`.

**game.tide** ([Run example](https://tide-engine.dev/examples/first-game/?stats))

```tide
component Body
{
    [Position] float2 position;
    float2 velocity;
    float radius = 20;
}

component Ball { }

input Controls
{
    float2 move;

    Sample(Devices devices)
    {
        var keys = devices.keyboard;
        if (keys.d.pressed) move.x += 1;
        if (keys.a.pressed) move.x -= 1;
        if (keys.w.pressed) move.y += 1;
        if (keys.s.pressed) move.y -= 1;
    }
}

scene Main { }

event(Spawned) Setup(with Main)
{
    Spawn(Body { velocity = float2(160, 120) }, Ball);
}

event(PlayerJoined joined) AddPlayer()
{
    Spawn(Body { radius = 16 }, Owner { player = joined.player });
}

system Steer(Controls input, mut Body body, with Owner)
{
    body.velocity = Math.Normalize(input.move) * 250;
}

system Move(mut Body body, Time time)
{
    body.position += body.velocity * time.dt;
}

system Bounce(mut Body body, with Ball)
{
    if (Math.Abs(body.position.x) > 160) body.velocity.x = -body.velocity.x;
    if (Math.Abs(body.position.y) > 160) body.velocity.y = -body.velocity.y;
}

view DrawBalls(Body body, with Ball)
{
    Draw.Circle(body.position, body.radius, Color.red);
}

view DrawPlayers(Body body, Owner owner, Session session)
{
    var color = owner.player == session.player ? Color.yellow : Color.gray;
    Draw.Circle(body.position, body.radius, color);
}
```



<a id="next"></a>

## Next

- `tide schedule` shows which of these systems could run at the same time, and why the others wait. See [the schedule](https://tide-engine.dev/game/schedule.md).
- Read what a game is made of, a topic at a time, starting with [components and entities](https://tide-engine.dev/game/entities.md). The language itself starts with [the basics](https://tide-engine.dev/language/basics.md).
- Browse the [samples](https://tide-engine.dev/guide/samples.md) for complete games to play in your browser, with links to their source.
