Lesson 5 Nesting

Nesting and the complicated walk around project

Its complicated but we can make it simple

It's game time but you create it

The demonstration we have for you in this lesson, is designed to get you thinking. For the first time we are going to ask you, if you think you can make a game out of this code. If you can think of some idea and develop that game, we will think 2 things. Great you are getting there, in your journey to becoming a coder and we would love to see your cool new game. So email it to us we would love to play it.

Right we are going to have to get to grips with what is happening here, with the logic. We have covered all the basics in the first 4 lessons, now we are going to do a thing called nesting our code. We do this to place our logic in one place, in the most efficient way we can. Get it right and you will have efficient code that is readable and compact. Take a look at this code.

The 1st thing we would like to talk about is the broadcasts. You can see we have drawn a line from one the another in the picture. I think of these like An airline route, you fly to the destination, do what you have to do and there return after you have finished. The 'SetVariable' broadcast is the most common one in this demo, by using the broadcasts we only write and set all the variables to what we want once. This is in one place neat and tidy. If we find we want to add or change anything it is easy do this as everything is together. There is a big word for this Alan and Ada will tell you about that, if you want, it's worth knowing but in your own time.

Tell me about Initialization




There are times in your code when you need to set everything up exactly the way you intended, this is called initialization. Look at the picture above we have a representation of all the variables just before we go into another loop, we need to reset everything as you can see in the picture. In this case, if we did'nt initialize the variable, the background music would just get higher on each loop. You may have notices that the music is not quite right, we will look at the when we talk about testing and debugging in a later lesson.

...

The next Broadcast is the play one. This is the fun bit in the demonstration, all we do is detect if the ball touches the cat but if you use your imagination. You can use this test in the IF statement to do lots of things, like score a goal find the right dress to got out in, its your birthday, pot the ball in a hole, there is so much more. The music is the opening bars from Bach's 'Tacarta en Fuge' I like it. Ada and Alan have so cool stuff on how scratch plays music. Ada and Alan have so cool stuff on how Scratch plays music.

Play it again Sam

This section is for our members, you can get details of how to become a member on our ready to go page. The members site is here.



...

In the demonstration we have an outer loop with a red boarder, so you can see it easy. the next level of the nested code is an IF statement. Have look at the picture below, can you see a pattern here? The Most obvious one is the note 64 or E is played every other beat we do this with a IF ELSE block. When the variable 'NoteSwap' = 1 it will play E 64 and the other notes when it is not = 1. We use a little trick to change the value of 'NoteSwap' every beat, we use the 'times' opperator like this('NoteSwap' * -1) which is a trick in maths. The value will change from 1 to -1 every beat. Please just accept this for now will will talk about this later. There is other code in the ELSE part of this code just use it as it is for now, the maths is a bit hard for this lesson more later we promise. The broadcasts are final part of the nesting. As we said at the beginning of this lesson, it would be great if you can make a game. I know you can it does not have to be complex. Use the mouse position code in the pong game for an example.

Remember the part of this code you will be looking to modify, is the block that is called from the play broadcast. This is call after an IF test, in this case it has the cat touching the ball, as it is tested every other beat when 'NoteSwap' = 1. What this means is the background music can be broken at any time, so you can have your game just getting on with stuff, until you have some event that will be a key part off your game. Your job is to decide what you want to do.


The Demo project


The complicated walk around project

We are going to have our cool kid walking around the path in both direction here. The cool kid can look left and right then make his mind up on where the gray path is, at the end of a strait bit of path. The code is starting to get more complicated here. We are giving a full explanation in our members area. We are sure that you will be able to advance your coding knowledge in our members area.


Down load the Complicated walk around project

This section is for our members, you can get details of how to become a member on our ready to go page. The members site is here.






2nd demo a game to play

Pop Up Jack

We would like to show you our go at putting a game together. Pop up Jack. This is in our members area. We hope you join the members area and let us help you to become a really good coder.

This section is for our members, you can get details of how to become a member on our ready to go page. The members site is here.





3rd Demo Dance studio



This section is for our members, you can get details of how to become a member on our ready to go page. The members site is here.




This section is for our members, you can get details of how to become a member on our ready to go page. The members site is here.