And fun.
So, let's build an app.
Starting with...
A set of instructions.
It's just a fancy word for...
It's useful for not repeating yourself
fill('red')
fill('red')
stamp('eagle')
fill('red')
stamp('eagle')
sound('fox')
stamp('cat') //In the middle
stamp('cat', 1000) //In the middle, but bigger
stamp('cat', 200, 300, 50) //At X 200, Y 300, size 50
What happens when you tap the screen?
function tap() {
stamp('pig', x, y)
}
What happens when you drag your finger or mouse on the screen?
function drag() {
stamp('flare', x, y)
}
function loop() {
stamp('splat', 50).move()
}
What should happen over and over again and again?
// Create...
// Name the function anything you want
function newPet(pet) {
stamp(pet).move()
}
// Then use...
newPet('cow')
newPet('pig')
newPet('duck')
newPet('elephant')
newPet('dolphin')
// Typing the same long thing in
// over and over again
stamp('cow').move()
stamp('pig').move()
stamp('duck').move()
stamp('elephant').move()
stamp('dolphin').move()
// Now make your pets sing
function newPet(pet) {
// Just add a .sing() to the end
stamp(pet).move().sing()
}
// No changes needed below
newPet('cow')
newPet('pig')
newPet('duck')
newPet('elephant')
newPet('dolphin')
// Here we need to add .sing() 5 times
// Imagine if you had an entire zoo!
stamp('cow').move().sing()
stamp('pig').move().sing()
stamp('duck').move().sing()
stamp('elephant').move().sing()
stamp('dolphin').move().sing()
Create a variable name,
put something in there,
do something with it
myPet = stamp('cat')
// We now have a variable called myPet
// Let's do something with it
myPet.sing()
function tap() {
// We can change the type of pet to something else
myPet.change(prompt('What kind of pet do you want?'))
myPet.sing()
}
What to do when something is true
n = random(10)
if (n < 5) {
text('It was a low number')
} else if (n == 10) {
text('It was 10')
} else {
text('It was between 6 and 9')
}
Make something be something
Don't worry about it :-)
name = 'Derek'
//Now name is Derek
Check if something is the same as something else
winningScore = 10
if (score == winningScore) {
text('Hooray! You Won!!!')
}
Are both things true?
n = 10
if (isEven(n) && isTen(n))
n = 8
if (isEven(n) || isTen(n))
n = 8
if (isEven(n) && !isTen(n))
Is the thing NOT true?
Are either thing true?
( Parens ) are like bubbles. They keep things together.
{ Curly braces } are like walls. They keep things together.
What are all the things you want to happen when a player wins your game?
// If the player won
if (score == winningScore) {
text('Hooray! You Won!!!')
stamp('treasurechest')
sound('claps')
song('fanfare')
}
What are all the things you want to be true?
// If the player won
if (
( day == 'Wednesday'
|| day == 'Friday'
)
&&
( month == 'Oct'
|| month == 'Feb'
)
) {
text('Hooray! You Won!!!')
}
number = 1
function tap() {
circle(x,y,5,'onyx')
text(number,x+10,y,30,'blue')
number = number + 1
}
function drag() {
circle(x,y,5,'sea green')
}
text('By Derek', 650, 980, 20, 'blue', 'console', RIGHT)