HomeworkBuilt at home. Verified in class at the start of the next session.
Thirty-two invaders in formation, marching side to side and dropping closer to the ground every time they reach a wall. These are three moments from the same running program.

The first frame

Seven bounces in

They landed
Nothing on this page is a new kind of code. You have nested loops from Activity 1.21, for from Activity 1.19, the game loop from Activity 1.18, and the idea of one number driving a whole drawing from Lab 2. What is new is putting them together: the loop counters stop being counts and start being positions.
'F' → Fullscreen
Everything today goes in a new program named 1-22-invaders. Nothing carries over from Lab 4.
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.
This lab needs the workspace, not Juicemind — same as Labs 1 through 4. Close the window to end the program. Escape works too.
formationX + col * spacingX. The last column is 7, not 8.int col = 0 to int col = 1, but predict first: how many invaders will there be, and does the one on the left or the one on the right disappear? Write your prediction down, run it, then put it back.spacingX to 40 — the same as invaderWidth — and run it. Say what happened to the gaps and why, then put it back to 56.
One row of eight
#:package Raylib-cs@8.0.0
using Raylib_cs;
int screenWidth = 640;int screenHeight = 480;
int cols = 8;int invaderWidth = 40;int invaderHeight = 24;int spacingX = 56;
int formationX = 60;int formationY = 50;
Raylib.InitWindow(screenWidth, screenHeight, "Invaders");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ Raylib.BeginDrawing(); Raylib.ClearBackground(Color.Black);
for (int col = 0; col < cols; col++) { int x = formationX + col * spacingX; int y = formationY;
Raylib.DrawRectangle(x, y, invaderWidth, invaderHeight, Color.Green); Raylib.DrawRectangle(x + 8, y + 6, 6, 6, Color.Black); Raylib.DrawRectangle(x + 26, y + 6, 6, 6, Color.Black); }
Raylib.EndDrawing();}
Raylib.CloseWindow();rows and spacingY, then put your row loop inside a new loop over the rows. Every line of your Task 1 loop moves one level in, so the whole block is marked — you are re-indenting it, not retyping it.y now uses row the same way x uses col.int drawn = 0; sits: inside the game loop, so it starts again from zero on every frame. Task 4 moves it.#:package Raylib-cs@8.0.0
using Raylib_cs;
int screenWidth = 640;int screenHeight = 480;
int rows = 4;int cols = 8;int invaderWidth = 40;int invaderHeight = 24;int spacingX = 56;int spacingY = 40;
int formationX = 60;int formationY = 50;
Raylib.InitWindow(screenWidth, screenHeight, "Invaders");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ Raylib.BeginDrawing(); Raylib.ClearBackground(Color.Black);
int drawn = 0;
for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { int x = formationX + col * spacingX; int y = formationY + row * spacingY;
Raylib.DrawRectangle(x, y, invaderWidth, invaderHeight, Color.Green); Raylib.DrawRectangle(x + 8, y + 6, 6, 6, Color.Black); Raylib.DrawRectangle(x + 26, y + 6, 6, 6, Color.Black);
drawn = drawn + 1; } }
Raylib.DrawText($"Invaders: {drawn}", 10, 10, 20, Color.White);
Raylib.EndDrawing();}
Raylib.CloseWindow();
The full formation
landed, then the movement block at the top of the game loop.speedX = 0 - speedX; flips the direction: 2 becomes -2, and -2 becomes 2 again. Every time it flips, the formation also drops by dropAmount.formationY + formationHeight, each bounce drops it 16 pixels, and the ground is at 450. Then run it and count the bounces as you watch.rows to 6 and cols to 5 and run it. The formation still turns around exactly at both walls. Say which line made that true — you did not touch it. Then put both back.#:package Raylib-cs@8.0.0
using Raylib_cs;
int screenWidth = 640;int screenHeight = 480;
int rows = 4;int cols = 8;int invaderWidth = 40;int invaderHeight = 24;int spacingX = 56;int spacingY = 40;
int formationX = 60;int formationY = 50;int formationWidth = (cols - 1) * spacingX + invaderWidth;int formationHeight = (rows - 1) * spacingY + invaderHeight;
int speedX = 2;int dropAmount = 16;int groundY = 450;bool landed = false;
Raylib.InitWindow(screenWidth, screenHeight, "Invaders");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ if (!landed) { formationX = formationX + speedX;
if (formationX + formationWidth >= screenWidth || formationX <= 0) { speedX = 0 - speedX; formationY = formationY + dropAmount; }
if (formationY + formationHeight >= groundY) { landed = true; } }
Raylib.BeginDrawing(); Raylib.ClearBackground(Color.Black);
int drawn = 0;
for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { int x = formationX + col * spacingX; int y = formationY + row * spacingY;
Raylib.DrawRectangle(x, y, invaderWidth, invaderHeight, Color.Green); Raylib.DrawRectangle(x + 8, y + 6, 6, 6, Color.Black); Raylib.DrawRectangle(x + 26, y + 6, 6, 6, Color.Black);
drawn = drawn + 1; } }
Raylib.DrawRectangle(0, groundY, screenWidth, 4, Color.Gray); Raylib.DrawText($"Invaders: {drawn}", 10, 10, 20, Color.White);
if (landed) { Raylib.DrawText("THEY LANDED", 174, 220, 40, Color.Red); }
Raylib.EndDrawing();}
Raylib.CloseWindow();
The bottom row lands exactly on the ground
Two changes, one at a time. Screenshot or describe what you see each time, and put it back before the next.
int drawn = 0; so it sits above the while line instead of inside the loop, and run it. Watch the counter for a few seconds, and work out how much it goes up on each frame.col < cols to col <= row, and run it. Record the shape, the counter, and where the formation turns around at the right side of the window.Neither one is an error. Both compile, and neither warns you about anything. In the first, the picture is perfect and the number is wrong. In the second, the picture changed and one number did not. Say which number, in each case.
groundY - cannonHeight, and the arrow keys work exactly like Activity 1.18's paddle.bool shotFlying keeps track, and IsKeyPressed from Lab 3 is the right key check here.x and y exist. It is the same kind of && chain as Activity 1.18's catch, with four parts instead of three:if (shotFlying && shotX >= x && shotX <= x + invaderWidth && shotY >= y && shotY <= y + invaderHeight){ hits = hits + 1; shotFlying = false;}hits on screen next to the invader counter, and play it.Your two broken versions from Task 4 should look like this.

Task 4, change 1 — two seconds in

Task 4, change 2 — turning around at the right side
Look at where the staircase turned around. Its right edge is more than 200 pixels from the wall, because formationWidth was worked out before the loop ever ran and still says eight columns. The picture changed; the number the bounce believes did not.
Answer the following questions before submitting your work.
formationX moves all thirty-two. Explain how, and compare it to what gap did in Lab 2.drawn has to start, and why the game loop is what makes that matter.formationWidth, and describe what would have to change for the bounce to match the picture again.Submit four things to the dropbox:
rows at 4 and cols at 8.