Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (2024)

Overview:

Our CS Academy course has come to an end. On this last unit we learnt some of the most important concepts in the whole course, including “for” loops in python. We also learnt about the creation of two new shapes: arcs, which are basically a portion of a circle, and arrows, which are basically lines with arrow heads on their limits. These shapes can be very useful, especially the arc, because you can make rounded edges or other shapes that only require a portion of a circle instead of a whole one. Local variables are the same as global variables, but created inside of functions. They aren’t accessible from any other part of the program, but they make your code efficient and reusable. For loops are also very important, because they allow you to draw the same shape several times but in different places, at once, instead of having to draw the same shape all over again the amount of times you need them.

Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (1)

New shapes: arrows and arcs:

In unit 7 we learnt how to program two new “shapes”, that can be very useful while constructing any shape or animation. The first shape is called an “arc”. This is just basically a part or a portion of an oval, or a circle. The arc shape takes in the same 4 attributes that the oval: the center coordinates, the width and the height. It also takes in two new arguments, specific only to the arc, called the “start angle” and the “sweep angle”. The start angle of the arc is where it starts (for example, it might start at 0 degrees, or at 90 degrees, etc.). The sweep angle determines how much of the oval does the arc cover. For example, if the sweep angle is 90, it means that the arc will cover 90 degrees of the oval, starting at the “start angle” that you input. An arc can also have the shape of a circle, instead of the shape of an oval, since, obviously, an oval with the same width and height is essentially a circle. For that, you just make the width and height of the oval be the same, which would be twice the radius of the circle you want to draw. Obviously, we can make an arc without necessarily drawing the circle that the arc “belongs” to. Here is a neat example of the application of arcs:

Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (2)
Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (3)

Arcs have many other applications, as we will see ahead, including the creation of animations.

The other new “shape” that we learnt was the arrow. Technically, arrows are not a new shape, they are just a line with arrow heads on its extremes. The “arrow” is more like a property of a line. You can make an arrow by adding the Boolean properties “arrowStart” and “arrowEnd” to a line. In this case, you would put arrowStart = True to make a line with an arrow head on it’s first point; or arrowEnd = True to make a line with an arrow head on its last point. The arrow heads belong to the line; they are not a different shape. This means that the properties (fill, opacity, etc.) of the arrow heads are the same than the ones of its line, and you cannot change them separately (which sucks). Here is a cool example of arrows and arcs being used in combination with onMouseDrag:

Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (4)

Local variables:

Local variables are very similar to global variables. The difference is that global variables are coded outside of any function, and therefore they are accessible throughout your program. Local variables, on the other hand, are defined inside of functions, and can be modified only inside of that function, not outside of it. This may look like global variables are more comfortable to use, and make your code easier, because you can use it in any part of your code. Even though this is partially correct, you can achieve the same results using local variables instead of global variables (but you need more knowledge), and they are “reusable” or “recyclable”. This basically means that if you want to use a piece of code from one program and past it to another, you wouldn’t have to change anything from the second program, because the variable is already defined inside of a function, making it much easier to achieve. Even though we didn’t know it, we were familiar with local variables since unit 2, with function parameters. Parameters are a special kind of local variables, because they are only used inside of a function, not outside of it. When you have local variables inside of a function that help the function to run, these are called “helper variables”. Here is a cool example:

Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (5)
Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (6)

“For” loops:

The last thing we learnt in unit 7 of CS Academy were loops. Loops are basically a way of iterating the same code a certain amount of code. “For” loops are very useful, because using one simple line of code, you can draw the exact same shape but in different positions, without having to draw them separately. Here is a simple “for” loop example, that can show how “for” loops work.

Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (7)
Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (8)

On the code above, the “for i in range 5” statement represents the amount of times that the code is supposed to be ran inside the loop. Based on the value of i, the program loops through the body of the for loop and executes the code i times. As you might notice, the i is also inside the actual code. “for i in range 5” means that i takes a value from 0 to 4, and that is not only the amount of times that the code is repeated, but that can also be used inside the body of the code. In this case, 68 + 80*i means that the x-position of the head of the audience shall be drawn 4 times, and that each time it will be drawn in a different position based on the value of i. For example: when i is 0, the code is ran for the first time. The position of the head would be 68 + 80*0 = 68. When i = 1, the code is ran for the second time, and the position would be 68 + 80*1 = 148. What this code does is basically to loop through the numbers from 0 to i, and each time it is a new number, it runs the code. If you add any value related to i, i will take the value of the number of the loop you are in (either it be 1, 2, 3, etc.)

Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (9)
Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (10)

There is also a very useful way of using for loops, but instead of building shapes, it is to refer to the children of a group, and to make changes to each of them using the for loop instead of referring to each and one of them separately. This spares time and a lot of lines of code. You can also “loop through groups” using conditionals to test things about the children of a group and to make changes to them depending on the result of the condition. Here is an example of looping through groups:

Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (11)
Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (12)

The looping through groups concepts is a very useful application in general coding. In this case, the i on the loop statement represents each and everyone of the children of a group. If i hits the shape of the snow pile, it disappears. This works because the for loop goes through, or “loops” through all of the children of the snowFlakes group, onStep (always) and if at any moment, a children of the group (i) hits the snow pile, it is removed from the group.

As you can see, our time in CS Academy coding is over. However, we took very important lessons from this last units that turned out to be very important in our culminative assignment. Here are the unit exercises for unit 7. See you next time!

Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (13)
Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (14)
Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (15)
Unit 7 CS Academy: Arcs, arrows, local variables and for loops. (2024)

References

Top Articles
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 6641

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.