Tuesday, December 21, 2010

Scratch Programming 2


Click the green Flag to start the above Scratch Animation. Let it run through to completion.

This animation uses two picture characters, (or "Sprites"). They are the Duck and the Hippo.

We got the background image off the internet, saved it to our hard drive, and used Photoshop to get its size to be the standard required 480x360 pixels. We then clicked on the Background icon in Scratch, and imported it into our project.

The duck speaks just after the Hippo starts to take off and fly.
How we organise this in Scratch, is for the Duck to send a "Broadcast Message" which we can then get the Hippo to receive. (If you know Flash, then it is a bit like event listener programming in Flash Actionscript.)

It is through the broadcast message that we can sync up the duck and the hippo.

Here is the Scratch script for the Duck sprite:

Script for Duck

When the hippo flys off the screen, the ends of his legs were staying stuck on the screen, and so we had to use the "hide" command to make him dissapear for a few seconds, and then we reset his position to be next to the duck, and then make him visible again.

Here is the Scratch script for the Hippo sprite:

Hippo Code


Keeping Score

This next Scratch project is straight from the MIT "Keep Score" Scratch Card:

Keep Score Card

Note that we had to press "Record" on the Play Sound block, then use Import, but we could not find the "chomp" sound, so we used Effects > Pop sound instead.

We also added a blue "If on Edge Bounce" block because the Shark kept going off screen. In addition, we right clicked on each sprite, chose "Resize this Sprite", and made both fish smaller. We also named the Sprites as "smallfish" and "shark".

Here is the final script for the Shark:

Sorry Picture Not Found

We added some extra code so that the small fish would momentarily dissappear when hit by the shark, as well as some random number coding to make the small fish respawn at a different new location each time.

Here is the final script for the small fish:

Sorry Picture Not Found

To play the finished Scratch animation, click the top right hand corner green flag. To stop the game click the top right hand corner red dot stop sign.



Shark Versus Fish Game

Finally here is a far more sophisticated version of the Shark versus Fish game. This was made here at Passy World using some ideas from Shark Games that were found on the MIT website.

If you join up with the MIT site at http://scratch.mit.edu/ then you can create an account, search for Scratch Projects, and download the programming source scripts etc.

First of all the game has costume changes on the shark, so that it is chomping with its mouth the whole time it moves:

Sorry Picture Not Found

We then have scripting on the fish and Shark sprites so that after the shark eats the first fish, the fish starts cloning itself. The fish can then earn points if it can clone for a while without being eaten.

Each time it does get eaten, the clones erase, and the cloning recommences. The Game has a timer on it to have a total game time of 30 seconds.

Here is the scripting for the Shark:

Shark Code

Here is the scripting for the Fish:

Sorry Picture Not Found

And here is the finished game for you to enjoy playing. Click the green flag to start the game, and you then use the mouse to move the shark around chasing the fish. You have 30 seconds to win more points than the fish wins by cloning itself.


Note that there is a glitch when we play the game online through the HTML outside the Scratch Environment. What happens is that we don't get our final "say" messages from the Shark at the end of the game.

Sadly, we have not been able to find anyway to fix this end of game problem.
The same problem happens on the original Shark games that are on the MIT site.

So that completes another fun lesson on Scratch.

Enjoy,
Big Passy Wasabi

No comments:

Post a Comment