# Making a Sprite Jump In Scratch

See the following video (Notes after VIdeo):

## Blocks used in this project

See the project below and observe the blocks that were used:

https://scratch.mit.edu/projects/473323685

# Repetition (Iteration) Constructs

Consider the following example:

Find the sum of 10 numbers provided by the user.

This can be accomplished as follows:

The above solution is very long as well as difficult to understand , manipulate and communicate .

Observe that there were many repeated steps in obtaining input from the user.

The solution can be simplified by looping.

A general looping construct is shown below:

The solution can be simply implemented using looping:

Class Discussion: walk-through of solution is discussed.

### Homework:

Create an algorithm which uses repetition to output  the 12 times table from 1 to 12.

2022-10-6: Added files used in this post.

# The Selection Construct

Sometimes we wish to change the order of processing based on certain conditions.

This can be accomplished by using a selection construct pictured below:

We will examine this in the following example:

Jim is a salesman for an auto car company. If his total sales for the week exceeds \$10000 he is rewarded a sales commission of 8 percent of the sale, if not he is awarded no commission.

Create a flowchart algorithm which accepts the total sales and outputs the commission.

### Exercise:

The problem is modified such that  if the total sales were less than \$10000 he would receive a commission of 4 percent. See changes below:

Jim is a salesman for an auto car company. If his total sales for the week exceeds \$10000 he is rewarded a sales commission of 8 percent of the total sales, if not he is awarded a commission of 4 percent.

Create a flowchart algorithm which accepts the total sales and outputs the commission.

Draw a flowchart for the modified scenario.

# Algorithms In Narrative Form

An algorithm in narrative form is not written in discrete steps.   It is expressed as directions in paragraph form and instructions on how rules should be followed.

An algorithm in narrative form can be considered a precursor to pseudocode or a flowchart

## Example

An example is given below for the given problem statement

Problem Definition: A solution is needed which finds the circumference of a circle.

Algorithm in narrative form: Prompt the user to enter a non-negative value for the radius and store it. If the radius entered is negative or is not a number, keep on prompting the user and accept values until a suitable value is entered.  Use the formula C=2*3.14* r to calculate and store the circumference. Output the circumference to the user and stop processing.

## In class exercise

Write an algorithm in narrative form which gives instructions/directions on how to follow the rules in order to play “Snakes and ladders”.

# WHAT IS AN ALGORITHM? – DEFINITION

An algorithm is a well-defined, precise, finite set  of steps for solving a problem.

# A Simple Game! – Part 5

In this lesson, we create a variable called score and update it when a collision occurs. The code occurs in our “Play game” broadcast while moving the ship.

So that the code becomes:

We add a variable called Still playing to keep track of our current game state:

And the score is only reset during initialization when “StillPlaying” is false.

In the case that the ship is missed after movement was completed,, we simply set “StillPlaying” to false and our score is reset during initialization:

That’s it for our main logic!

Here’s the full code for the spaceship:

Here’s the code for the planet:

A full implementation with all the bells and whistles can be found here:

Planet Destroyer

# A simple Game! – Part 4

In this part , we update our ship’s initialization to return to a static point after motion as well as add our planet which appears at a random position when the game starts.

First, return our spaceship to it’s starting position:

And now we set up the initialization to choose a random starting position:

(ENSURE THAT WE ADD THE CODE BLOCK TO THE PLANET’S SPRITE!!!)

In the next part, we’ll update the score on a collision , and reset the score to 0 when a miss occurs.

# A simple Game! – Part 3

In this part, we move our spaceship until it touches the edge.

We will move our spaceship 3 units at a time in the direction it’s facing until it touches the edge of the screen.

First setup the constraints for moving until the edge is touched. We accomplish this using a looping construct and a sensing block:

Now we add the following code block to move the ship:

It’s setup is shown here:

It is important to note that you don’t need to understand how the codeblock above works, only that it moves the ship 3 units in the direction it’s pointing!

Our final scenario is shown below:

# A simple Game! – Part 2

In this section, we start by getting setting up the initial state of our game.

We need a variable to store the bearing, and here we set the bearing to 0 and point the spaceship vertically:

Ensure that your rocketship’s sprite is selected so that the code is applied to the rocketship.

Note the use of the broadcast system – we use this to achieve a modular approach.

Next, we setup the code to Accept the bearing and turn the ship:

Now, when we click on the green flag, we’re able to prompt the user to enter a bearing and turn our rocketship!

The full code is shown below:

# A simple game!

## Update and Video (2021 Feb 9th)

I’ve produced a video on this tutorial , the most notable difference is that the sprite was rotated to simplify movement. This means that the block below was able to be replaced by a simple move block.

The older tutorial is below:

# Introduction

In this tutorial, we’ll be developing a simple game from beginning to end.   We’re going to go through all the many, many subtasks that goes into developing our game.

This project is a problem solving one.

In this development project, our problem statement is represented by the task at hand.

Create a simple game using scratch.  The player is prompted to enter a bearing for a spaceship which then adjusts its direction and travels forward in that direction. The objective of the game is to hit a planet which is subsequently destroyed,  If the player is successful, increment his score, otherwise the game ends.

### Part 1 – Setting up the sprite

First, I’m  going to create my scratch project. And name it “Planet Destroyer – Lesson Tutorial”:

Now, we replace our sprite with one more suited to look like a rocket.

First delete the default sprite by clicking the x on the sprite.

Next choose a sprite using the “Choose a sprite” button

Search for “Rocket” and select the rocketship.

Now we have a rocketship added to our project.

My rocketship  is taking up a lot of space, so let’s reduce it to about 30%:

And we’re done setting up our Sprite!

Next: A simple Game! – Part 2