Back

Activity 2.4: Raylib Lab 7 — Parameters You Can See

divider

What You're Building

A dark blue night sky over a black street, with three gray buildings of different widths and heights, every floor lit with rows of yellow windows

By the end of Task 2, one method draws all three buildings. You give it where the building stands, how wide it is, and how many floors it has. The method works out the rest — how tall it is, where its roof goes, and how many windows to light on every floor.

Task 3 is not shown. That city is yours to design.

divider

Activity 2.4

Raylib Lab 7 — Parameters You Can See

Key Concepts

One Method, Many Pictures

Parameters as Dials

Shapes Worked Out From One Number

The Create Task's Core Requirement

Activity 2.2's Parameter Had to Be Taken on Trust

Countdown(3) printed four lines and Countdown(5) printed six. You counted the lines and believed that the parameter changed what the method did.

Today the change is a picture. Turn a parameter and a tree moves, grows, or changes color while you watch. There is nothing to count and nothing to take on trust.

This Is the Most Important Lab in the Course

The Create Performance Task asks for a procedure you wrote that uses one or more parameters that “have an effect on the functionality of the procedure.”

Every student in the course will need one. In a terminal, whether a parameter really matters can be hard to see. On a screen it is impossible to fake: either the picture changes when the argument does, or it does not.

Three Circles, Three Sets of Numbers

Raylib Lab 1 drew circles by typing every number into Raylib.DrawCircle. Here are three, each with its own position and radius. Nothing connects them except that they look alike.

Demo
Raylib.DrawCircle(200, 225, 20, Color.SkyBlue);
Raylib.DrawCircle(400, 225, 45, Color.SkyBlue);
Raylib.DrawCircle(600, 225, 75, Color.SkyBlue);

Three Circles, Three Sets of Numbers

Give them one method, DrawDot, with a parameter for each number that changes. Predict what the three calls draw.

Demo
DrawDot(200, 225, 40);
DrawDot(400, 225, 90);
DrawDot(600, 225, 150);
void DrawDot(int x, int y, int size)
{
Raylib.DrawCircle(x, y, size / 2, Color.SkyBlue);
}

Three Circles, Three Sets of Numbers

Three light blue circles in a row on black, small, medium, and large

The same picture as before. DrawDot was written once, and each call turned its three dials — x, y, and size — to different settings. size / 2 turns a width into the radius DrawCircle wants.

You Have Been Turning Raylib's Dials Since Lab 1

Raylib.DrawCircle(x, y, radius, color) is a method with four parameters, and the labs have called it with arguments since Lab 1. Somebody at Raylib wrote it once; you have used it hundreds of times.

DrawDot is the same idea, written by you. Its body calls Raylib's method and passes its own parameters along — a method calling a method, the way Activity 2.1's Outer called Inner.

A Tree Is Two Shapes Worked Out From One Number

A tree is a brown rectangle for the trunk and a green circle for the canopy. Neither shape gets its own numbers. Every measurement is worked out from x, y, and size.

Demo
void DrawTree(int x, int y, int size)
{
Raylib.DrawRectangle(x - size / 10, y, size / 5, size, Color.Brown);
Raylib.DrawCircle(x, y, size / 2, Color.Lime);
}

A Tree Is Two Shapes Worked Out From One Number

Follow one call through the body. Work out each number before it appears.

Value Trace
DrawTree(160, 260, 80)
trunk left edge x - size / 10 160 - 8 152
trunk top y 260
trunk width size / 5 80 / 5 16
trunk height size 80
canopy center (x, y) (160, 260)
canopy radius size / 2 80 / 2 40

The trunk is a fifth as wide as the tree is big, and exactly as tall. Change size and all four measurements change together, so the tree keeps its shape at any size.

Three Calls, Three Trees

The trunk runs from y down to y + size. In each of these calls that adds up to 340 — where the ground starts. Predict what the forest looks like.

Demo
DrawTree(160, 260, 80);
DrawTree(400, 200, 140);
DrawTree(640, 280, 60);

Three Calls, Three Trees

Three trees of different sizes standing on the same strip of ground

Every trunk stands on the ground, because the arguments were chosen so that y + size is 340 every time. The method does not know about the ground; the calls do.

A Parameter You Ignore Does Not Count

