← Previous
Foundation Concepts

Fork the Course Repository

Let's set up your own copy of the course code! Forking allows you to save your progress and access reference code at any time.

Why Fork?

Personal Progress: Commit and save your own implementations, even create your own game variants.

Reference Access: Quickly return to any lesson’s code using saved snapshots in the original repository.

Setting Up Your Fork

  1. Create a GitHub account if you don't have one:
    • Visit github.com and sign up
    • A GitHub account is essential for any developer
  2. Fork the repository:
  3. Open a terminal here in the IDE:
    • Drag up from the bottom edge
    • Or use menu → Terminal → New Terminal

🤯 Heads Up! Always use Ctrl+Shift+V to paste in the terminal. This is different from regular copy-paste in the IDE.

  1. Clone your fork (replace <YOUR_GITHUB_USERNAME> with your actual GitHub username):
    git clone https://github.com/<YOUR_GITHUB_USERNAME>/snake-tutorial.git
    
  2. Set up upstream and fetch lesson branches:
    cd ~/sandbox/snake-tutorial
    git remote add upstream https://github.com/GNAT-Academic-Program/snake-tutorial.git
    git fetch upstream
    git checkout -b lesson-02-work upstream/lesson-02-start
    git push -u origin lesson-02-work
    

Expected result: You now have your own fork with access to all lesson snapshots and can track your progress!

Create a Game Engine Library in Ada

Let's create our first Ada library! We'll build a game engine called "noki" that will power our Snake game.

  1. Create the library using Alire:
    alr -n init --lib noki
    
  2. Enter the noki directory:
    cd noki
    
  3. Explore the generated structure:
    ls -la
    

What Alire Created

  • alire.toml: Project configuration and dependencies
  • src/: Source code directory
  • src/noki.ads: Library specification (public interface)
  1. Build the library to verify everything works:
    alr build
    

🤯 Heads Up! The -n flag tells Alire to skip interactive prompts and use defaults.

Expected result: Alire creates a new library project structure with all necessary files, builds successfully, and you're now in the noki directory ready for development.

If the build fails: Something went wrong with the setup. Start over from step 1 of lesson 2 to ensure everything is configured correctly.

Expose a Custom Log Method in Your Noki Library

Let's create our first Ada procedure! We'll add a Log method to our noki library for printing messages.

  1. Open the specification file noki.ads:
    • Use the file explorer on the left side of the IDE
    • Navigate to snake-tutorialnokisrcnoki.ads and click to open it
    • The extension .ads means Ada Specification
    • This file is the public interface of your compilation unit
    • It's like .h/.hpp in C/C++, but Ada uses semantic modules (called packages) instead of text pre-processing
  2. Add the procedure declaration right after the line package Noki is:
    procedure Log (S : String);
    
  3. Create the implementation file noki.adb:
    • .adb means Ada Body
    • This is similar to a .c/.cpp file in C/C++
    • Using the IDE menu create a new file snake-tutorialnokisrcnoki.adb
  4. Add the package body structure to noki.adb:
    package body Noki is
    
    end Noki;
    
  5. Implement the Log procedure inside the package body:
    procedure Log (S : String) is
    begin
       null;
    end Log;
    

📋 Code Review - Complete noki.adb

Your complete noki.adb should look like this:

package body Noki is

   procedure Log (S : String) is
   begin
      null;
   end Log;

end Noki;

Build the library:

alr build

If something goes wrong: If you don't see Build finished successfully in ..., re-check the instructions carefully and start again from step 1.

Expected result: Successful build with no compilation errors.

Implement Your Custom Log Procedure

Let's make our Log procedure actually print to the terminal! We'll use Ada's standard I/O package for portable text output.

  1. Open the file src/noki.adb
  2. Import the I/O package at the very top of the file:
    with Ada.Text_IO;
    
  3. Replace the null; statement inside the Log procedure with:
    Ada.Text_IO.Put (S);
    Ada.Text_IO.New_Line;
    

📋 Code Review - Complete noki.adb

Your complete noki.adb should look like this:

with Ada.Text_IO;

package body Noki is

   procedure Log (S : String) is
   begin
      Ada.Text_IO.Put (S);
      Ada.Text_IO.New_Line;
   end Log;

end Noki;

Build the project:

alr build

If something goes wrong: Check that your syntax matches exactly, including semicolons and capitalization.

Expected result: Successful build - your Log procedure now prints text to the terminal!

Use Your Custom Log Procedure

Let's test our Log procedure! We'll create an executable program that uses our noki library.

  1. Verify that Log is exported by running this in the terminal:
    nm lib/libNoki.a
    

    Expected result: You should see something like:
    noki.o:
    ...
    0000000000000000 T noki__log
    
  2. Create a binary project to use the noki library:
    • Make sure you're in the noki folder:
      cd ~/sandbox/snake-tutorial/noki
      
    • Create a new executable project named snake_game:
      alr -n init --bin snake_game
      
  3. Make your executable depend on your library:
    • Move into the new project:
      cd snake_game
      
    • Open the snake-tutorialnokisnake_gamealire.toml file and add this at the end of the file:
      [[depends-on]]
      noki = "*"
      
      [[pins]]
      noki = { path = ".." }
      
      [environment]
      ADAFLAGS.append = "-gnat2022"
      
  4. Call Noki.Log in your program:
    • Open the snake-tutorialnokisnake_gamesrcsnake_game.adb
    • At the top, add the package from your library:
      with Noki;
      
    • Replace null; with:
      Noki.Log ("Welcome to my Snake Game!");
      

📋 Code Review - Complete snake_game.adb

Your complete snake_game.adb should look like this:

with Noki;

procedure Snake_Game is
begin
   Noki.Log ("Welcome to my Snake Game!");
end Snake_Game;

Build and run:

alr build
bin/snake_game

If something goes wrong: If you don't see Build finished successfully in ..., re-check the instructions carefully and start again from step 1.

Expected result: On the terminal you should see: Welcome to my Snake Game!

🤯 Heads Up! This tells Alire to use the local noki library from the parent folder.

Save Your Progress

🎯 End of Lesson 2 — Save Your Progress

To keep your work safe and accessible, push it to your fork.

Make sure you're at the root of the repo:

cd ~/sandbox/snake-tutorial

Make sure you're on your work branch:

git status
  • 🚨 If you're NOT on branch lesson-02-work, run:
    git checkout -b lesson-02-work
    

Stage and commit your progress:

git add .
git commit -m "Lesson 2 progress"

Push to your fork:

git push origin lesson-02-work

Expected result: Your fork now has a lesson-2-work branch with your code so far.

Level up your Server Side game — Join 15,000 engineers who receive insightful learning materials straight to their inbox

← Previous
Foundation Concepts