Back

Activity 2.7: Raylib Lab 8 — Composing a Scene

divider

What You're Building

An aquarium you fill by clicking. Click on the left half of the tank and a fish appears where you clicked; click on the right half and you get a crab. The deeper you click, the bigger the creature. Every fish, crab, and strand of seaweed is drawn by a method you call with parameters.

A blue aquarium with sand along the bottom and three strands of green seaweed. Four orange fish of different sizes are on the left half and four red crabs on the right; the ones lower in the tank are bigger. White text in the corner reads Creatures: 8 of 20.

The finished aquarium after eight clicks

divider

Activity 2.7

Raylib Lab 8 — Composing a Scene

Key Concepts

Methods as Building Blocks

Call Order Is Paint Order

The Mouse, One Frame at a Time

What Does Not Count as Yours

Activity 2.4's Skyline Never Listened

DrawBuilding drew a different building for every set of arguments, but the arguments were typed into the program. Run it twice and you get the same city twice. Nothing you did while it ran made any difference.

Today the person using the program chooses the arguments, by clicking. And instead of one method drawing one kind of thing, several methods each draw their own part of a scene, and the main program arranges them.

One Method Per Thing

Here is a park. DrawTree is Activity 2.4's tree, unchanged. DrawCloud is new: three overlapping gray circles, all worked out from one size.

Demo
void DrawCloud(int x, int y, int size)
{
Raylib.DrawCircle(x - size, y, size * 2 / 3, Color.LightGray);
Raylib.DrawCircle(x, y - size / 3, size, Color.LightGray);
Raylib.DrawCircle(x + size, y, size * 2 / 3, Color.LightGray);
}

One Method Per Thing

The main program's whole job is these five calls. Picture the scene before it appears.

Demo
DrawCloud(180, 90, 50);
DrawCloud(560, 60, 35);
DrawTree(160, 260, 80);
DrawTree(400, 200, 140);
DrawTree(640, 280, 60);
Two gray clouds in a black sky above three trees of different sizes standing on green ground

One Method Per Thing

Each method knows how to draw one thing, and nothing about where it goes. The main program decides where, and knows nothing about how a cloud is built.

That split is abstraction from Activity 2.5 doing real work. To add a cloud, you write one call and never open DrawCloud. To make every cloud fluffier, you change DrawCloud once and never touch the calls.

Call Order Is Paint Order

Raylib paints each shape on top of whatever is already there. The clouds were called first, so anything called after them could cover them. The ground was drawn before the trees, so the trunks stand on it rather than behind it.

Nothing is in front or behind on purpose. It is layered by the order of the calls, and moving one call up or down the list moves that shape forward or back. You will try it in Task 1.

The Mouse Works Like a Key

Raylib Lab 3 checked a key once per frame: IsKeyPressed was true on the one frame the key went down.

Demo
if (Raylib.IsKeyPressed(KeyboardKey.Space))
{
// ... runs on the one frame Space goes down
}

The Mouse Works Like a Key

The mouse button works exactly the same way, and two more methods say where the pointer is, in the same pixels as every Draw call.

Demo
if (Raylib.IsMouseButtonPressed(MouseButton.Left))
{
int x = Raylib.GetMouseX();
int y = Raylib.GetMouseY();
// ... do something at (x, y)
}

IsMouseButtonPressed(MouseButton.Left) is true on the one frame the button goes down. Hold it down, and every later frame is false until you let go and click again.

A Click Lasts One Frame

Every frame starts with ClearBackground, wiping the window, and then redraws everything — sixty times a second.

Predict: if a program draws a cloud at the pointer inside that if, and nowhere else, what do you see when you click?

A Click Lasts One Frame

Almost nothing. The cloud is drawn on the click's frame, and the very next frame wipes the window and does not draw it again, because the if is now false. It is on screen for a sixtieth of a second.

So a program that should keep what you click has to remember each click: store its x and y in arrays, count how many are stored, and redraw all of them every frame. The window remembers nothing; your arrays remember everything.

Some Libraries Call Your Code for You

In many other tools you would not check the mouse yourself. You would write a method with a special name, such as p5's mousePressed() or Scratch's “when this sprite clicked” block, and the system calls it whenever the click happens. That is an event handler.

The College Board says: “Event handlers are built-in abstractions in some languages and will therefore not be considered student-developed.” You fill in its body, but its name, its parameters, and when it runs were all decided by the library.

