Keep on Abstracting


9/27

Agenda:

  • Grad invites
  • Little Brother Time
  • Adding to Luigi
  • Shapes!

Objective: Practice using abstraction to simplify code

individual meeting link: https://meet.lync.com/teals/williamvaughn/VQDDNSNL

As you come in today please being the brain warm-up!

Brain Warm-Up

WU - Graduation Letters Intro

Graduation day will be here before you know it for you Seniors and the same can be said for all of the rest of you. With graduation day comes the chores of preparation. The Senior photos, buying a cap and gown, applying to colleges, applying to jobs, renting a snazzy prom getup, and all the other activities that come with the transitional year.

One of these tasks is inviting people to your graduation day.

For this exercise, you take on the role of a graduating Senior needing to invite the most people to your graduation. Particularly imagine you're a graduating Senior who's relatives believe in sending money to graduating family members, thus incentivizing you to spam out letters like a Nigerian prince attempting to scam the elderly.

Complete the exercise below.

WU - Exercise (write down your answers and thoughts, we will discuss as a group)

Consider the amount of writing you will need to do if you were to write a letter to 50 potential guests.

How many words would your write if, on average, you wrote 30 words per letter? What if now you wanted to invite 100 guests?

What if there were a typo on the dates you listed on your cards, how many times would you need to correct this mistake given you are sending to 100 guests?

Can you think of any algorithms or ways to decrease the amount of writing you would need to do?

Could you make it to where the only thing you are needing to add to the original letter is the name of the recipient? or could you find a solution where you correcting the typo by only changing the date in one location?

How could you make this task as easy as possible so that you can spam the most mailboxes? Consider your favorite tool as a Computer Scientist when coming up with solutions.

How does this relate to abstraction and what we have seen with custom blocks in Snap!?

Ready Player One

Recently we have been learning about variables and abstraction. How to focus on creating more dynamic programs to do more powerful things with less code.

Below is a starter xml that includes our recurring intro to cs pal Mario. However, this time he is accompanied by his little brother, Luigi.

https://drive.google.com/open?id=0B-3H08jxiXUgZldaTVJHYWd3YWM

For this exercise:

  • Create a script that asks the user which player they would like to play as when the green flag is clicked (store this information for use in future scripts)
  • Switch the sprite to the appropriate character costume based on user input. The user should remain the character they chose until they hit the green flag and are asked to change their character again.
  • By now we are Sprite walking veterans, within the "Game Loop" (a forever loop that has conditionals inside it to check for key presses or variable changes) check to see for key presses and have the character (in the right costume based on user selection) walk in the given direction with a walk animation.

!! Make sure your sprite is using all 3 frames of the run animation!!

Try to do this in as little code as possible. How can you use the variable to decide which costumes you should be using?

  • Notice that walk left and walk right are mostly the same code with minor differences. Using what we know about custom blocks combine the re-used code from each section into one custom block so there isn't as much repeated code.
  • ****Bonus**** Try to play code golf. See how few blocks you can get your code down to using custom blocks and variables.

************ Hints *************************************

    • Whenever prompted if you'd like a custom block or variable to be global or for this sprite only choose global (for the time being)
    • When creating custom blocks there are three types they let you pick between for block type:
          • Command: a script of blocks to be run,
          • Reporter: reports a value much like variables do,
          • Predicate: a boolean check much like a reporter but True/False
    • Observe the similarities in costume names and try to think of the existing patterns. What are the parts of the costume name? Could you conceptually separate the costume name into sections that could be combined.
    • When making custom blocks you can choose to make the block a "reporter". Reporters (like variables) are blocks that store information. Try making a custom block that tells you which costume you should be in. This custom block should be a combination of the players selected size+ protagonist + the rest of the name of their costume + the number.

************ End of Hints *********************************

We can do better

Using variables to more wisely control program state lets you clear your mental workspace. This works just like the solutions we created for sending out graduation invitations. By not having to repeat yourself you save time and effort that can be used to write other parts of your program. Once you have the above working to your liking try the following:

  • Introduce a variable that tracks whether Mario/Luigi is his bigger version or not. This can be a boolean value or a word stored in a variable.
  • Create a script that changes this variable when the player touches the mushroom (much like we have done in previous labs).
  • Change your custom reporter or costume change code to include the word "Big" when Mario/Luigi is bigger and to not include "Big" when the character is smaller.


aaaand a little extra

For this section use whatever variable you were using above to track the size of Mario/Luigi to determine what happens to the character when it comes into contact with something harmful.

  1. Create a script that responds to Mario/Luigi taking damage.
  2. If your character is in "big mode" have them shrink
  3. If your character is currently not in "big mode" have them change to the costume provided in this section

Try changing variables in your new code, so that you do not have to alter your older existing code.

More shapes!

https://tealsk12.gitbooks.io/introduction-to-computer-science/content/Unit%203%20PDF/Lab%203.2%20Drawing%20Shapes%20Again.pdf

More shapes. Just like we are veterans of walking scripts, we've drawn shapes a bunch too. This lab focuses on creating custom blocks to draw shapes. By the end of this lab you should have a custom block that can take how many sides you want and how large you want them. Use variables to avoid having "hard coded" values. Let these values change based on input.

Try to focus on the "If you've seen one" section and get that completed. This displays the power of dynamic code.

*****Hint ************

Look at how many degrees you must turn to make a shape. All angles must add to 360. A square has 4 sides and each angle is 90 degrees. 4 * 90 = 360.

So think of the equation based on the number of sides you want that will give you how much the sprite should turn each time it needs to turn.

*****End of Hint*****