So now we are getting out of beginner's waters as far as sprites are concerned and into intermediate and what we are going to do here is make the cat move as we choose and tell us when he's gone as far as he can go. In technical terms this is called interacting actively and arguably it is a simple form of artificial intelligence; in other words we are going to give the cat some brains. Here's a snapshot and a link to the finished project:

Coding for Primary Schools

Back to Title

Making  Sprites Move as we Choose

Previous Page catedge scratch.mit.edu/projects/48357764/

Here's a snapshot of  the code for the cat:


There are four scripts in here but have a look at  the two big ones. They are in fact exactly the same thing as far as blocks are concerned and the only difference is in the event handlers at the top and the values in some of the blocks; underneath the differences are highlighted:


If you remember in the last lesson I spent a sentence or two banging on about x and y positions on the screen.  Those of you who have read other sections on this website will now be thinking "he's not going to bang on about Cartesian co-ordinates for ages is he? "  and those of you who know this website from old will be thinking  "he is you know."

The above illustrates the point nicely and just to hammer it home here's a lengthy diatribe accompanied by a diagram.


Those of you who specialise in English will probably dimly remember the thing on the left there but to those of you who do maths it should be very familiar. It's just a four quadrant graph and it shows how your viewer in scratch is split up into a series of two dimensional co-ordinates. By convention we state them as  x : y  with x running horizontally and y vertically. The point where the two lines intersect is  x=0 y=0  or  0,0  and is referred to by mathematicians as the origin. Left of the origin is dot 1 which would have coordinates -100,0 and right is dot 2 at 200,0. Dot 3 is 100,100  and dot 4 is at           -240,100. Dot 5 is  -100,-100 and dot 6 had I not drawn it slightly to the right of where I meant to would be 0, -100 and as it stands it's probably 10, -100. Dot 7 is 140, -100.


The summary:  the further left of the centre of the screen, the more negative the x position. The further right, the more positive. The further up from the centre, the more positive the y position, the further down, the more negative the y.

The diagram above also shows you the limits of the movement your sprites are allowed in Scratch. The top of the screen is  y = 180,  the bottom is y=-180. The left edge is x=-240 whilst the right is 240.


What does this have to do with the cat walking? Well, look at the conditionals:


So, if the cat is too far left, it stops and tells you so. It does the same if it's too far right.


To those of you looking at that and thinking "but -240 and 240 are the edges of the screen and that says    -194 and 194" go to the top of class, get a gold star and the nuns won't hit you all day with a ruler (you can tell what sort of school I went to can't you?).


The reason for foreshortening the edges is to make room for the sprite's edges. Observe:


catedge catcentre

To the left there you can see where the CENTRE of the cat sprite is and it is the centre that we consider when stipulating the x and y positions.

To the left there is the difference between the centre of the sprite and the edge of the screen. The cat is a highly irregular shape and has no order of symmetry and so we need to take that into account. So knocking 43 pixelsoff the limit ensures he does not run off the screen entirely.

You can change the centre of your sprites to make them behave in different ways and later we'll do a clock project to show that. For this project, all that remains is the code for the shadow. By now you should be familiar with all these blocks and so I'm just going to do a line by line comment on them:


Event handler -sets initial conditions when project started

Ensures the shadow stays behind the cat

Sets the shadow to the correct position relative to the cat

Sets the horizontal position counter to the correct position on starting

Sets up a recurring loop

Sets the position of the shadow to the correct position relative to the cat. Being in the recurring loop it does this forever.

If you started knowing nothing about coding you've come a long way if you've got this far and you now have the grounding for doing anything in Scratch if you've understood all this. Officially you are no longer a beginner so have a cup of tea to celebrate.


In the next lesson, which will be the last in which we use the cat for a while, we send him down to the disco.

Next Lesson >