Your first game in Gama
This tutorial builds CrateCatch, a small game where crates fall from the sky and you catch them with a paddle. It takes about fifteen minutes and shows the parts of Gama a game uses most: trees of parts and UI, events, a class, a module and physics. The finished game is in Examples/CrateCatch.
You need a build of Gamma with the window (any preset but headless); see Getting started. The commands below use Gamma for the built executable, such as build/debug/Gamma.
1. Make the project
Gamma new CrateCatch
Gamma play CrateCatchGamma new writes main.gma, an assets folder and a .luaurc that turns on strict type checking. Gamma play opens a window that says hello. Open the folder in VS Code with the Gama extension for highlighting, completion and errors as you type.
2. A floor and a camera
Replace main.gma with:
Scene.PhysicsEnabled = true
let camera = Scene.CurrentCamera :: Camera
camera.Transform = Transform.lookAt(<0, 14, 34>, <0, 10, 0>)
Part {
Name = "Floor",
Size = <40, 1, 10>,
Position = <0, -0.5, 0>,
Anchored = true,
Color = $3a3f4b,
Parent = Scene,
}Scene.PhysicsEnabled = trueturns on the physics simulation, so parts that are not anchored fall.<0, 14, 34>is aVector3and$3a3f4ba colour.Part { ... }is a tree: it makes a part and sets its properties.Anchored = truekeeps the floor still.Scene.CurrentCamera :: Cameratells the type checker the camera is there, as it is in every new Scene.
3. A paddle you can move
Add the paddle under the floor, and move it every frame while an arrow key is held:
let paddle = Part {
Name = "Paddle",
Size = <6, 1, 4>,
Position = <0, 2, 0>,
Anchored = true,
Color = $6d8dff,
Parent = Scene,
}
const speed = 20
on Run.Heartbeat(deltaTime)
let direction = 0
if Input:IsKeyDown(Enum.KeyCode.Left) then
direction -= 1
end
if Input:IsKeyDown(Enum.KeyCode.Right) then
direction += 1
end
let x = math.clamp(paddle.Position.X + direction * speed * deltaTime, -17, 17)
paddle.Position = <x, 2, 0>
endlet declares a variable and const one that never changes. on Run.Heartbeat(deltaTime) ... end connects a function to the event that fires every frame, with the seconds since the last one. The paddle is anchored, so physics leaves it where the script puts it, but falling crates still land on it.
4. Crates, in a module of their own
Make src/Crate.gma:
let random = Random.new()
export class Crate
part: Part
caught = false
fn new(paddle: BasePart, onCaught: () -> ())
self.part = Part {
Size = <2, 2, 2>,
Position = <random:NextNumber(-10, 10), 30, 0>,
Color = Color.fromHSV(random:NextNumber(), 0.6, 0.9),
Parent = Scene,
}
on self.part.Touched(other)
if other == paddle and not self.caught then
self.caught = true
onCaught()
self.part:Destroy()
end
end
end
end- A class has fields, with a type, a value or both, and methods.
fn newis the constructor, called asCrate.new(...), andselfis the new crate. - The crate is not anchored, so it falls once it is in the Scene.
Touchedfires when the simulation finds the crate touching another part, and passes that part.caughtmakes sure a crate counts once, even if it bounces.exportmakesCratepart of what the module gives to the files that require it.
5. Drop crates and keep score
Back in main.gma, require the module at the top:
let Crates = require("./src/Crate")and add a score label and a timer at the bottom:
let score = 0
let label = Text { Text = "Caught: 0", FontSize = 24, Color = $ffffff }
Surface { Target = "Window", Padding = 20, Parent = Scene, label }
every 1.5 now do
Crates.Crate.new(paddle, fn()
score += 1
label.Text = `Caught: {score}`
end)
endSurface draws UI on the window, and Text is a node in it. every 1.5 now do ... end runs its body straight away and then every 1.5 seconds, without holding up the rest of the file. fn() ... end is a function passed to the crate, which it calls when it is caught. `Caught: {score}` is an interpolated string.
Run it with Gamma play CrateCatch, and catch what you can.
6. Check it
Gamma check CrateCatchtype checks every .gma file in the project, as the editor does, and exits with 1 on errors, so it can run in CI. Try writing paddle.Positon and see what it says.
Where to go next
- gama.md has the whole language, with a cheat sheet at the end.
- projects.md covers project folders, physics, settings and exporting a game as one executable.
- Make the paddle a
class, give crates a chance to be gold and worth more with amatch, or weld two parts together with aWeldConstraintto make a wider paddle.