0% found this document useful (0 votes)
3 views

AdvancedProgrammingWithScratch

The document provides an overview of advanced programming concepts using Scratch, including the use of the pen feature for drawing, creating reset scripts, and utilizing loops for repetitive tasks. It also covers the importance of variables, conditionals, and broadcasting events in Scratch programming. Additionally, it discusses how to create levels in games and manage sprite visibility for game development.

Uploaded by

stela_francis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

AdvancedProgrammingWithScratch

The document provides an overview of advanced programming concepts using Scratch, including the use of the pen feature for drawing, creating reset scripts, and utilizing loops for repetitive tasks. It also covers the importance of variables, conditionals, and broadcasting events in Scratch programming. Additionally, it discusses how to create levels in games and manage sprite visibility for game development.

Uploaded by

stela_francis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Advanced Programming with

Scratch
Slides
• Many of these Scratch slides come from here:

– h:p://cs.harvard.edu/malan/scratch
– h:p://scratched.gse.harvard.edu/
– h:ps://courses.edx.org/courses/course-
v1:HarveyMuddX+CS002x+2T2016/course/

2
Pen
• Scratch has a secret!
• The Sprites are carrying a pen!
• You can tell it to put the pen down.
• Then when the cat moves, he draws a line!
• What happens when you do this?

• 100 pixel line (move cat to see it all)


Drawing
• When drawing it may be very helpful to do the
following:

– Clear the screen


– Move the pen back up
– Move the cat back to 0, 0
Reset Script
• A Reset Script can be very helpful
• You can do that when the space key is pressed
• You may also want the Cat to always point to the
right when you clear the script in case he gets turned
around.
Backpack
• If you want the reset script in a few files (when
creaWng shapes: square, rectangle, etc.) You can:
– Save As under File menu so you can work off exisWng code
in a new file.
– Drag it to your backpack and you will always have it! (You
can also drag Sprites or media in your backpack!)
Click on bo:om of Scratch screen

Drag code into backpack


Make a square
• I can start like this but what’s next?
Let’s do it like this first
• Duplicate:

• Double-click in Scratch and what happens?


Let’s slow it down
• Try this now!
Some concerns
• What if I want to change the size of the square
and have the Cat move 50 steps or 200 steps?
• I have to remember to change it everywhere
in all 4.
• Let’s use a loop!
Using a loop!
Pen Color

Click the box, then click


a color on your screen!
Set Pen Size
• Let’s make it thicker
Change size
• What do you think will happen now?
Can you draw a Equilateral triangle
(60-60-60) triangle?
• Let’s try it together – what should I do?
I’d like to do this!
What happened?
Exterior / Interior Angles

• What is an exterior angle?


• The sprite turns an angle which is called an Exterior
angle.
• Sum of exterior angles is 360.
• Exterior angle + interior angle = 180
• So…

• 180 – interior angle (what you want) = Exterior angle


which is the turn you should specify.
Exterior / Interior, cont.
• In a square you didn’t have to think about interior
and exterior angles because they are both equal to
90º.
• With a triangle, turn 120.
SoluWon
In Class Exercise
• Draw a Pentagon (You can work in pairs)
• AND answer this quesWon:

• How many degrees did the character turn in total


when it drew the pentagon?

• If you have extra Wme, draw these shapes and


answer the same quesWon above:
– Hexagon
– Circle
It always equals 360!
For all 3 shapes the total amount I turn is 360!
So how would I draw a Hexagon?
• Use division.
• Hexagon has 6 edges so would repeat 6 Wmes.
• What is 360 divided by 6?
• You can even have Scratch do the math for you!

• You can use this logic to draw other shapes like


an Octagon or even a circle.
Circle
• Perimeter is 360
360 X 1

• Larger Circle
• Perimeter is 540 (360 X 1.5)
Blocks
• Similar to FuncWons/methods
• Make a new block. Call it draw square.
• Now you can add the logic to draw a square
and call it whenever you want.
• Let’s try!
• Click edit – you can add input if you want the
person calling draw square to specify
something like size of square.
Create a new Block
NegaWve Steps?
• What do you think would happen if I moved
-100 steps?
Variables
• In programming, a variable is a placeholder
for some value, much like x and y are popular
variables in algebra.
• In Scratch, variables are represented with
blocks shaped like elongated circles, uniquely
labeled by you.
Variables
• Counter – let’s watch the variable change
Variables and CondiWons

What will happen in each of these cases:


MulWple IFs

What will X be in the end?


55
AND OR NOT

TRUE if BOTH are true

TRUE if EITHER are true

TRUE if what’s in diamond is FALSE


What is the value of X at the end of
this Script?
Velocity
Use a variable! – See the variable on the
Stage
Input
• We can ask our user for input!

User can enter input “Rachel”


Variable Answer
• Where is the name stored?
• Variable: Answer
Join!
• Or you can do this with a join.
Variable
Instead of input you can also create a variable:
Make a variable called name
MulWple Joins
Broadcast
• Blocks whose labels begin with "broadcast"
signal events
• whereas blocks whose labels begin with
"when" handle events.
Broadcast
• Watch my Duck/Cat Broadcast Script
• I added a Duck Sprite and flipped it to point
towards the cat’s direcWon here
Broadcast, cont.
Duck Scripts
Cat Scripts
Adding a Level
• Levels can be added. Levels are just addiWonal
backdrops.
• Can create 3 backdrops by going to paint a new
backdrop, then right-click and select duplicate.
• Set them to different colors each (use paint bucket)
• Name level1, level2
and level3
Paint a door
• Paint a new sprite door – rename it Door by
clicking on the i by the sprite.
Level code
Cat Script

• Script for Stage


Level, cont.
• If you were creaWng a game, you can keep
track of levels with a variable that gets
incremented each level.
Games
• You may want to hide Sprites (in games someWmes
they appear and reappear at random intervals.)
This work is licensed under the CreaWve Commons A:ribuWon 4.0 InternaWonal License.
To view a copy of the license, visit h:ps://creaWvecommons.org/licenses/by/4.0/.

You might also like