Here is DrawTree with one change: the canopy's radius is the number 50 instead of size / 2. It still takes size, and the trunk still uses it. Predict the forest.

Demo
void DrawTree(int x, int y, int size)
{
Raylib.DrawRectangle(x - size / 10, y, size / 5, size, Color.Brown);
Raylib.DrawCircle(x, y, 50, Color.Lime);
}

A Parameter You Ignore Does Not Count

Three trees whose trunks are different sizes but whose round canopies are all exactly the same size

Three canopies, all the same size. The method accepts size and the canopy ignores it. A parameter only counts for the Create task if changing it changes the result — and on a screen you can check that in one glance.

Raylib's Methods and Your Main Program Are Not Yours

The College Board says that built-in procedures, and language structures such as event handlers and main methods, are not student-developed. They do not count for the Create task, however much code you put in them.

So Raylib.DrawCircle does not count: you call it, but Raylib wrote it. Your main program — the window and the while loop — does not count either; it is the main method C# builds from your top-level lines.

DrawTree counts. You wrote it, it takes parameters, and they change what it draws.

Today's Objectives

  • Writing a Raylib method that takes position and size parameters
  • Working out every measurement of a drawing from its parameters
  • Predicting what a call will draw before running it
  • Explaining what makes a parameter affect functionality

Key Terms

Student-developed procedure
A procedure you wrote yourself. Built-in methods like Raylib.DrawCircle, and the main program, do not count.
Affects functionality
A parameter does this when changing its argument changes what the procedure does or produces.
Derived measurement
A number worked out from a parameter, like size / 5 for the trunk width, rather than typed in.

'F' → Fullscreen

divider

Build

Today you build a city at night in a new program named 2-4-skyline. Every building comes from one method. The deck's trees used their parameters for sizes; your buildings use them to decide how many times a loop runs, so a bigger argument means more windows, not just a bigger shape.

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: Buildings That Stand on the Street

  • Write the program below and run it. DrawBuilding takes a position, a width, and a number of floors. Each floor is 30 pixels tall, so the method works out the height, and from that the top of the building: 400, the street, minus the height.
  • The method goes below Raylib.CloseWindow();, at the very bottom, the same as every method in this course.
  • Change the last building from 4 floors to 8 and run it. Write down which way it grew — up or down — and which line of the method made that happen. Put it back to 4.
2-4-skyline
#:package Raylib-cs@8.0.0
using Raylib_cs;
Raylib.InitWindow(800, 450, "Skyline");
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.DarkBlue);
Raylib.DrawRectangle(0, 400, 800, 50, Color.Black);
DrawBuilding(80, 100, 6);
DrawBuilding(250, 70, 11);
DrawBuilding(400, 130, 4);
Raylib.EndDrawing();
}
Raylib.CloseWindow();
void DrawBuilding(int x, int width, int floors)
{
int height = floors * 30;
int top = 400 - height;
Raylib.DrawRectangle(x, top, width, height, Color.Gray);
}

Task 2: Turn the Lights On

  • Add the two loops to the bottom of DrawBuilding. The outer loop runs once per floor; the inner loop draws one window every 30 pixels across, so width / 30 windows per floor. Activity 1.21's nested loops, inside a method.
  • Run it and compare it with the picture at the top of this page. Count the windows on the widest building and check the count against width / 30.
  • Change the middle building's width from 70 to 100 and run it. Write down how many windows each floor has now, and why. Then put it back to 70.
2-4-skyline
#:package Raylib-cs@8.0.0
using Raylib_cs;
Raylib.InitWindow(800, 450, "Skyline");
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.DarkBlue);
Raylib.DrawRectangle(0, 400, 800, 50, Color.Black);
DrawBuilding(80, 100, 6);
DrawBuilding(250, 70, 11);
DrawBuilding(400, 130, 4);
Raylib.EndDrawing();
}
Raylib.CloseWindow();
void DrawBuilding(int x, int width, int floors)
{
int height = floors * 30;
int top = 400 - height;
Raylib.DrawRectangle(x, top, width, height, Color.Gray);
for (int floor = 0; floor < floors; floor++)
{
for (int col = 0; col < width / 30; col++)
{
Raylib.DrawRectangle(x + 10 + col * 30, top + 8 + floor * 30, 15, 15, Color.Yellow);
}
}
}