Raylib Makes You Ask, and That Does Not Count Either

Raylib has no event handlers. Your main program asks about the mouse every frame, inside the while loop.

But Activity 2.4 already said the main program is a main method, and the College Board excludes those too. So the code that notices the click does not count, whether a library calls it or your main loop runs it.

DrawFish counts. You wrote it, you named it, you chose its parameters, and you call it.

Input and a Method of Your Own Are Two Different Boxes

The Create task needs input and it needs a student-developed procedure. A mouse click covers the first box. It does nothing for the second.

Adding more click checks, a right-click, or a key that clears the screen adds more input handling, all of it in the main program. None of it is a method of your own. The click is how the user talks to the program; your method is what the program does with it.

Your Own Projects Need Both Boxes

In Build 1, and later in the Create task, you choose what to make. If you build in Raylib, a click or a key is how your program will take input.

And this is the requirement people fail by accident: a Raylib project where every loop and every decision sits in the main program, beside a method that only draws one shape. Know which part is yours before you start planning.

Today's Objectives

  • Composing a scene from several methods with parameters
  • Taking input from the mouse with IsMouseButtonPressed
  • Remembering clicks in arrays so they stay on screen
  • Explaining why input handling is not a student-developed procedure

Key Terms

Event handler
A method a library or system calls for you when something happens, such as a click.
Main method
The program's starting point; in your files, the top-level lines C# builds it from.
Frame
One pass through the drawing loop, clearing the window and drawing everything again.
Student-developed procedure
A procedure you wrote yourself; built-in methods, event handlers, and the main program do not count.

'F' → Fullscreen

divider

Build

Create a new Raylib program named 2-7-aquarium. You start with a fixed tank, then let the mouse fill it, and finally make where you click decide what appears and how big.

How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.

The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.


Task 1: Compose the Tank

  • Two methods draw everything. DrawFish takes a position, a size, and a color: a tail and a body drawn with Raylib.DrawEllipse, which takes a center, a half-width and a half-height, and a black eye. DrawSeaweed stacks short green pieces up from the sand, nudging every second piece to the right.
  • Write the code below and run it. Then add one more fish and one more strand of seaweed by adding two lines to the main program. Do not touch either method.
  • The yellow fish is drawn in front of the tall seaweed. Move its call (line 19) up, above the three seaweed calls, and run it. Write down what changed and why. Put it back.
2-7-aquarium
#:package Raylib-cs@8.0.0
using Raylib_cs;
Raylib.InitWindow(800, 450, "Aquarium");
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.DarkBlue);
Raylib.DrawRectangle(0, 400, 800, 50, Color.Beige);
DrawSeaweed(120, 160);
DrawSeaweed(560, 240);
DrawSeaweed(700, 100);
DrawFish(250, 150, 60, Color.Orange);
DrawFish(575, 300, 50, Color.Yellow);
DrawFish(620, 120, 90, Color.Pink);
Raylib.EndDrawing();
}
Raylib.CloseWindow();
void DrawFish(int x, int y, int size, Color color)
{
Raylib.DrawEllipse(x - size / 2, y, size / 8, size / 3, color);
Raylib.DrawEllipse(x, y, size / 2, size / 3, color);
Raylib.DrawCircle(x + size / 4, y - size / 10, size / 12, Color.Black);
}
void DrawSeaweed(int x, int height)
{
int pieces = height / 20;
for (int i = 0; i < pieces; i++)
{
int sway = 0;
if (i % 2 == 1)
{
sway = 8;
}
Raylib.DrawRectangle(x + sway, 400 - (i + 1) * 20, 12, 20, Color.Green);
}
}

Task 2: A Click Lasts One Frame

  • Delete all the DrawFish calls from the main program, including the one you added. The tank starts empty now.
  • Add the if on lines 18–21. It draws a fish at the pointer on the frame you click.
  • Run it and click around the tank. Write down what you see, and explain it using what the deck said about frames.
