Eight invaders scattered across the sky, a cannon on the ground, and this time an invader you hit stays gone. Clear all eight in as few shots as you can.

In Lab 5 every invader came back on the very next frame, because nothing in the program remembered that it had been hit. Activities 1.23 through 1.25 built the tool that fixes it: an array. Today the positions come from arrays, and so does the memory of which invaders are gone.
'F' → Fullscreen
Everything today goes in a new program named 1-26-invaders. Nothing carries over from Lab 5, although a lot of it will look familiar.
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 5. Close the window to end the program. Escape works too.
, 330 at the end of xs and , 140 at the end of ys, then run it. Write down what changed, and which lines you did not have to touch., 140 back out of ys only, but predict first: will the program draw eight invaders, nine, or something else? Write your prediction down, run it, and record the exception and the line number it names., 330 out of xs too, so both arrays are back to eight numbers.
Eight invaders, from data
#:package Raylib-cs@8.0.0
using Raylib_cs;
int screenWidth = 640;int screenHeight = 480;
int[] xs = { 40, 150, 260, 370, 440, 160, 270, 400 };int[] ys = { 70, 50, 100, 60, 130, 170, 200, 210 };
int invaderWidth = 40;int invaderHeight = 24;
Raylib.InitWindow(screenWidth, screenHeight, "Invaders");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ Raylib.BeginDrawing(); Raylib.ClearBackground(Color.Black);
for (int i = 0; i < xs.Length; i++) { int x = xs[i]; int y = ys[i];
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.DrawText($"Invaders: {xs.Length}", 10, 10, 20, Color.White);
Raylib.EndDrawing();}
Raylib.CloseWindow();formationX, speedX, and sway, and the movement block at the top of the game loop. Then add formationX to every x.col * spacingX. The formation sways from formationX 0 to formationX 160 and back.formationX, plus invaderWidth. Then run it and watch that invader at the right wall.xs to 500 and run it. Describe what happens at the right side of the window, and say which number did not know about your change. Then put it back to 440.#:package Raylib-cs@8.0.0
using Raylib_cs;
int screenWidth = 640;int screenHeight = 480;
int[] xs = { 40, 150, 260, 370, 440, 160, 270, 400 };int[] ys = { 70, 50, 100, 60, 130, 170, 200, 210 };
int invaderWidth = 40;int invaderHeight = 24;
int formationX = 0;int speedX = 1;int sway = 160;
Raylib.InitWindow(screenWidth, screenHeight, "Invaders");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ formationX = formationX + speedX;
if (formationX >= sway || formationX <= 0) { speedX = 0 - speedX; }
Raylib.BeginDrawing(); Raylib.ClearBackground(Color.Black);
for (int i = 0; i < xs.Length; i++) { int x = formationX + xs[i]; int y = ys[i];
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.DrawText($"Invaders: {xs.Length}", 10, 10, 20, Color.White);
Raylib.EndDrawing();}
Raylib.CloseWindow();!shotFlying.x and y exist. Right now, all it does is stop the shot.#:package Raylib-cs@8.0.0
using Raylib_cs;
int screenWidth = 640;int screenHeight = 480;
int[] xs = { 40, 150, 260, 370, 440, 160, 270, 400 };int[] ys = { 70, 50, 100, 60, 130, 170, 200, 210 };
int invaderWidth = 40;int invaderHeight = 24;
int formationX = 0;int speedX = 1;int sway = 160;
int groundY = 450;int cannonWidth = 50;int cannonHeight = 20;int cannonX = 295;int cannonY = groundY - cannonHeight;
int shotX = 0;int shotY = 0;bool shotFlying = false;int shots = 0;
Raylib.InitWindow(screenWidth, screenHeight, "Invaders");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ formationX = formationX + speedX;
if (formationX >= sway || formationX <= 0) { speedX = 0 - speedX; }
if (Raylib.IsKeyDown(KeyboardKey.Left)) { cannonX = cannonX - 5; }
if (Raylib.IsKeyDown(KeyboardKey.Right)) { cannonX = cannonX + 5; }
if (Raylib.IsKeyPressed(KeyboardKey.Space) && !shotFlying) { shotFlying = true; shotX = cannonX + cannonWidth / 2; shotY = cannonY; shots = shots + 1; }
if (shotFlying) { shotY = shotY - 8;
if (shotY < 0) { shotFlying = false; } }
Raylib.BeginDrawing(); Raylib.ClearBackground(Color.Black);
for (int i = 0; i < xs.Length; i++) { int x = formationX + xs[i]; int y = ys[i];
if (shotFlying && shotX >= x && shotX <= x + invaderWidth && shotY >= y && shotY <= y + invaderHeight) { shotFlying = false; }
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.DrawRectangle(0, groundY, screenWidth, 4, Color.Gray); Raylib.DrawRectangle(cannonX, cannonY, cannonWidth, cannonHeight, Color.SkyBlue);
if (shotFlying) { Raylib.DrawRectangle(shotX - 2, shotY, 4, 12, Color.Yellow); }
Raylib.DrawText($"Invaders: {xs.Length}", 10, 10, 20, Color.White); Raylib.DrawText($"Shots: {shots}", 520, 10, 20, Color.White);
Raylib.EndDrawing();}
Raylib.CloseWindow();
Eleven shots, eight invaders
bool[] hit = new bool[xs.Length]; right under the two position arrays. It is made from xs.Length, so it always has one flag per invader.!hit[i] && at the front, so a dead invader cannot be hit again, and hit[i] = true; inside, so a hit is remembered.if (!hit[i]), and count each invader you draw. The three DrawRectangle lines move one level in, so they are marked — you are re-indenting them, not retyping them.left instead of xs.Length, and CLEARED appears when it reaches 0.hit holds when the game starts. Then play until you clear the screen, and write down your shot count. Eight is perfect.#:package Raylib-cs@8.0.0
using Raylib_cs;
int screenWidth = 640;int screenHeight = 480;
int[] xs = { 40, 150, 260, 370, 440, 160, 270, 400 };int[] ys = { 70, 50, 100, 60, 130, 170, 200, 210 };bool[] hit = new bool[xs.Length];
int invaderWidth = 40;int invaderHeight = 24;
int formationX = 0;int speedX = 1;int sway = 160;
int groundY = 450;int cannonWidth = 50;int cannonHeight = 20;int cannonX = 295;int cannonY = groundY - cannonHeight;
int shotX = 0;int shotY = 0;bool shotFlying = false;int shots = 0;
Raylib.InitWindow(screenWidth, screenHeight, "Invaders");Raylib.SetTargetFPS(60);
while (!Raylib.WindowShouldClose()){ formationX = formationX + speedX;
if (formationX >= sway || formationX <= 0) { speedX = 0 - speedX; }
if (Raylib.IsKeyDown(KeyboardKey.Left)) { cannonX = cannonX - 5; }
if (Raylib.IsKeyDown(KeyboardKey.Right)) { cannonX = cannonX + 5; }
if (Raylib.IsKeyPressed(KeyboardKey.Space) && !shotFlying) { shotFlying = true; shotX = cannonX + cannonWidth / 2; shotY = cannonY; shots = shots + 1; }
if (shotFlying) { shotY = shotY - 8;
if (shotY < 0) { shotFlying = false; } }
Raylib.BeginDrawing(); Raylib.ClearBackground(Color.Black);
int left = 0;
for (int i = 0; i < xs.Length; i++) { int x = formationX + xs[i]; int y = ys[i];
if (!hit[i] && shotFlying && shotX >= x && shotX <= x + invaderWidth && shotY >= y && shotY <= y + invaderHeight) { hit[i] = true; shotFlying = false; }
if (!hit[i]) { 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);
left = left + 1; } }
Raylib.DrawRectangle(0, groundY, screenWidth, 4, Color.Gray); Raylib.DrawRectangle(cannonX, cannonY, cannonWidth, cannonHeight, Color.SkyBlue);
if (shotFlying) { Raylib.DrawRectangle(shotX - 2, shotY, 4, 12, Color.Yellow); }
Raylib.DrawText($"Invaders: {left}", 10, 10, 20, Color.White); Raylib.DrawText($"Shots: {shots}", 520, 10, 20, Color.White);
if (left == 0) { Raylib.DrawText("CLEARED", 226, 220, 40, Color.Green); }
Raylib.EndDrawing();}
Raylib.CloseWindow();
Cleared in eight shots
Three changes, one at a time. Write down exactly what you see each time, and put each one back before the next.
bool[] hit = new bool[xs.Length]; from line 10 to inside the game loop, just above int left = 0;. Play, and watch an invader you hit, and the counter.hit[i] = true; on line 82 to hit[0] = true;. Shoot three or four invaders that are not the one at the far left. Record which ones disappear, and what the counter does.!hit[i] && from the start of the hit check on line 80. Shoot the invader that sits just below another one — it is 160 in xs and 170 in ys, under the one at 150 and 50 — and then try to shoot the one above it. Record what happens to your shots.All three compile, none of them warns you, and none of them crashes. For each one, say what the hit array held that it should not have, or did not hold that it should.
There is no code block for this task. When it is over, your program is exactly what it was at the end of Task 4.
sway is a typed-in 160 that knows nothing about the positions in xs. Move one invader further right and the formation walks it off the screen.sway is declared, find the index of the largest number in xs with Activity 1.25's best-arrow pattern.sway to screenWidth - invaderWidth - xs[widest], where widest is that index. With the page's positions it should still come out as 160.Your Task 5 change 2 should look like this after a while.

Task 5, change 2 — twenty-three shots in
The counter went to 7 and never moved again. Every hit marked index 0, so the invader at the far left vanished on the first one and every invader you actually aimed at stayed. The shots really were landing; the program was writing them down against the wrong invader.
xs and ys, and you can point to the two numbers that place any one of themCLEARED appears at 0hit held in eachAnswer the following questions before submitting your work.
hit is declared decides whether the game remembers a hit.!hit[i] has to be in the hit check as well as around the drawing.Submit four things to the dropbox:
xs back to its starting numbers.hit held in each.