Preview only show first 10 pages with watermark. For full document please download

An Overview Of The Palettes A. Programming Objectives

   EMBED


Share

Transcript

Skills Training 4 A. programming objectives There are various types of projects you can create in Scratch 2.0. They include Animations, Games, Interactive Art, Music and Dance, Quizzes and Stories. You can make Video Sensing Programmes which utilise your web cam or develop your own interactive motion-capture games for external devices such as Kinect. As a budding computer programmer, you are limited only by the scope of your own imagination. Let’s review before moving on. Chapters 1, 2 and 3 introduced most of the main features and resources of the Scratch 2.0 programming environment. You should be familiar with the main Project Editor, the Costumes Panel, the Paint Editor an overview of the palettes Motion You have used blocks from 9 out of the ten palettes and have learned about the types of code blocks there are in Scratch. You have yet to use blocks from the Data palette (Variables and Lists). Before that, let's learn more about an essential element of game programming, i.e. user control of the sprite with mouse or keyboard. You will see that we need the Data palette to create scores and timers. There’s still much to practice, lots to experiment with and a whole new world of creative fun to enjoy. and blocks Events: Hat blocks (also called trigger blocks) have a rounded top and a bump at the bottom, so you can only place blocks below them. With the exception of the two broadcast blocks, they have ‘when’ in their label and they start every script. There is one other hat block and it's in the Control palette. The two Event broadcasts command blocks are explained on page 61. Motion Motion Motion Events Motion & Looks: With the exception of three reporter block in each palette, all the blocks in these two palettes are command blocks (also called stack blocks) . They have a notch on the top and a bump on the bottom so that they can be placed above and below other blocks to make a stack of script. The change effect and set effect blocks have an associated lists of effects and range of colours. These are illustrated on page 22. 52 and the three Libraries. You have learned that you are not limited to the sprites, backdrops and sounds that are presented to you in Scratch. You can import, improvise and create your own. Control: The loop blocks forever, repeat (_), repeat until are in this palette with the conditional loops if then, if else, when and repeat until. There are a few command blocks wait, wait until, stop. There are three clone blocks also. Clones are new in Scratch 2.0 and they are explained in Chapter 5, page 80. Skills Training 4 Motion Motion Motion Motion Control Sound Sound: You import sounds as MP3 or WAV files into your project from the library or from an external source. You then add the sound to the sprite from the list on the ‘play sound’ blocks. The play drum, play note for beats and set instrument sound blocks have long lists of drums beats, musical notes and musical instruments. There are 18 drum types. You can choose from 127 notes (not just the ones listed). Beginning with piano, there ae 21 musical instruments listed. There are also blocks for rests, tempo and volume controls. (See p. 16 and p. 44 for music making.) Sensing: Many of the blocks in this palette are reporters and their purpose is explained on pages 42 -43. The command blocks ask and wait and answer receive user input. These two special blocks make their appearance on page 76 and explained more fully on page 86. Pen: All the blocks in this palette are stack block commands: clear, stamp, put the pen down, lift the pen up, set pen colour, shade, size, change colour, shade, size. The Pen palette facilitates drawing and interactive art. See pages 27 to 30 for pen programming. Operators: All blocks in this palette are reporters with pointed or rounded ends. They appear throughout the book and in the Extension to Numeracy sections. Data: Variables and Lists are explained separately in the book. Variables were briefly introduced on p. 43 and in greater detail on p. 57. See lists p. 88. Motion Motion Make a Block New Block Options OK Cancel More Blocks: This palette makes it possible custom to create your own blocks for the selected sprite. The dialogue box includes options to add parameters for three types of data, input; number, string or boolean or add a text label. Type a New Block name and then define your new block. The More Blocks palette was introduced on page 48 (see p. 82 also). 53 Skills Training 4 B. user control with the Mouse-pointer 1. Drage a Sprite www.scratch.mit.edu/projects/19624640 (b) Script on Mouse sprite: 4_01 Mouse_Cheese1 Mouse To do: Create an animation where a Mouse smells and follows a moving piece of cheese. When the Mouse touches the cheese he thinks ‘Delicious’ and the programme stops. You need two sprites. Get Mouse1 from the library and draw the cheese yourself (See p.37). To move the cheese make it forever have the same location as the mouse-pointer. Script the sprites as shown at (a) and (b). NOTES: 1. For an alternative script to programme dragging the cheese see Example 4, page 43 2. The Mouse sprite is located in the bottom left corner of the stage, the opposite corner of the stage from the green flag. He delays one second to say “Mmm. I smell cheese.” These strategies give the player a chance to click the green flag and move the cheese before being pounced upon and gobbled up by the mouse. (a) Script on Cheese: Cheese Ideas: You could improve this animation using skills you have already acquired: e.g. animate the mouse’s tail, improve the backdrop, add sound (increase the tempo as the mouse closes in on you). It’s important, before closing the Paint Editor after drawing your cheese sprite, that you set the costume centre of the sprite (See Paint Editor Hints page 38). drag a sprite without scripting it Cheese x: 0 y: 0 Use library sprites to create other games e.g. cat chasing mouse, fox chasing duck, monkey chasing bananas. Same code just a different look. you have many sprites to match. You don’t need to put code on the sprites you want to drag. Code the matching sprites to react when touched. direction: 90 rotation style: can drag in player: show: In the Sprite information box, tick ‘can drag in player’, You can drag the sprite without the need to use code. This can be advantageous in drag and drop games, picture quizzes etc. where 54 apple colour red animal horse fruit Skills Training 4 C. user control with the KEYBOARD 2. Move a sprite through a maze continued www.scratch.mit.edu/projects/19624709 sprite is returned to the start! In the second loop the reporter tests for true if the sprite touching color touches the target. The sprite says “I WON!” 4_02_maze This maze is quite basic and there are several ways it can be improved, using obstacles, a timer and introducing various levels of difficulty. When you have learned more scripting skills you can return to the maze and bring about your own improvements. (1a) To do: Guide a sprite through a maze using the four arrow keys of your keyboard. To avoid walking through the walls, we use the sprite touching colour sensing reporter block. You can draw your own maze in Scratch or generate one online using a free Maze Generator such as www.mazegenerator.net 1. Draw the maze either as a background or as a sprite. If you prefer to draw your own maze, use the vector line tool and tweak the corner points with the reshape tool. The maze in the picture was generated online and downloaded to local file as an SVG graphic. Then it was uploaded to Scratch. (See Paint Editor Hints page 38 for info on file formats). x: 20 y: -158 New sprite: Stage 1 backdrop (1b) (1c) (1d) (2) Sprite 1 New backdrop Paint new backdrop Remember the dimensions of the stage are 480 x 360. Your cell width and height selections should suit the stage dimensions. My selection of 12 cells x 8 cells, fills the stage and the path is not too difficult to guide a sprite through. Place a red target at the end of the maze path. 2. Programme the sprite. I decided to use the cat sprite, but first I shrank him down in size. There are five scripts on the sprite. The 4 scripts (1a) to (1d) with the arrow keys should be easy enough to understand. Script (2) consists of two conditional loops. In the first loop the touching color reporter tests for true if the sprite touches the black walls of the maze path. The 55 Skills Training 4 c. user control with the key board 3. The keyboard events keys www.scratch.mit.edu/projects/20601820 New blocks are introduced: B. Scripts on the Ghoul: 4_03 Bat_Catcher (1) Ghoul (2a) (2b) To do: Create an animated game in which a Ghoul (or Ghost) catches bats in a spooky wood. Every time a bat touches the Ghoul it’s bagged and counts as a score. There is a time limit which adds to the excitement and makes high scoring difficult. 2 Costumes The Game Plan: The bats flit about and the player controls the movement of the Ghoul. The object of the game is to catch as many bats as possible. Gotcha! (2c) This game is a development of an earlier project from page 21. Choose the bat and Ghoul from the Sprites Library. Rename the bat as Bat1 and script as in (A) below. Test the scripts. (2d) A. Scripts on each Bat x4: (3) Bat1 Idea: Programme the Ghoul to express his satisfaction each time he catches a bat? 2 Costumes NOTES: 1. What happens to the bat to show that it is caught? It is ‘recycled’. We show the same technique to catch fish in the ‘Shark Chomp’ project page 60. The movements of the bat and the fish are programmed differently but they are scripted in the same way, to make the sprites they catch recycle. They disappear and reappear randomly. 2. There is no reason why each bat should move at the same speed. You could use a ‘pick random’ variable in place of the fixed number of steps. When you have finished scripting the bat make three duplicates. Four bats is all you need in order to populate the wood with flying creatures! 56 3. The background sound could be scripted on the stage or on one bat. It doesn’t have to be on the Ghoul. When you test your game, I expect you will be able to clear the wood of bats, if only for a second or two. You will soon see the need to keep a record of each bat that is caught. You need a variable to keep a record of the score. That’s next. Skills Training 4 D. data: variables 1. Keeping score in a game variable reporter with monitor To do: Create score variable to keep a record of the bats the Ghoul has caught. Create a count down variable that stops the clock after 30 seconds play. A. Creating the Score Variable: 1. Click the Data Palette 5. The small check box beside the score variable reporter, is known as a monitor. When it’s ticked, the variable is visible on the stage. Yes, you want to see the score so it should be ticked. Double-click the monitor to skip through the display format options. monitor display format options Score 0 Score 0 B. Count each Bat caught: Motion 1. 2. Make a Variable Make a List Stage 2. Click the stage thumbnail as we’re going to create the variable on the stage for all sprites. 1 backdrop New backdrop New Variable Add this code to the stage. This sets the Score to zero each time the game is started. Add the change score by 1 block to each bat. The bat is caught when it is ‘touching’ the Ghoul. This requires you to drag the change score by 1 block inside the conditional loop where the white indicator line appears, 2. Create a count down variable Follow the same steps and add extra code to the script on the stage. I’ve named the new variable seconds left. Variable name: Score For all sprites OK Cancel 3. For Variable name type Score for all sprites and click OK. 4.When you click OK, the Data blocks palette contains five blocks. Motion Make a Variable You set score to zero and it increases by one for each bat that is caught. You set seconds left to 30 seconds and it decreases by one as each second passes. If you want to know how to add a ‘GAME OVER’ caption when the 30 seconds is up, find out on pages 38 and 61. Test your game and have fun. Inbuilt variables in Scratch Make a List Variables are so called, because the data they store can vary. As well as creating your own variables, Scratch has many inbuilt variables (all are rounded reporter blocks) such as the x position, y position and direction blocks in the Motion palette. 57 Skills Training 4 4. Gravity chopper www.scratch.mit.edu/projects/19624973 Blocks used here for the first time: 4_04 helicopter 2. key control: simulate gravity effect B. Script 1 on helicopter (a) To do: Programme a helicopter to lift off and fly right and left. When it’s not moving forward or rising it gradually descends to land. Also, instead of bouncing at the edge of the stage it ‘wraps’ to opposite side. Draw and programme clouds, birds etc. Get creative. Draw a cloud and add the script below. The ghost effect makes the cloud 40% transparent. The ‘go to front’ block brings it in front of everything on the stage. You can see the helicopter as it moves behind the cloud. Clouds move in one direction and they don’t bounce. In this script they are programmed to move in a negative direction, right to left. At the edge of the stage they are sent back to the opposite side at a random height. Experiment with clouds, levels, transparency. Could you use costumes to give more variety than just three clouds? 1. clouds: layers, transparency A. Script on Cloud Add this script to the helicopter. You can get royalty free sound effect from many web sites e.g. www.freesfx.co.uk. (b) (c) helicopter (d) (e) Notice the down arrow is not in the script. Can you say why? (f) (g) (a) Set the starting position of the helicopter. (b) Use the up arrow key for lift-off and to move the helicopter upward. (c) If helicopter is not lifting, it drops - this is meant to simulate the pull of gravity. (d) Use the right arrow key to set the helicopter in a positive direction and move it forward in that direction. cloud (e) Change y by 2 to offset the ‘pull of gravity’ and maintain the forward movement of the chopper. (f) Use the left arrow key to set the helicopter in a negative direction and move it forward in that direction. (g) Change y by 2 to offset the ‘pull of gravity’ and maintain the forward movement of the chopper. (h) Experimentally, find out the purpose of the ‘change y by 2’ blocks (e) and (g). Leave them out and see what happens. 58 Skills Training 4 3. wrap a sprite to stage C. Script 2 on helicopter helicopter We programmed the cloud to go out left and come in from the right. The helicopter should be programmed to go off either side and approach from the opposite side because the player can change its direction on the keyboard. This effect can be achieved using the wrap to stage effect. Apply the following script to the helicopter. Let’s figure out how it works. When the helicopter reaches the left side of the stage where x = -240, the ‘less than’ operator reports true and sets x to 240 on the opposite side of the stage. The helicopter goes off stage left and reappears stage right. When the helicopter reaches x = 240 on the right side of the stage the ‘greater than’ operator reports true. The helicopter goes off stage right and reappears stage left. 4. background scrolling the visible stage at start up -478, 0 2 pixel overlap 478, 0 0, 0 background 1 sprite piggyback graphic* There are 2 Scripts on background 1 sprite background 2 sprite 1 Script on background 2 sprite (a) (b) background 1 background 2 * The 'piggyback graphic' is just a drawn image and not a sprite. The background sprites move behind it while it just sits on the stage. In this project it overlaps the join between the background sprites. Draw your own background to scroll (See page 40) 59 Skills Training 4 1. shark chomp continued opposite page e. broadcast & receive messages 5. Shark chomp Blocks used here for the first time: www.scratch.mit.edu/projects/19625064 4_05 shark_chomp 4. In the Paint Editor rename the first shark costume as shark-closed and rename the second as shark-open. Draw a dark blue line around the shark’s jaws in the shark-closed costume. The code expects the small fish to have a red outline. 5. This following script has a conditional statement which keeps the shark within 3 pixels of the mouse-pointer. It reads, if it is true that the shark’s distance to the mouse-pointer is greater than 3 pixels then move 5 steps in the direction of the mouse-pointer. Script the shark sprite’s movement as shown at (B). If you like the book x: 20 y: -158 To do: Create an animation where the Shark is controlled by the mouse pointer to capture little fishes. When he touches a fish he gobbles him up and prowls for another little fish. The ‘eaten’ fish disappears for a few seconds before reappearing elsewhere on the stage as if it was a different little fish. He’s just been recycled! B. Script 1like on Shark: (moves the shark) and would to continue beyond this point Unlike the mouse and cheese game where the player with the mouse-pointer was pursued, in this game the player with the mouse-pointer is pursuing the prey. We introduce new skills later in this chapter which will add an exciting extra dimension to this game. please buy the book Shark Costumes: 2 or contact me at [email protected]. You need two sprites. Get the Shark sprite from the library and draw the little fish yourself. 1. When drawing your own sprite, always make it face to the right. This is because the default direction is 90º facing right. Draw this fish with horizontal symmetry so that it looks no different when upside down. In script (1) we can set its rotation style to full rotation and make it twist and turn in all direction. Make him a lively little fellow! C. Script 2 on Little Fish: (collides with shark) Click Slot into go to x: Slot into go to y: BUY A BOOK in main menu 2. The red outline of the fish is a feature to keep in mind, as we intend to programme a conditional statement which reports true when one colour meets another specified colour. 3. To script the Little Fish’s movement, add code to the little fish sprite as shown at (A) below. A. Script 1 on Little Fish: (moves the sprite) (1) Little Fish Costumes: 1 60 Little Fish 6. Drag the Little Fish’s if loop into the ‘forever’ loop. The completed little Fish script is shown opposite. NOTE 1: The conditional loop in Little Fish script (2) makes the sprite disappear to simulate being eaten! It waits for one to three seconds before popping up in some random position on the stage, as if it was a different fish. Reminder: When you use the ‘hide’ block in a script, always add a ‘show’ block immediately after the event launcher for the script or you might not see the sprite at the start of your game!