2-7-aquarium
#:package Raylib-cs@8.0.0
using Raylib_cs;
Raylib.InitWindow(800, 450, "Aquarium");
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.DarkBlue);
Raylib.DrawRectangle(0, 400, 800, 50, Color.Beige);
DrawSeaweed(120, 160);
DrawSeaweed(560, 240);
DrawSeaweed(700, 100);
if (Raylib.IsMouseButtonPressed(MouseButton.Left))
{
DrawFish(Raylib.GetMouseX(), Raylib.GetMouseY(), 40, Color.Orange);
}
Raylib.EndDrawing();
}
Raylib.CloseWindow();
void DrawFish(int x, int y, int size, Color color)
{
Raylib.DrawEllipse(x - size / 2, y, size / 8, size / 3, color);
Raylib.DrawEllipse(x, y, size / 2, size / 3, color);
Raylib.DrawCircle(x + size / 4, y - size / 10, size / 12, Color.Black);
}
void DrawSeaweed(int x, int height)
{
int pieces = height / 20;
for (int i = 0; i < pieces; i++)
{
int sway = 0;
if (i % 2 == 1)
{
sway = 8;
}
Raylib.DrawRectangle(x + sway, 400 - (i + 1) * 20, 12, 20, Color.Green);
}
}

Task 3: Remember Every Click

  • Two arrays hold up to 20 positions, and count says how many are in use. A click stores its x and y at position count, then adds 1 to count.
  • Make the changes shown: the arrays at the top, the click check moved above BeginDrawing, and a for loop that draws every stored fish, every frame. Delete Task 2's if.
  • Run it and click six times. Then keep clicking past twenty. Write down what happens after the twentieth fish, and which part of line 14 makes it happen.
2-7-aquarium
#:package Raylib-cs@8.0.0
using Raylib_cs;
int[] xs = new int[20];
int[] ys = new int[20];
int count = 0;
Raylib.InitWindow(800, 450, "Aquarium");
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
if (Raylib.IsMouseButtonPressed(MouseButton.Left) && count < xs.Length)
{
xs[count] = Raylib.GetMouseX();
ys[count] = Raylib.GetMouseY();
count = count + 1;
}
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.DarkBlue);
Raylib.DrawRectangle(0, 400, 800, 50, Color.Beige);
DrawSeaweed(120, 160);
DrawSeaweed(560, 240);
DrawSeaweed(700, 100);
for (int i = 0; i < count; i++)
{
DrawFish(xs[i], ys[i], 40, Color.Orange);
}
Raylib.DrawText($"Fish: {count} of {xs.Length}", 20, 20, 20, Color.White);
Raylib.EndDrawing();
}
Raylib.CloseWindow();
void DrawFish(int x, int y, int size, Color color)
{
Raylib.DrawEllipse(x - size / 2, y, size / 8, size / 3, color);
Raylib.DrawEllipse(x, y, size / 2, size / 3, color);
Raylib.DrawCircle(x + size / 4, y - size / 10, size / 12, Color.Black);
}
void DrawSeaweed(int x, int height)
{
int pieces = height / 20;
for (int i = 0; i < pieces; i++)
{
int sway = 0;
if (i % 2 == 1)
{
sway = 8;
}
Raylib.DrawRectangle(x + sway, 400 - (i + 1) * 20, 12, 20, Color.Green);
}
}

Task 4: Make the Click Decide

  • Two more arrays: sizes and isCrab. A click now works out a size from how deep it is — 20 + y / 8, so clicks near the sand make bigger creatures — and stores whether it was on the right half of the tank.
  • The drawing loop now decides between two methods for each creature. Make the changes shown.
  • Replace the comments at the bottom with DrawCrab, written yourself from the description. It takes x, y, and size, and every measurement should be worked out from size, the way DrawFish's are.
  • Fill the tank with at least four fish and four crabs at different depths, and take a screenshot.
