Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

CHAPTER 04

PROGRAMMING
THE FROG MAZE
Slides by
Ms. Abeera Sikandar
lecturer
bahria college anchorage
computer sciences
Sprite: Frog and Gift box
Backdrop: night-time class

Starting values
Click on the frog. Set the starting position and size of frog sprite.

Script: short program to control the sprite.


Program: whole game with several sprites.
How to get the frog to move
We will add commands in the screen to move the frog using arrow keys. For example:
Up arrow key : frog will move up.

Change the y-coordinate


x/y coordinate is a number value. It represents the position of a point on screen.
Y-coordinate sets how far up or down a point moves on the screen.
Higher sprite means higher value of y-coordinate.
Increasing the y-coordinate will move the sprite up on screen.
How to get the frog to move

Change the y-coordinate


These blocks will add 10 to the y coordinate if the user presses the up
arrow key.
How to get the frog to move

Forever Loop
We want the frog to move not just once but again and again, so we
need to put the movement blocks inside a forever loop.
The Gift

Second sprite: Gift.


This script will not move, but has some starting value.
• Set size to 50%
• Set the x value to 0, set the y value to 150
Add a Snake

Danger: Third sprite: Snake


The script for snake will be including following commands:
• Start when the user presses the green flag.
• Set size to 50%
• Set x and y coordinates to 0.

Then add a forever loop, to make snake move forward and backward.

The rotation block means the snake stays facing the right way.
Add a Conditional Loop

What is conditional loop? And how does it work?


A conditional loop is a loop controlled by a logical test.

There is a space ate the top of block.


A logical test will fit inside the loop.
These commands inside the loop will repeat ‘until’ the logical test is True.
The snake catches the frog

As the snake will touch the frog, the game will stop.

For this, Put the ‘touching Frog’ inside the conditional loop block.

The snake will say “caught you”.


Touch the Gift

Now we will change the frog script.

If frog touches the gift, he wins. So the loop used here will be:
Conditional loop

Remove the forever loop and put repeat until loop


With conditional logic: ‘Touching Gift’.
What happens if the frog touches the gift?

Frog will say:


‘Happy Birthday to me’
The game stops.

Change the backdrop


If the frog touches the gift, the backdrop will be changed into a party picture.
Click on ‘Choose a backdrop’ button.
Find the backdrop called ‘party’. Click to select it.

Add this block to frog script


What happens if the frog touches the gift?

Change the sound


Add happy sound of the frog.
Across the top of screen, are three tabs: Code, Costume and Sound.

Click ‘Sound’.

At the bottom of screen, find a button like this 


Click on ‘Choose a sound’.
Complete the Program

So when frog touches the gift, three things happens:

1. The backdrop changes into party.


2. The frog says ‘happy birthday to me’.
3. A happy sound plays.
Press the UP arrow key

So frog moves up, if the y coordinates changes by 10:

What is Logical test used for?


The logical test checks if the user has performed the called action or not. For example if he has
pressed up arrow key.
Press the other arrow keys
How can you make the frog to move in all four directions?
The up and down arrows change the y coordinate.
The left and right arrows change the x coordinate.
Create a Module

What is a module?
The module is like a mini-program that stores commands to make
the script smaller and easier to read. The module has a name
chosen by programmer.
They are also called procedures/functions.

For example: all the four movements of frog will be stored


in movement module.

How can I make a module??


How can we create a movement Module for frog?

A red block will appear in your script area.


Store the movement blocks in this module.
Drag all the movement blocks out of the frog script.
Join them to new block, named ‘movements’, you made.

that’s how module is created.


How Modules work?

A programmer can use modules in their programs. When the computer sees the name of a
module in the program, it will carry out all the commands stored in the module.

Give at least three advantages of modules:


 It makes program shorter and easier to read.
 Programmers can use modules in more than one part of their program. That saves time &
effort.
 Programmers can use modules in new programs. They can share them with other
programmers. That makes everyone work easier.
Modules in Scratch?

In scratch, modules are called, ‘My Blocks’.


Click the red dot, that says my Block.
Make more Modules
Make a module
1. startup:
It will store all the starting commands that
come before the loop in this module.
2. endgame:
It will store all the commands from the end
of the game.
Make Maze game
Upload a new backdrop of maze from the options shown in picture.

Open the backdrop menu and choose the top option:


Upload Backdrop”.

To upload means, to copy a file from your computer to scratch.


Select the maze image.
This image is now the backdrop of your maze.
Edit the scripts
Edit means making changes to your script and make new script.

Frog script:
Load the maze backdrop. Put the frog in its right place.

Movement module:
Change all movement modules from 10 to 2. This will let the frog
Move slowly and easier to control.
Edit the scripts
Edit means making changes to your script and make new script.

Gift script:
Set the x/y coordinates of gift sprite and set it at the end of maze.

Sense if the frog touches the maze walls:


Change the frog script. If the frog touches the maze wall,
Means if it touches the black colour of wall. For this, set all
Colours to 0.
Edit the scripts

Sense if the frog touches the maze walls:


Change the frog script. If the frog touches the maze wall,
Means if it touches the black colour of wall. For this, set all
Colours to 0.

Go back to start:
Sense block will be in if structure.
If frog senses maze walls, it will go back to its start position.

You might also like