Step 1¶
This chapter will guide you on setting up the base of your game so that it can start running.
The Program's Base¶
A basic Shards program consists of:
-
Writing Shards and Wires
-
Scheduling Wires onto a Mesh
To learn more about Wires, Meshes, and the overall flow of Shards, check out the primer here.
A basic Shards program looks like this:
1 2 3 4 5 6 |
|
For the program to run continuously, we will be using a looped Wire instead. This can be done by replacing defwire
with defloop
. You can then adjust the time interval between each loop by adding a second argument to (run)
.
Since most games run at 60 FPS (Frames per Second), we will be using (/ 1.0 60.0)
to get the time interval between each frame.
Note
(/ 1.0 60.0)
reads as "1 divided by 60".
1 2 3 4 5 6 |
|
defloop
is a macro used to define a Loop.
Game Loops¶
To make a game, you will need:
-
Code to draw the UI (such as your game menus, text boxes, and images)
-
Code to run the game's logic (such as calculating the score, determining when to end a round)
It might be quite disorderly if we did everything within a single defloop
.
To keep our code easily readable and organized, we split the UI and logic code into separate Loops running together on the Mesh.
We can achieve this by employing the Branch
shard and creating two separate loops for the game's logic and UI.
Branch
Branch
creates a mini-Mesh of sorts and schedules Wires to run on it.
1 2 3 4 5 6 7 8 9 |
|
The Setup Zone¶
Most games will require code to ready the program, such as by loading resources and setting up variables to use.
We can ready empty shards load-resources
and initialize-variables
to carry out these tasks. Place them in a Setup
shard within the game-loop
to ensure that they only run once.
Variables
Variables are containers that store values. You define a variable with a name, and assign a value to it. The value can then be retrieved by calling the variable with its assigned name.
Setup
The Setup
shard will only be executed once, even within a Looped Wire. This makes it ideal to hold code that is only used once to ready the game.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
defshards
allows multiple shards to be grouped together into a single shard. We will be placing shards that load resources intoload-resources
for example.- The shards to load resources and initialize variables are only run once in the game loop
The basic game loop is now ready!
In the next chapter, we will learn about drawing the User Interface and adding graphics.