2. Syllabus
MyCS: Computer Science for Beginners is composed of five units of
curriculum, which alternate back and forth between the ideas that shape
computer science and Scratch programming activities. The topics of these
units are listed below:
Unit 1: What is Computer Science? …………………………………….. (03)
Answer broad questions about the role of computers and the goals of
computer scientists.
Explore the definition of intelligence as it relates to computers.
Unit 2: Solving Puzzles in Scratch ……………………………………… (10)
Learn the basics of Scratch programming through a series of pre-made
mazes of increasing difficulty.
Unit 3: Data and Codes ………………………………………………………. (17)
Practice encoding and decoding information using a variety of codes and
methods.
Learn to represent numbers in binary.
Connect these concepts to computer science and working with data.
Unit 4: Projects in Scratch ………………………………………………….. (26)
Create your own stories, games, and interactions using Scratch.
Practice design skills for making unique programming projects.
Unit 5: Problem Solving and Algorithm ……………………………… (45)
Build intuition for how people and computers solve problems differently.
Learn basic algorithms for searching and sorting information, as well as
how we can compare these algorithms.
parts of this Scratch programming course will be review, it will also explore
in much more depth how problem solving and algorithms can be used
within Scratch.
After completing Week (5), students wishing for additional practice in
Scratch should sign up for CS002x: Programming in Scratch. While early
(02)
3. (03)
Ibrahim Omar CS 2015
Week (1)
What is Computer Science?
Computer Science: a branch of science that deals with the theory of
computation (stream of 1s and 0s) or the design of computers.
Computer Scientists study how to solve problems using computers.
Think of computers as machines that can store information (Input), use
that information according to a set of rules (Processing), and then
provide the results to the user again (Output).
My Definition: Computer as machine that store input data, then
processing it to appear the final form required.
Calculator, phone, Camera and Brain do the same things as laptop.
What's In a Computer?
At the simplest level, the inside of a computer is a very complicated
circuit. Electricity runs through all of the different parts of the computer to
allow us to store information, send visual and audio messages, process
data, and even run a small fan designed to keep the machine from
overheating.
Parts of a Computer: 1. Hardware Team
photos
8. (08)
Ibrahim Omar CS 2015
2. Software Team
photo
The Software in a computer tells the Hardware Team agents how to
store and display information by providing each agent with a set of
instructions.
Unlike Hardware, which is limited by how it's built, Software can be
completely rewritten and replaced; this flexibility is why it's called Soft-
ware.
Software can also be undesirable! Computer viruses are software
programs that give the Hardware Team agents bad instructions.
However, as with any team, the agents need instructions on how
to work together and accomplish their part of the task.
9. (09)
Ibrahim Omar CS 2015
Types of Software:
Operating Systems (Windows or Mac) are a type of software that
manage the basic operations of your computer and how you can
interact with it. They provide instructions to your machine, as well as a
structure for how other pieces of software can communicate with
hardware.
Computer Games, Web Browser like Google Chrome or Mozilla
Firefox, written an essay in Microsoft Word or play music in iTunes.
The Apps on a phone are also pieces of software: games, e-mail, to-
do lists, calendars, even basic alarm systems and timers.
Software and Hardware must work together to create a functional
computer, which Hardware can store and process information and
Software provides the instructions.
Computer Intelligence
When we refer to Artificial Intelligence (AI), we're specifically describing
the ability for computers to think and respond like humans.
Computers don't make decisions but Humans do, then those decisions
are carried out through clear linear instructions provided by the Software.
Alan Turing (1912-1954) was an English mathematician and computer
scientist. In 1950, He wrote a paper asking the question: "Can machines
think?" He proposed a thought experiment where a judge, a human and
a computer are all placed in separate rooms. The judge converses with
the human and with the computer without knowing which entity is the
human and which is the computer. The judge then must decide who is the
computer and who is the human. If the computer can successfully
impersonate a human to the point where the judge can't tell who is the
human and who is the computer, then the computer is considered
"intelligent".
Today, we call this test the Turing Test. So far, most computer scientists
agree that no computer has managed to fully pass this test.
10.
11. (10)
Ibrahim Omar CS 2015
Week (2)
Solving Puzzles in Scratch
What is Scratch?
Scratch is an online programming language created by MIT to teach the
basic concepts of Computer Science to people who are new to
programming (https://scratch.mit.edu).
Rather than typing out lines of code, we program using "blocks" that
represent different actions our program can take. By clicking and
dragging blocks into stacks, we create lists of commands for the
computer to follow.
the Blocks area in the middle of the screen.
Loops are a programming tool that let us easily repeat a step or series of
steps as many times as we need. In Scratch, we can make loops using
the "Repeat 10" block, which is the second block in the "Control" tab of
Scratch Puzzle 1.1 - 1.5: YouTube.com/watch?v=PBhbtmLDZlQ
12. (11)
Ibrahim Omar CS 2015
In general, writing a program whether in Scratch or in any other
language, isn't something that you usually do "Right" on your first try. It's
important to experiment and make some mistakes to find a Script that will
work really well.
Scratch Puzzle 1.6 - 1.10: YouTube.com/watch?v=p8wvnirhPO0
Scratch Puzzle 2.1 - 2.10: YouTube.com/watch?v=p3-NO3IkQuw
13. (12)
Ibrahim Omar CS 2015
Conditional Statements
In most programming languages, including Scratch, the way that we
construct a conditional statement is in two parts:
(1) If A is true, (2) then do B.
If A is not met, B will not run.
This allows us to run different versions of a program depending on the
situations the program is trying to address.
Repeat Until
In some cases, we might not know exactly how many times we want a
series of steps to repeat, then we can use a different kind of loop "repeat
until" to build our program.
Rather than stopping after a set number of repetitions, it will simply check
at the end of each repetition whether the condition is true. If not, it will run
through the loop again.
Scratch Puzzle 3.1 - 3.5 YouTube.com/watch?v=UGMIynQ_zYA
14. (13)
Ibrahim Omar CS 2015
Costumes and Changing looks
Sprites in Scratch have a feature called costumes. This means that it's
possible for a Sprite to switch between more than one possible "look" in
the stage area.
In the case of Puzzle 3.6, if you simply move Sidney to the portal
entrance, nothing will happen, it won't teleport to the portal exit. This
portal only works if your sprite switches to its second costume (green)
before passing through (that is, before moving onto the portal).
Scratch Puzzle 3.6 - 3.10 YouTube.com/watch?v=c0lI6M87Xig
15. (14)
Ibrahim Omar CS 2015
Note: Sometimes, you may want to repeat until one of multiple things
happens (for instance, landing at either of two portal exits). In this case
you'll want to use the "or" block in the Operators tab of the blocks menu.
Scratch Puzzle 4.1 - 4.10 YouTube.com/watch?v=gQNUSHyhIRE
16. (15)
Ibrahim Omar CS 2015
Sensing Sprites
In these puzzles, though the backdrop color doesn't change between
different portal exits, so we'll need to test where the Sprite has gone
using an alternate block.
In the Sensing tab in the Blocks area, you can find the "touching __"
block. This block allows you to select a Sprite from the dropdown menu,
allowing you to test for whether the Sprite is coming into contact with a
specific portal or obstacle.
Sensing Sprites
The "broadcast" block, found in the Events section of the Block area,
allows Sprites to send messages to each other while a program is
running. This can be useful to schedule the order in which multiple sprites
should move or interact, as well as to indicate the "statuses" of sprites
as they reach different stages of a program.
Scratch Puzzle 5.1 - 5.5 YouTube.com/watch?v=8mXJZ20xIEY
17. (16)
Ibrahim Omar CS 2015
After Week (4) Projects in Scratch, you'll know a lot more about how
to build your own projects in Scratch.
Scratch 1.1 - 5.10: https://scratch.mit.edu/studios/1401512
Scratch Puzzle 5.6 - 5.10 YouTube.com/watch?v=yYpmQAoAfBI
18.
19. (17)
Ibrahim Omar CS 2015
Week (3)
Data and Codes
Defining Encoding
Encoding is simply the process of changing information from one form to
another, either to communicate it or to store it for later use. Encoding can
take many forms and be used for many reasons, many of which tie into
computer science.
Encoding Letters to Numbers
This simple encoding matches up each letter in the alphabet to a number,
starting with A = 1, B = 2, C = 3, .… all the way to Z = 26.
For example, if we wanted to encode the word ROBOT = 18-15-2-15-20
Then, if our friends received the message 18-15-2-15-20, they could
reverse the method to find the letter that corresponds to each number,
getting the word ROBOT again. This process of Reverse Encoding is
called Decoding.
9 + 3-1-14 + 4-5-3-15-4-5 = I Can Decode
Dashes "-" separate characters in the same word and plus signs "+"
represent spaces between words.
HARVEY MUDD COLLEGE
= 8-1-18-22-5-25 + 13-21-4-4 + 3-15-12-12-5-7-5
OR = 8-1-18-22-5-25 13-21-4-4 3-15-12-12-5-7-5
20. (18)
Ibrahim Omar CS 2015
Computers use character encodings such as ASCII (American Standard
Code for Information Interchange) to represent a large number of
characters using numbers. Encodings like this are important for
computers to be able to send, receive, and store our information.
The table below shows the number codes and "Hex" codes for the
first 128 ASCII characters. You'll notice that ASCII includes separate
numbers for upper and lower case letters as well as for small numbers,
symbols like & and #, and some "characters" that only have meaning
to the computer. This way, computers can turn complicated messages
into just a series of numbers to store, send or interpret.
21. (19)
Ibrahim Omar CS 2015
Cryptography
Cryptography is the art of creating, using or cracking these sorts of
codes, which are intended to allow people to communicate securely.
Computers have entered widespread use, cryptography has become a
really important area of work. People have more and more private
information available online, whether through banks, email, or social
media. On the other hand, computers are getting faster and faster at
cracking codes, making it more and more difficult to protect that
information.
So, people who study cryptography use a combination of computer
science, math and engineering to better understand how to keep our
information safe.
Key Words:
Cipher: a series of instructions on how to encode or decode private
messages.
Encryption: the process of encoding a message to make it no longer
readable by the public.
Decryption: the process of decoding an encrypted message to read its
original content.
The Caesar Cipher
The Cipher was simple: he would take each letter of his message, then
replace it with the letter that came three after it alphabetically. For
example, he would replace an A in his original message with a D in his
encrypted one, and would replace a B with an E. If he reached the end of
the alphabet, he would simply restart the alphabet again after Z, so that
X, Y and Z would become A, B and C in his encoded messages.
22. (20)
Ibrahim Omar CS 2015
Today, we call any cipher like this one a Caesar cipher, where each
letter of the message is replaces by the letter some number of places
away in the alphabet. It's an example of a Substitution Cipher, where
small pieces of text (in this case, single letters) are replaced by other,
enciphered pieces of text (in this case, other letters).
If our cipher has a Left-Shift of 2, what do we get when we encode
"LATIN"? JYRGL
If our cipher has a Right-Shift of 3, what do we get when we decode
"VSB"? SPY
While the Caesar Cipher was very effective 2,000 years ago, it is nearly
useless for keeping messages secret today. With the help of computers,
any Caesar Cipher-Encoded message can be deciphered almost
instantly, with very high accuracy.
You can try testing out some Caesar Cipher Decryption tools:
Like this: http://xarg.org/tools/caesar-cipher
The reason? When it comes down to it, there are really only 26 possible
versions of the Caesar Cipher using the English Alphabet, a computer
can do this instantly, creating the 26 possible decoding of the message.
23. (21)
Ibrahim Omar CS 2015
Representing Information with Numbers
In order for a computer to store or use any information - not just text, but
also images, sound and anything else that happens on your computer
is eventually reduced down to a series of numbers, which can then be
interpreted by the machine to produce the right combination of colored
lights, sounds and processes.
And doesn't stop there; your computer can only use two digits (0 and 1)
to represent any information it uses. So, how are computers able to
represent so much with such small building blocks?
Number Bases: Decimal (base ten)
When you first learned to count, you probably learned the decimal
number system, also known as base ten. In base ten, each digit of our
number can be one of ten different options: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
Then, the amount that each digit represents depends on where it's placed
in the number.
The rightmost digit of a number is what we call the "ones" place, with the
"tens" place to its left, and then the "hundreds" place to the left of that.
We can find the next place value to the left by multiplying the current
place value by 10 and so on.
3 4 9
= (3 x 10 x 10) + (4 x 10) + (9 x 1)
= (3 x 100) + (4 x 10) + (9 x 1)
= 300 + 40 + 9
The number can be split into parts by multiplying each digit by its
place value (one, ten, one hundred); adding the results back together
gives the total number.
3 4 9
= 300 + 40 + 9
= (3 x 100) + (4 x 10) + (9 x 1)
= (3 x 10 x 10 x 1) + (4 x 10 x 1) + (9 x 1)
24. (22)
Ibrahim Omar CS 2015
So, why do we use tens so much in our number system? Mostly for
one simple reason:
Throughout History, people have used many other bases other than
base ten for math, For instance, the Ancient Mayan Civilization used a
base twenty counting system and the Babylonians used a base sixty
counting system. Nowadays, pretty much every person uses base ten.
Computers don't use hands to count; they use electricity. And from the
perspective of a computer, it's much easier to measure whether
electricity is "on" or "off" than to check ten different possible amounts
of electricity that could pass through its circuits.
Number Bases: Binary
So, computers use a base two counting system, which we call binary.
This way, there are only two digits to worry about: (0 and 1), where 1
represents a circuit that's "on" and 0 represents one that's "off".
Because the majority of people have ten fingers to count with. However,
the number ten is a choice and not the only one we can make.
25. (23)
Ibrahim Omar CS 2015
Representing numbers with Binary
As decimal numbers, each of the digits in our binary numbers is going
to represent a place value. We call these digits bits in binary numbers,
and each one can either be a 0 or a 1.
The rightmost digit (bit) is still the "ones" place, but since we're using
base 2, we'll find the next place to its left by multiplying the place value
by 2 instead of 10:
1 x 1 = 1,
1 x 2 = 2,
1 x 2 x 2 = 4,
1 x 2 x 2 x 2 = 8, and so on.
So, if we have a six-digit binary number like the one below which
represents forty-two, we can denote the place values as powers of 2:
101010
= (1 x 2 x 2 x 2 x 2 x 2) + (0 x2x2x2x2) + (1 x 2 x 2 x 2) + (0 x2x2) + (1 x 2) + (0x1)
= (1 x 32) + 0 + (1 x 8) + 0 + (1 x 2) + 0
= 32 + 8 + 2
= 42
26. (24)
Ibrahim Omar CS 2015
Computers tend to store numbers in sets of eight bits, called a byte,
which can represent any decimal value from 0 to 255 (256 patterns).
2 bits = 4 patterns (0 - 3)
3 bits = 8 patterns (0 - 7)
4 bits = 16 patterns (0 - 15)
5 bits = 32 patterns (0 - 31)
6 bits = 64 patterns (0 - 63)
7 bits = 128 patterns (0 - 127)
8 bits = 256 patterns (0 - 255)
9 bits = 512 patterns (0 - 511)
10 bits = 1024 patterns (0 - 1023)
With ten bits, you can count up to 1023. That's why computer scientists
think that 1024 is a nice, round number.
In fact, on a computer, one k is 1024 patterns (0 - 1023) not 1000.
You can count on binary on your hand. Each finger either up (1) or
down (0). So you can count from (0 - 31) on one hand and with two
hands, you can count to 1023.
Binary Counting with Dots: YouTube.com/watch?v=b6vHZ95XDwU
For each bit that we add, we double the range of numbers:
1 bit = 2 patterns (0 - 1)
27. (25)
Ibrahim Omar CS 2015
Binary: 0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010.
Decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10.
So, the same rules of counting apply with our different digits. These same
rules govern any counting system with any number base, as long as it's a
whole, positive number.
Binary-Decimal Conversion
There are many ways to convert from decimal to binary, but perhaps the
easiest is what we call "greedy" method, which we'll demonstrate below.
Let's start with the number 21 in decimal. The largest place value that is
not larger than 21 is 16 or 2 x 2 x 2 x 2 x 1 (fifth place from the right).
1 _ _ _ _
Then subtract 16 from 21 to get 5. Then, we repeat the process: the
largest place value that is not larger than 5 is 4 or 2 x 2 x 1 (the third
place value from the right).
1 _ 1 _ _
We subtract 4 from 5, and have only 1 left over. The largest place value
that is not larger than 1 is just the ones place on the far right, so we'll
put a 1 there.
1 _ 1 _ 1
Then, if we subtract 1 from 1, we're left with 0. So, we can replace every
unused space with a 0.
1 0 1 0 1
The digits we've put in place should add up to 21. Let's check:
= (1 x 2 x 2 x 2 x 2) + (0 x 2 x 2 x 2) + (1 x 2 x 2) + (0 x 2) + (1 x 1)
= (1 x 16) + 0 + (1 x 4) + 0 + (1 x 1)
= 16 + 4 + 1
= 21
28.
29. (26)
Ibrahim Omar CS 2015
Week (4)
Projects in Scratch
Scratch from Scratch
Computer Science in general and programming specifically, is as much
about creativity as it is about science.
Computer Science can be used to solve big math problems, but also it
can be used to animate movies, play music or run your favorite games.
Then, click the "Create" button to open up a new Scratch project.
However, we'll also remind you where to find certain blocks and
buttons as you go.
In Unit (2), we practiced using the basic tools of Scratch Programming
to navigate a series of puzzles. Now, we'll put those tools and more to
create our own projects, so now is a good time to open
https://scratch.mit.edu in a new tab.
If you like, you can walk through Scratch's built in tutorial to refresh on
the basics of using the Scratch Studio. Also you can see these Tutorial
Videos: https://scratch.mit.edu/help/videos
30. (27)
Ibrahim Omar CS 2015
After clicking "Create" you should end up at a page that looks like this:
As with the puzzles in Unit (2), we'll be clicking and dragging blocks
from the blocks menu in the middle of the screen to the Scripts Area
on the right in order to control what happens in the Stage Area on the
left.
Now, we only have one sprite on our stage, known as the Scratch Cat.
We'll start out by writing programs for this cat, then learn about adding
new sprites and programs.
"Hello, world!"
Computer Scientists often start learning a new programming by creating
a program that prints "Hello, world!" This exercise will explain how to
write a Hello World program in Scratch.
We use the "say" block in the Looks tab to control what our sprite is
saying. Create a script with a green flag block and attach a "say __"
block to get started. Then, type "Hello, World!" into the say block to
control what the cat says when the program runs.
Finally click the green flag at the top-right corner of the Stage Area to
watch the cat says your message.
31. (28)
Ibrahim Omar CS 2015
Animating "Hello, world!"
Now, we'll add some animation to our Hello World Script. The script
below moves the Sprite to the side of the stage and Decreases its size.
The "go to x, y" block can be found in the Motion tab, where the two
numbers refer to a point somewhere on the Stage. In this case, x:-213
means that the sprite will be on the far left side of the stage and y:0
means that the cat will be neither above nor below the middle height of
the stage.
The "set size to %" block can be found in the Looks tab, and will cause
the Sprite to shrink to 40% of its original size when it runs.
32. (29)
Ibrahim Omar CS 2015
Now, add another Motion block that makes the Script glide to the
position x = 0, y = 0 over 2 seconds.
This is different from the "go to" block because instead of jumping from
one location to another, the sprite will move gradually between the two
over the given amount of time.
Then, after the motion block, we can add a wait block from the Control
tab to let the cat rest for a second before saying "Hello, world!"
Let's add one bit of code to change the size of the sprite as it moves.
Since blocks in a single script execute in order and we want this size
change to occur at the same time as something in the other script, we'll
need a second script that will change the size of the sprite inside a
repeat.
As a reminder, the "repeat" block can be found in the Control tab and
will run the steps it contains in order however many times are indicated to
right of the word "repeat." In this case, the step that will be repeated 60
times is increasing the size of our sprite by 1, making it look like the
sprite is gradually growing. This "change size" block can be found in
the Looks tab.
33. (30)
Ibrahim Omar CS 2015
We can repeat about 30 times per second, so the sprite will finish
growing after about two seconds, around the same time it finishes
moving across the stage.
Making a Story
We are going to make the following Story:
34. (31)
Ibrahim Omar CS 2015
You'll find that the Duck and Cat have completely separate Script areas.
Click on each character to see their script area.
(1) To make a new sprite - click on the small character icon above
the sprites area.
(2) These will be helpful to get the characters facing each other. Try to
figure out what each command (left) and button (right) does.
35. (32)
Ibrahim Omar CS 2015
Cat's Scripts:
Duck's Scripts:
Press the green flag to start the short Story.
(Note): You'll notice that we chose to name the messages that were
broadcast so that it would help us keep track of what we were doing. We
recommend you do this in your projects.
36. (33)
Ibrahim Omar CS 2015
Sound
You should feel comfortable with a variety of Scratch sound blocks and
with using the repeat block to make your code more concise.
Basic Sound Blocks
The play note block is one of the most important sounds block in
Scratch. This block plays a note for a certain duration. You can change
both the note that is played and its duration.
Another important sound block is the play drum block. This block will
play the drum you select for the specified duration.
The rest block is a third important sound block. Its only variable is the
duration of the rest.
Try making a song using these blocks.
Tempo
Tempo is a variable that controls how many beats are played in a
minute. A higher tempo increases the speed of music that is being
played. So, the same song played at a higher tempo sounds faster.
You can see the current value of the tempo on the stage by clicking
the checkbox next the tempo variable in the Sound tab.
37. (34)
Ibrahim Omar CS 2015
Setting and Changing Tempo
The tempo can be changed using two different blocks, the set tempo
block and the change tempo block.
When using the set tempo block, it doesn't matter what the current
tempo is. It will set the new tempo to the number you type in.
When using the change tempo block, it does matter what the current
tempo is. It will add or subtract from the current tempo.
Volume
Volume works the same way as tempo!
Use the checkbox next to the "volume" block to always see the volume.
Use the set volume block to reset the volume to the level you want.
Use the change volume block to increase or decrease the volume by
a certain amount.
38. (35)
Ibrahim Omar CS 2015
Designing Games
In this section, we'll try building a few example games together, which
we'll call the target game and the helicopter game. Then, you can use
the skills from those games to develop your own projects.
Basic Target Game
In this first exercise, you'll create a game where a (flying or swimming)
sprite tries to reach a target while avoiding an enemy. The next few
pages will give examples of possible additions to this basic game.
Flying
First, you'll create a new Scratch project and add a flying character to
your new Scratch project. Just under the stage area "New Sprite"
followed by Click the "Choose Sprite from Library" button to add a
sprite from Scratch's collection of pre-made sprites.
Choose a new sprite, preferably one that either flies or swims. Then,
add blocks like these that allow your sprite to move around the screen
using the four arrows keys:
After you've tested that your sprite moves as expected, add the
following scripts so that the sprite can jump to the left side of the
stage by moving to the right edge, effectively "wrapping around" the
stage area.
39. (36)
Ibrahim Omar CS 2015
These scripts will wrap for a sprite to opposite side when moving
into the side edge. You can add wrapping scripts as you see fit.
Then, add an enemy sprite. Your Hero will want to avoid this enemy.
We want the enemy to move continuously as it tries to prevent the
hero from reaching its goal.
Add this script to your enemy to allow it to move continuously:
Start Location
We want our hero sprite to always start the game at the same location
and to teleport back to that same location each time it runs into an enemy
sprite. We'll use the position x:-170, y:-140 as our start location.
40. (37)
Ibrahim Omar CS 2015
The first block of the script makes sure that when the program
starts, the sprite moves to the starting location.
Then, the "forever" block below will continue to check whether the
hero sprite is touching the enemy sprite for as long as the program
is running. If it touches the Enemy, it will go to the same starting
location as in the first block.
Customizing Your Target Game
Now, you're ready to customize your game. Add at least two of these
features:
Add a changing score variable.
Add more enemies.
Have the enemies move unpredictably.
Add "power-up" sprites that change your hero's size or speed.
Add portals that teleport your hero randomly.
Have the game end when a timer runs out.
Keeping Score
Click on the "Data" tab and then click the "Make a Variable" button.
Name our new variable "score".
Then write a script that checks if the hero is touching the goal. If it is,
then increase our score variable using the "change score by 1" block.
41. (38)
Ibrahim Omar CS 2015
If you try running the game with this previous script, you'll see that the
score increases way too fast!, To fix this problem, add a block that
waits to add a point to the score until the hero is no longer
touching the goal.
This block should go inside previous script in "if" block before the
"change score" block. This way, we'll only add one point each time
the hero touches the goal.
Making the Enemy move Randomly
To make the game a little more interesting, we'll make our enemy
move unpredictably around the stage area.
Adding a Timer
Scratch has a built-in timer to count seconds from the "Sensing" tab.
To activate the timer, click on the checkbox next to the "timer"
block, you may want to include a "reset timer" block after "when flag
clicked" to make sure that the timer starts at 0 when the game starts.
We'll need to add a script that ends the game when the timer reaches a
time limit. We'll use the "wait until" block, which pauses the script
after a certain time, for example: 20 seconds.
42. (39)
Ibrahim Omar CS 2015
To let the player know that the game is over, you may want your sprite
to say "Game over!" before or after stopping the program.
Adding Power-up
We'll show how to add a power-up, which will appear in a random
location on the stage, then disappear after the hero claims it. After a
few seconds, it will reappear in a different location. Power-up will
increase the score by one point when the hero touches it.
As soon as the game starts, the power-up will immediately become
invisible and teleport to a random spot in the stage area. Then,
after 2 seconds, it will appear, and will wait in place until the Hero is
touching it.
At that point, it will add one point to the score, then disappear
again for another two seconds as it jumps to another random
location. The forever loop ensures that the power-up will keep
regenerating after 2 seconds for as long as the program is running.
43. (40)
Ibrahim Omar CS 2015
Helicopter Game
We're going to start working on a new project, building a game with a
"Helicopter" that flies. After finishing the project, the Helicopter will be
able to fly to the right and the left and land on a platform. It will also be
affected by gravity.
(1) Start by making a new project.
(2) Open up a new sprite by click on the button.
In the Sprite Library, under the "Transportation" category double click
on the "Helicopter".
Then, write a script for each of the following, you can use the Target
game as a reference:
When you press the left arrow, make the Helicopter move left.
When you press the right arrow, make the Helicopter move right.
44. (41)
Ibrahim Omar CS 2015
Switching Directions with Costumes
The Helicopter might look a little funny flying left while it faces right.
Make a new costume for the helicopter where it faces the correct way
by: Click on the "costumes" tab.
(3) Then click the "Flip Left-Right" button so that the new costume is
now facing the other direction.
Now you have 2 costumes for your helicopter like the picture below.
45. (42)
Ibrahim Omar CS 2015
Use the new costume you just made when the helicopter flies left.
This block might be helpful:
Gravity
Whenever the up arrow is not pressed, the helicopter should float
down like is being pulled down by gravity.
Try using a forever block to always change the helicopter's y value by
some small negative number. For example: - 0.5
Platforms
In this step, we'll make the helicopter stop falling when it lands on
platforms.
Make sure you have 2 points that mark the bottom of your helicopter
sprite as (2 green dots you see in the picture). This should be a color
that you're not using in any of your other sprites.
46. (43)
Ibrahim Omar CS 2015
Make sure you add the dots to both the left-facing and right-facing
costumes.
You also need to have ONE platform sprite, make a new sprite using
the "Paint New Sprite" button shown below. The platform should look
something like the one below (it can be any color).
Now add this Script to your helicopter. The first color is the color
of your dots, the second color is the color of your platform.
When you click on the color, you will be able to choose any color on
your screen by clicking on that new color with your mouse.
47. (44)
Ibrahim Omar CS 2015
Adding Features
It's time to add some obstacles to your helicopter game. Here are some
examples of the many possible ideas:
The helicopter can land on other platforms.
The helicopter can collect prizes to gain points or speed boosts.
The helicopter is escaping from some sort of enemies.
The helicopter loses points if it hits lava at the bottom.
The helicopter loses points if it is hit by moving bird.
The helicopter falls faster if it touches a black hole.
The helicopter spins out of control if it hits a rock or when a rock
falls on it from above.
Note: If the rock is going to move, it has to be a Sprite. There is a rock
Sprite in the "Sprite Library" under the "category Things". If the rock
is stationary, it can be drawn on the stage background.
No limit for innovation, you can add any other tools in your game.
The Shark (My 1st Scratch Game): scratch.mit.edu/projects/71581906
48. (45)
Ibrahim Omar CS 2015
Week (5)
Problem Solving and Algorithms
What are Algorithms?
When we solve problems with computers, we want our tools to work for
not just one specific problem, but a whole category of problems. In order
to make a tool like this, we have to make a step-by-step procedure
called an algorithm.
Think of an algorithm as a set of instructions that we can use to solve
any version of a particular kind of problem. For instance, long division is
an algorithm that we use to divide one number by another. It doesn't
matter two numbers we use; if we follow the steps of long division, we'll
find the correct quotient at the end of the process.
Humans and computers alike use algorithms to solve all sorts of
problems. Press on the video link below, created by TED-Ed and
Harvard CS Professor David J. Malan, to see an example of how
algorithms work: https://www.youtube.com/watch?v=6hfOvs8pY1k
Problem Solving Techniques
To create good algorithms, there's a lot that goes into the design of a
good algorithm. It can't be just set of instructions: it should be clear
and relatively quick, and it should work every time for the category of
problems it's designed to solve.
49. (46)
Ibrahim Omar CS 2015
These questions are a good starting point to know what our
algorithm is supposed to do:
1) What information are we given? While each version of a problem
may have its own specific information, the categories of information
provided in each version of the problem should be the same. For
example, in an addition problem, we start with a list of numbers to add
together. These are the inputs to our addition algorithm and the output
will be their sum.
2) What should our solution look like? We might be looking for a
number, a word, a list of words, or maybe just an answer to a "True"
or "False" question. Our algorithm should end with us finding or
creating this solution.
3) How do we know our solution is correct? To answer this question,
we have to understand the rules of our problem that our solution
will need to follow. For instance, if we successfully solve a Rubik's
Cube, then each side of the cube will be entirely one color. We can use
these rules as a way of checking our answers for our algorithm.
4) What are the simplest examples of this problem? If we try to create
an algorithm to find the tallest person in any group of people, we
should start by considering the easiest versions of this problem.
For instance, if our "group" of people only has one person, then that
person is guaranteed to be the tallest.
5) What are the trickiest examples of this problem? To make sure our
algorithm works on every version of our problem, we need to think
about the versions of the problem most likely to make our
algorithm mess up. For our addition problem, what do we output if
we're asked to add up a list with no numbers in it? For our tallest
person problem, what answer to we give if there are two people of the
same height? Our algorithms may need special instructions for
dealing with these trickier problems.
50. (47)
Ibrahim Omar CS 2015
Problem: Handshakes
We can use algebra to represent the number of handshakes at a party
with n guests.
You have to shake (n – 1) hands، because you're not shaking your hand.
Every person at the party needs to shake n minus 1 hands, because
everybody has to shake everybody else's hands.
We have n people that need to do this.
So we multiply this by n. So we have n times (n – 1).
However, we over counted, because handshakes go two ways. We
counted going one direction and again for the other direction. To account
for that, we just divide the whole number by two and that's our answer.
The Answer = n (n – 1)/2
If we have 8 guests, how many handshakes will a correct
algorithm require?
The Answer = (8 x 7)/2 = 28 handshakes
51. (48)
Ibrahim Omar CS 2015
Search Problems
As computers and the internet have grown more popular and more
powerful, there's been a surge in the amount of data and information
that can be found. But with all of this information, how do we find what
we are looking for?
Linear Search: https://youtube.com/watch?v=kjurNV5Ffv0
Inputs: a target (the item that you want to find), and a list of items to
search through.
Instructions:
1. Start at the beginning of the list.
2. Check the first item on the list to see if it's what you're looking for.
If it is the right item, you're done.
If not, move to the next item on the list.
3. Continue to check the items in order until either you find your
item or you reach the end of the list.
This algorithm can work well when you don't know anything about how
the list is organized, but when lists get big, this method becomes slow.
The simplest search algorithm is linear search. Here's how it works:
52. (49)
Ibrahim Omar CS 2015
Binary Search: https://youtube.com/watch?v=iDVH3oCTc2c
If we know the way that items on our list are organized, we can take
advantage of that information to make searching much faster.
To use the binary search algorithm, we need to know the rule that the
list that we're searching has been sorted based upon it. This could be
alphabetical order, numerical order or something else entirely. If we
compare any two items from our list, we'll know from our rules which one
comes before the other.
Imagine that our list is a dictionary, where all of the items are words
arranged in alphabetical order. Here's how it works:
Inputs: a target item (what you're searching for) and a sorted list.
Instructions:
1. Start at the item half-way through your sorted list.
2. Compare this center item to your target.
If the two items match, you're done.
53. (50)
Ibrahim Omar CS 2015
If your target item should come before this center item, then your
item won't be in the second half of the list. Ignore the second half
and move to the center of the first half of your list.
If your target item should come after this center item, then your
item won't be in the first half of the list. Ignore the first half and
move to the center of the second half of your list.
3. Repeat this process of checking an item at the center of the
remaining list, determining whether the target should come before
or after the center item and eliminating the other half of the list.
4. End when either you find the target or you eliminate all items from
the list.
We only need one more additional step, when we double the
number of items in our list, because our first step will cut the list
in half, making the problem the same size as before again.
Linear vs. Binary Search
The difference between the two becomes more pronounced with larger
data sets; after all, linear search will probably take twice as long to
search through a data set that's twice as big, whereas binary
search will only require one additional step.
But binary search requires a sorted list, it's less flexible than linear
search. Not all data lends itself well to sorting and even the fastest
algorithms required to sort data are still much slower than the ones we
use to search through it. This can make linear search a better
choice on occasion, since it will work on any list, sorted or not,
without any additional work.
54. (51)
Ibrahim Omar CS 2015
Comparing Algorithms
We don't just care whether they successfully solve our problem. Often,
multiple algorithms will work to solve the same problem, but will run at
different speeds and sometimes even give back different correct answers.
Many computer scientists focus their work on algorithms, answering
questions such as:
need to solve it?
Sorting Problems
In this section, we'll be looking at different algorithms that can be
used to sort objects. Just like with searching, some algorithms perform
faster than others; we'll compare them as we go.
Can this problem be solved with an algorithm.
How good are the results that this algorithm finds.
How quickly does this algorithm solve the problem.
Is there another algorithm faster than this one? How much faster.
How much does the size of our problem affect the amount of time
55. (52)
Ibrahim Omar CS 2015
Thinking about Sorting
In general, computers must sort by comparing only two elements at a
time, without being able to assume much about the rest of the list. Good
sorting algorithms try to use as few of these comparisons as possible to
sort a list, but we can't take shortcuts by eliminating part of the list:
even the fastest ones will still have to look at every element of the
list at least once and usually multiple times.
In this algorithm, computer repeatedly searches through the unsorted
list to find the smallest element, then puts that element at the
beginning of the list before checking for the next smallest element.
Inputs: an unsorted list and a method for sorting them (in this case,
in increasing numerical order).
Instructions:
1. Start at the beginning of the list.
2. Compare the first two numbers in the list. Whichever number is
smaller will be our current minimum.
3. Repeatedly move down the list one space and compare our
current minimum to the next element in the list.
If the next element is smaller than our current minimum, we
replace our current minimum with the next element before moving
down the list.
If the next element is larger than our current minimum, we hold
onto our minimum and move down the list.
4. When we reach the end of the list, we move whatever element was
smallest to the beginning of our list.
1. Selection Sort: YouTube.com/watch?v=92BfuxHn2XE
56. (53)
Ibrahim Omar CS 2015
5. We repeat steps 1-4 with the unsorted part of the list, leaving the
earlier minimum values at the far left. Once every element has
been moved to the sorted list on the left, we're done.
This algorithm is called Selection Sort because each time we pass
through the unsorted list, we select precisely one element to put next
in our sorted list.
In this algorithm, the computer "marches through" the unsorted list
and compares only numbers that are right next to each other, two at a
time. If the numbers are out of order, it switches them.
Inputs: an unsorted list and a method for sorting them (in this case,
in increasing numerical order).
Instructions:
1. Start at the beginning of the list.
2. Compare the first two numbers in the list.
If the first number is smaller than the second number, do nothing.
If the first number is larger than the second number, have these
two numbers trade places so that the smaller one comes first.
3. Repeat step 2 until you reach the end of the list, then start again
at the beginning of the rearranged list.
4. Repeat steps 1-3 until you can pass through the entire list without
switching the locations of any numbers. Then, you're done.
This algorithm is called Bubble Sort because the largest number
"bubbles up" from the unsorted list on the left into the sorted list on
the right.
2. Bubble Sort: YouTube.com/watch?v=Cq7SMsQBEUw
57. Ibrahim Omar CS 2015
3. Merge Sort: https://youtube.com/watch?v=ZRPoEKHXTJg
It works by splitting the list you want to sort in half and sorting each
half individually.
Those halves will be sorted the same way, by dividing the quarter
lists in half and so on until every "half list" is just a single element.
Then, these elements get merged back together into sorted lists.
Inputs: an unsorted list and a method for sorting them (in this case,
in increasing numerical order).
Instructions:
1. If the list is only one item long, it's sorted.
2. If the list has more than one item, split it in half and sort each half
using Merge Sort.
3. Then, repeatedly compare the first elements of each half, moving
the smaller one to the left side of a final, sorted list.
4. When all of the elements of the two halves are added to the final,
sorted list, the algorithm is done.
This algorithm is called Merge Sort because we sort the list by
repeatedly merging two independently sorted lists. Merge Sort can
seem a bit magical in how it works, since it's not clear at what step
elements are being put into the correct order.
The idea behind the algorithm, though, is very similar to what makes
Binary Search so effective: because we halve the size of the list
repeatedly, we don't need to make as many comparisons, meaning this
is a much faster algorithm.