2-7-aquarium
#:package Raylib-cs@8.0.0
using Raylib_cs;
int[] xs = new int[20];
int[] ys = new int[20];
int[] sizes = new int[20];
bool[] isCrab = new bool[20];
int count = 0;
Raylib.InitWindow(800, 450, "Aquarium");
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
if (Raylib.IsMouseButtonPressed(MouseButton.Left) && count < xs.Length)
{
xs[count] = Raylib.GetMouseX();
ys[count] = Raylib.GetMouseY();
sizes[count] = 20 + ys[count] / 8;
isCrab[count] = xs[count] >= 400;
count = count + 1;
}
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.DarkBlue);
Raylib.DrawRectangle(0, 400, 800, 50, Color.Beige);
DrawSeaweed(120, 160);
DrawSeaweed(560, 240);
DrawSeaweed(700, 100);
for (int i = 0; i < count; i++)
{
if (isCrab[i])
{
DrawCrab(xs[i], ys[i], sizes[i]);
}
else
{
DrawFish(xs[i], ys[i], sizes[i], Color.Orange);
}
}
Raylib.DrawText($"Creatures: {count} of {xs.Length}", 20, 20, 20, Color.White);
Raylib.EndDrawing();
}
Raylib.CloseWindow();
void DrawFish(int x, int y, int size, Color color)
{
Raylib.DrawEllipse(x - size / 2, y, size / 8, size / 3, color);
Raylib.DrawEllipse(x, y, size / 2, size / 3, color);
Raylib.DrawCircle(x + size / 4, y - size / 10, size / 12, Color.Black);
}
void DrawSeaweed(int x, int height)
{
int pieces = height / 20;
for (int i = 0; i < pieces; i++)
{
int sway = 0;
if (i % 2 == 1)
{
sway = 8;
}
Raylib.DrawRectangle(x + sway, 400 - (i + 1) * 20, 12, 20, Color.Green);
}
}
// DrawCrab(x, y, size) draws a red crab centered on (x, y):
// a red oval body, size wide and half as tall
// (Raylib.DrawEllipse takes the half-width and half-height, like DrawFish's body),
// a red circle claw at each top corner of the body,
// and two small black eyes on top.
// Write it here, below DrawSeaweed.

Task 5: Which Part Is Yours?

Three things, one at a time. Write down exactly what happens.

  1. On line 16, delete && count < xs.Length, so the check is only if (Raylib.IsMouseButtonPressed(MouseButton.Left)). Run it and click 21 times. Copy the error and its line number, and explain which line ran out of room and why. Put the check back.
  2. Add a right-click that empties the tank: after the left-click if, add an if on Raylib.IsMouseButtonPressed(MouseButton.Right) whose body is count = 0;. Fill the tank, right-click, and fill it again. Explain why the old creatures do not come back, even though their numbers are still in the arrays.
  3. In a comment at the top of your program, list every method you wrote and say whether it would count as a student-developed procedure. Then name the code that handles the clicks, and say why it does not count — and why adding the right-click did not get you any closer.

Keep the right-click. Your finished program is Task 4 with the right-click added and the comment at the top.


Challenge (Optional): Fish That Face Both Ways

  • Give DrawFish a fifth parameter, bool faceLeft. When it is true, the tail goes on the right and the eye on the left.
  • One way: a variable facing that is 1 normally and -1 when faceLeft is true, multiplied into the two offsets that put the tail behind and the eye in front.
  • Make fish placed in the right part of the fish half face left, toward the crabs. A bool parameter that decides which way something is drawn is a parameter that changes what the method does.
divider

Checkpoint

Every picture below is a screenshot of this page's own programs, with the clicks played back automatically.

The Task 1 tank: three strands of seaweed and three fish, orange, yellow, and pink, with the yellow fish in front of the tall seaweed

Task 1, as written

The same tank with the yellow fish now behind the tall seaweed, its body striped by the green pieces in front of it

Task 1, yellow fish called before the seaweed

The empty tank with one orange fish where the click happened

Task 2, the frame of the click

The empty tank, with no fish at all

Task 2, one frame later

The tank with six orange fish of the same size scattered around it, and white text reading Fish: 6 of 20

Task 3, after six clicks

Task 5's first change, on the 21st click:

Example Output
Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Program.<Main>$(String[] args) in 2-7-aquarium.cs:line 18
  • DrawFish, DrawSeaweed, and your DrawCrab are all below the main program, and every measurement in DrawCrab comes from size
  • Clicks on the left make fish and on the right make crabs, deeper ones bigger, and the tank stops at 20
  • Right-click empties the tank and it fills again
  • You recorded the Task 1 layering change, what Task 2 showed, the Task 3 twenty-first click, the crash, and your comment naming which methods are yours
divider

Reflection

Answer the following questions before submitting your work.

  1. In Task 2 the fish vanished, and in Task 3 it stayed. Explain the difference using the word frame.
  2. Why does the College Board exclude event handlers and main methods from counting as student-developed? Give a reason, not just the rule.
  3. Suppose you build your Build 1 project in Raylib. What would your input be, and which method would be yours?
divider

Submit

Submit four things to the dropbox:

  1. Your 2-7-aquarium program file, with your DrawCrab, the right-click, and the comment at the top.
  2. A screenshot of your Task 4 tank.
  3. Your Task 1 layering note, what Task 2 showed, what happened after the twentieth click, and the Task 5 crash with its line.
  4. Your three reflection answers.

Activity Complete