23
Page 1 of 23 BUILD AN AQUARIUM IN SCRATCH The Story – Piggo & Cowey are underwater and want to build a fish tank to bring their fish friends back home from the sea! Learning Goals – Reading, Math, Science, Logic and Programming, Scratch Functions used here - Events, functions, sensing, operators, looks, pen, motion. Key Game Components Mastered Learn how to create a button Use Keyboard keys and mouse to control the characters How to send/receive messages between characters Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com

01 Build an Aquarium Game

Embed Size (px)

DESCRIPTION

Learn coding - Build an aquarium uisng Scratch programming

Citation preview

Page 20 of 20

BUILD AN AQUARIUM IN SCRATCH

The Story Piggo & Cowey are underwater and want to build a fish tank to bring their fish friends back home from the sea!

Learning Goals Reading, Math, Science, Logic and Programming,

Scratch Functions used here - Events, functions, sensing, operators, looks, pen, motion.

Key Game Components Mastered Learn how to create a button

Use Keyboard keys and mouse to control the characters

How to send/receive messages between characters

Create random life like movements of characters

Automatically create copies of a character

Interact with the User Get their input and use in program

Repeat actions using conditions

Change backdrops and sound effects

Code for the Button

When this sprite clicked send a message called Message1. This message is sent to all the sprites to display on the screen (other than itself).Once clicked, the Button receives its own message and hides itself.

When Flag clicked - Since the program uses multiple backdrops/Stages, the Button resets itself to a default backdrop called Underwater2 when flag is clicked.

Code for the Well

When Flag Clicked - The sprite is hidden when the program is started. The only thing visible is the Build an aquarium button. When we click this button, it sends a message to all the sprites.

When I receive message When the Well sprite receives a message from the button sprite, it is placed at a certain spot on the screen which is created by the goto X & Y values. It then shows itself.

When this sprite clicked - When clicked once, The well follows the users mouse. The way to release the mouse from the Well, is to chose a spot and press the Space Bar Key

Code for Train

The code for the train is the same as the code for the well, the only difference is where it is placed. This difference is seen in the Go to X & Y values under the When I receive message hat block!

Code for Plant

When the flag is clicked, the button hides.

Plant like all other sprites receives message from Button to display/show itself.When the sprite clicked - We ask the user, how many plants they want for their aquarium? The answer has to be a number not text.

We create duplicates of the plant using a feature called Stamp/under the Pen blocks. We use the pick random X & pick random Y to place the plants in random places on the screen. The max range for the X axis on scratch is -240 to 240 left to right. The max range of Y in scratch is -180 to 180 from bottom to top.

We use the clear block under pen to clear the stamps every time we start building a new aquarium.

Code for Fish Net

When Flag clicked we reduce the size of the fish net using the Set size block instead of the shrink icon on the screen.

This sprite also receives its X & Y position from the Button block and shows itself upon receiving message1 from Button

When this sprite clicked tells the sprite what actions to perform when a user clicks on it.

It grows the net larger so it can clean faster

It plays the sound of bubbles to make it feel like its moving around stuff in the water

the sprite picks random X & Y positions and turns counter clockwise to clean. It picks random values to imitate the random motions a human would make to clean the tank. It uses a repeat loop to clean it 3 times.

After it has simulated a cleaning action, we change the backdrop to a brighter one to show the cleaning has taken effect.

We then send the cleaning net back to its original resting position and reset its size. We wait for a few seconds (in real life it would be a few weeks!) to make the aquarium dirty again! By changing the backdrop!

Code for Fish

Picture from Scratch Library

When Flag clicked, the fish hides like all other sprites and gets activated to show when it receives a message from the Button sprite.When the sprite clicked we want the user to tell us how many fish they want in their tank.

We use a repeat loop to create as many fish as the user wants. We capture the users answer in the variable answer already available under Sensing blocks. However the repeat loop repeats fish creation in numbers, 1 more than the what the user has asked for. To create the exact number as the user asks, we run the repeat loop Answer minus 1 times. So if the user wants 4 fish, the repeat loop will run 5 times giving us 5 fish. To correct that we run it (5-1) = 4 times!We want to place the fish in random locations like they would be in a real tank, so we use the pick random X & Y functions to place them on the screen.

The code that creates the fish is called create clone of myself. This is different from a Stamp function which also creates duplicates of any sprite. However we cannot use any motion/action on a Stamp . A clone allows us to assign it some action like a real sprite except that we dont need to duplicate the sprite.

To make a clone perform some action , we use the when I start as a clone hat block.

MoveFish is a special feature used in this program where we find code that can be reused. It is created using More Blocks Much like a pair of shoes that we reuse every time we want to walk instead of buying a new one and filling all the space in our home, a good program reuses blocks wherever possible, so it can run much faster and use less space on the computers storage space called memory.

We need to make the fish move in random order like it would do in a real tank. We also need the clone of the fish to do the exact same function of the Fish. For this reason, instead of writing the same code 2 times once for the fish and again for the clone, we created a Move Fish block one time. Then we called this block 2 times once for the Fish to move. 2 for the clone of the fish to move.

