Summary

This document provides an introduction to Scratch programming. It covers fundamental concepts, such as flowcharts, conditional loops, and Sprite movement. The guide also delves into creating games, illustrated with clear examples and step-by-step instructions. An overview of block categories in programming is also included.

Full Transcript

# Coding with Scratch - Unit 4: Coding ## Start of activity: Fill in the blanks with the correct words from the word bank given below. * Stage * Sprite * Scratch * Variable 1. The area in which sprites perform a given task. **Stage** 2. A character to be coded in Scratch. **Sprite** 3. A non-tex...

# Coding with Scratch - Unit 4: Coding ## Start of activity: Fill in the blanks with the correct words from the word bank given below. * Stage * Sprite * Scratch * Variable 1. The area in which sprites perform a given task. **Stage** 2. A character to be coded in Scratch. **Sprite** 3. A non-textual program application. **Scratch** 4. You can keep things in it (like points!) and keep adding to it or subtracting from it. **Variable** ## Understanding Functions **Functions** Functions are blocks that perform tasks based on a set of instructions that have been put together in one group. The term is not used in Scratch, but you will come across it in other programming languages. There are six types of blocks in Scratch, as shown below. **Hat Block** This is used to start a script. A block can be added below this block. **Stack Block** Other blocks can be added above and below this block. These are used to perform main commands in the script. **C Block** These C-shaped blocks are used in conditional programming. **Boolean Block** These blocks represent Yes or No values. **Reporter Block** These blocks can hold numeric and string values, and can be added inside another block when a value is needed. **Cap Block** This block is used to end scripts. No block can be attached below it. **Block Categories:** All these blocks are arranged in the following block categories: * **Motion:** control movement of a sprite (15 stack blocks and 3 reporter blocks). * **Looks:** change the appearance of the Sprite and the Stage, applying graphic effects or changing a sprite's costume (17 stack blocks and 3 reporter blocks). * **Sound:** add and play sounds (8 stack blocks and 1 reporter block). * **Events:** sense events (6 hat blocks and 2 stack blocks). * **Control:** control the script (3 stack blocks, 5 C blocks, 2 cap blocks, and 1 hat block). * **Sensing:** detect a situation (5 Boolean blocks, 3 stack blocks, and 10 reporter blocks). * **Operators:** perform mathematical operations (7 Boolean blocks and 11 reporter blocks). * **Variables:** store and access data (4 stack blocks and 1 reporter block). * **My Blocks:** create procedures. ## Understanding Flowcharts and Conditional Looping **Flowcharts & Conditional Looping** When you start writing complicated programs, flowcharts help to work out the flow and logic of the programs first. A flowchart uses diagrams to represent each step of the program. **Example of a flowchart:** A flowchart for a program that asks a question and checks the answer: 1. **Start** (rounded rectangle) 2. **Ask:** What is 4 * 5? (rectangle) 3. **Get Answer** (rectangle) 4. **Is Answer = 20?** (diamond shape) * **Yes:** Say: That is correct! (rectangle) * **No:** Say: That is not right (rectangle) 5. **Say: Bye!** (rectangle) 6. **End** (rounded rectangle) **Getting Input in Scratch** 1. **Look for a block that says `ask...and wait`.** You can change the text to whatever you want. 2. **When you run the program, a box will appear at the bottom of the stage where you can enter a response to the question.** 3. **Scratch creates a variable called `answer`, which you can find just below the `ask` block, and stores whatever the response is in that variable.** 4. **You can store the value of `answer` in your own variable.** For example, create a variable called `UserSaid` and store the value of `answer` in it. ## Looping **Looping** In the Control section of block code, there are `repeat x`, `forever`, and `repeat until` loops. **`Repeat X times` Block:** repeats one or more blocks for a specific number of times in a loop. **`Forever` Block:** repeats an action in a loop forever. **`Repeat until` Block:** creates a conditional loop. The condition that is to be met must be put in the space after "repeat until" and the instructions must come in the space inside the block. The loop will then continue running the code until the condition is met. **Variables & Loops:** Variables and loops are very often used together. **Example: Program to Generate Every Fifth Number from 5 to 50:** 1. **Initialize the variable `counter` to 5.** 2. **Set the condition of the loop:** When the value of `counter` becomes greater than 50, we want to loop to stop. 3. **Display the number.** 4. **Add 5 to the number.** ## Creating a Game: Race of 2 Sprites 1. Choose the sprites "Avery Walking" and "Dog2", rename them "Avery" and "Rover", and set their sizes to 50. 2. Create a new sprite and name it "Finish Line". 3. Set starting positions for Avery and Rover. 4. Create a variable for Avery called "AverySpeed" and initialize it to a random number between 5 and 10. 5. Create a variable for Rover called "RoverSpeed" and initialize it to a random number between 5 and 10. 6. Use a conditional loop that tells the sprites to keep moving until they touch the "Finish Line" sprite. ## Creating a Game: To the Moon! **Scenario:** Chandrayaan has successfully lifted off from the earth and is in space, nearing the moon. We need to turn it and then land it. **Stage 1:** Make sure the rocket is in the right position. * Use the "Moon" and "Stars" backdrops. * Switch the backdrop to "Stars". * Switch the costume to "rocketship-b". * Set the x and y positions to -148 and 50. * Set the direction to 180 degrees. **Stage 2:** Have the rocket travel a bit until it reaches above the moon, and then switch off the engine and turn to land. * Use the `glide` block to move the rocket. * Switch the costume to "rocketship-e". * Turn the rocket 90 degrees (15 degrees at a time). **Stage 3:** Approaching the moon - switch to the moon backdrop, hide the rocket, and show it coming in from the top. * Hide the rocket. * Switch the backdrop to "Moon". * Wait 1 second. * Position the rocket at the top of the screen. * Switch back to the "rocketship-c" costume. * Show the rocket. **Stage 4:** Ready to land! * Glide the rocket down to the moon's surface. * Switch off the engine. **Chandrayaan has landed on the moon!**

Use Quizgecko on...
Browser
Browser