Task 3: Your City

  • Give DrawBuilding a fourth parameter, Color lights, and use it for the windows instead of Color.Yellow. A call then looks like this: DrawBuilding(420, 90, 8, Color.Orange);
  • Replace the three buildings with a skyline of at least six, using at least two different heights and two different window colors. Raylib has many named colors: Color.Gold, Color.SkyBlue, Color.Orange, Color.Pink, and more.
  • Nothing may overlap or run off the right edge, which is at x = 800. Each building's x plus its width tells you where the next one can start.
2-4-skyline
#:package Raylib-cs@8.0.0
using Raylib_cs;
Raylib.InitWindow(800, 450, "Skyline");
Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose())
{
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.DarkBlue);
Raylib.DrawRectangle(0, 400, 800, 50, Color.Black);
DrawBuilding(80, 100, 6);
DrawBuilding(250, 70, 11);
DrawBuilding(400, 130, 4);
// Task 3: give DrawBuilding a fourth parameter, Color lights, for the
// windows. Then replace these three buildings with a skyline of at
// least six, at least two different heights and two different colors.
Raylib.EndDrawing();
}
Raylib.CloseWindow();
void DrawBuilding(int x, int width, int floors)
{
int height = floors * 30;
int top = 400 - height;
Raylib.DrawRectangle(x, top, width, height, Color.Gray);
for (int floor = 0; floor < floors; floor++)
{
for (int col = 0; col < width / 30; col++)
{
Raylib.DrawRectangle(x + 10 + col * 30, top + 8 + floor * 30, 15, 15, Color.Yellow);
}
}
}

Task 4: Break It on Purpose

Two changes, made to a copy of your Task 2 program, one at a time. Screenshot or describe what you see each time, and put it back before the next.

  1. In DrawBuilding's outer loop, change floor < floors to floor < 5.
  2. Put it back. In the first call, swap the last two arguments, so it reads DrawBuilding(80, 6, 100);.

Neither change gives an error. For each one, say what is wrong with the picture, building by building, and which number the method used that it should not have. Number 1 has something in the street that should not be there.


Challenge (Optional): A Dial That Adds Something

  • Add a bool antenna parameter. When it is true, draw an antenna on the roof: a line straight up from the middle of the roof, 40 pixels tall, with a small red light on top.
  • Raylib.DrawLine(x1, y1, x2, y2, color) draws a line between two points. The middle of the roof is x + width / 2, and the roof is at top.
  • Give only your tallest building an antenna. This parameter decides whether something is drawn at all — the strongest kind of “effect on the functionality” there is.
divider

Checkpoint

Your two broken versions from Task 4 should look like this. If yours looked different, you changed something else as well.

The skyline with every building lit for exactly five rows: the tall tower is dark below its top five floors, and the short building has a row of yellow windows floating in the black street below it

Task 4, change 1 — the loop ignores floors

The skyline with the first building replaced by a thin gray line running from the street off the top of the window, with no windows at all

Task 4, change 2 — width and floors swapped

  • One DrawBuilding method draws every building, and it is below the main program
  • Every building stands on the street, and its windows come from the two loops
  • Your Task 3 skyline has at least six buildings, two heights, and two window colors, with nothing overlapping
  • You recorded the Task 1 and Task 2 changes, and both Task 4 pictures with the number that went wrong
divider

Reflection

Answer the following questions before submitting your work.

  1. Which of your DrawBuilding parameters affects functionality, and how would you prove it to someone in ten seconds?
  2. Raylib.DrawRectangle takes parameters too, and you have called it since Lab 1. Explain why the Create task would not count it as a student-developed procedure, when it would count DrawBuilding.
  3. Task 4's swapped call drew a building six pixels wide and a hundred floors tall, with no error. Use what Activity 2.2 showed about arguments to explain why C# could not catch it.
divider

Submit

Submit four things to the dropbox:

  1. Your 2-4-skyline program file, with your Task 3 city.
  2. A screenshot of your Task 3 skyline.
  3. Your Task 1 and Task 2 notes, and both Task 4 results with the number that went wrong in each.
  4. Your three reflection answers.

Activity Complete