To actually move the fish, we used the same steps as Code fish net choosing random X & Y positions and tuning them at an angle and have them move a few steps at a time in a recurring loop. This time, instead of only making the fish move a few times, we made it move forever like it would in a real tank. We used a forever block to make the fish move till the game is stopped using the red button.

Code for Worm

We decided to give the fish real worms to feed on, apart from the plant. The code the worm is the exact same as the fish except

1. We have named the More Blocks function to move worm instead of Move Fish!

2. Its stationary position using X & Y axis is different so we can see the worm and fish side by side to select them to live in the aquarium.

Good luck programming! Remember programming is fun! It is for Everyone!

Until next time, keep scratching Piggo & Cowey (All the above pictures are available at wwwopenclipart.org for free. Please download from there.02 FEED THE FISH ON TIME

Story: When we created our beautiful fish tank, we brought home a really hungry fish! Our Fish is hungry all the time! We decided to create a game to feed it. We also want to see how many worms the fish can eat in a minute. If it eats a lot, then we can feed it less next time. If it eats a little bit, then we need to give it more. We want to make sure our fish gets plenty of food like it did in the sea. Enjoy playing!

Learning Goals Learn Math, Reading, Logic and Programming

Scratch Functions used here - Motions, Controls Loops, Events, Operators, Variables, Sound & Looks.Key Game components mastered

Learn to create a chase game

Keep score of worms eaten

Create a timer. Use it to compare score between players. How many worms/minute eaten?

Create communication. Send /Receive Messages to fulfill an action Create Animation for fish to eat worm

Create life like movements for worm & fish

Use conditions and loops to make the worm disappear when eaten and appear again on the screen

Backdrop Choose backdrop from Scratch LibraryCode for Worms

When Flag clicked point towards right (90). It can be any direction; we just want the worm to start moving!)

Forever Loop Show till the Stop button is clicked. We are moving the worm, again try different steps here, 2 steps is just an example. We turn random degrees to make the worm appear as natural in its movement as possible. If the worm touches the edges of the screen, we make it bounce back.

If the worms body (pink) touches the Fishs face (Orange), then we send a message (broadcast) to the fish. This means that the fish has eaten the worm. So we hide the worm. Wait 1 second and bring the worm back to life at a random spot on the screen using the random X & Y position. We unhide the worm with the function SHOW!

Code for the Fish

Picture available in Scratch Library

Data Function in Scratch - Creating Variables We create 2 variables to help us keep track of information.

To keep track of time, we create a variable called Mytimer.

To keep track of the number of worms eaten by the fish, we create a variable called Count my eatingWhen Flag ClickedSet the variables to be 0 every time we start the game. Have the hungry fish open its mouth when the game begins. If the distance between the fish and the mouse is greater than 10, move in the direction of the mouse. If the distance is less than 10, stop moving. This means that the closer you take the mouse to the fish, it will stop moving.

Create a timer for the program. Until the stop button is pressed (Forever), wait one second and increase the Mytimer variable by 1. By the end of 60 seconds. When (1 Minute), the Mytimer variable will also be 60. When Mytimer is 60, stop the game.Fish receives the message from worm, when it touches it. This means it has eaten the worm. We increase the Count my eating variable score by 1. To make it look like the fish really ate the worm, we create a costume for the fish to open and close its mouth. When it touches the worm, we change the costume to its Fish-Closed mouth closed. After less than a second, we change the costume to Fish Open, to open its mouth again. We got a hungry fish that is always looking for more worms to eat! Since we have a timer attached to the game, we can find out who can get most worms in 1 minute. The person who makes the fish eat the most worms in 1 minute (60 seconds) is the winner!

03 The Fish party!

Story The fish is having a birthday party! Its best birthday present ever a blue flute! It uses the flute to catch a lot wormy treats! There is one problem! Some humans litter the sea with all kinds of dangerous things. A war ship just exploded above the party scene and all the cannon balls are rolling down the ocean floor. Ouch, the balls are going to hurt the fish. He has to stay away from them otherwise it will destroy him. Help the fish eat the worms and escape the cannon balls.

Learning Goals - Math, Language, Science, Programming, Logic Artificial Intelligence (AI)

Scratch Functions used here Motions, Looks, Data, Events, Controls, More Blocks, Sensing, Operators and Sound.

Key game components mastered Artificial Intelligence

Code generated cannon balls chase the fish without the help of user

Generate less code more function

Reuse Code multiple times using More Blocks

Creating Clones of characters instead of copying them. Play with nested loops and complex conditions Create Random movements for characters

Use data variables to Store Score information in the games memory

All images are available at www.openclipart.org for free download.

CODE FOR FISH

CODE FOR WORMS

CODE FOR OTHER FISH IN THE AQUARIUM

CODE FOR CANNON FIRE BALLS

Get Free Coding lessons, videos & games NOW! Join us @ www.littlecodeninja.com