the clock strikes again

My students will be doing final Snap projects soon and that means I will be guiding them. Part of that process is showing them other Snap projects written by other students. And wouldn’t you know it? This is the weekend the Snap folks are modding the cloud and it is unavailable. That means there is no access to the projects stored on the Snap cloud and no saving new projects to the Snap could, either.

What’s a woman to do? Write a new project and save it locally, that’s what. Here is the new Pomodoro Clock, Snap edition:

No link because … no Snap cloud. I’ll be GitHubbing it soon, but I want to beta it first. This has no custom blocks, no lists, many costumes, and many broadcast messages.

is it in there?

Sometimes you want to know if something is in a list. If the list isn’t sorted, the only way you can know for sure is to look at every item in the list and see if it is what you’re looking for. That is called a sequential search and it’s pretty easy to do. Since we’re using Snap, I’ll draw you a picture.

This script reports whether the item was found in the search or not, but it could be modified to do other things. Suppose for example, you have a list of students and you’re looking for all students named Bob. You don’t want it to simply stop with the first student. So you’d need to modify the search to change the “found” half of the or on the repeat until … and instead have found be a variable that you set to an empty list before the loop and then add the index positions as list items to it when you find a Bob in the class. If there is one. If there are no students named Bob, you would have an empty list, same as what you started with. If there were two students named Bob, you would have a list with two index positions in it, telling you where the name Bob appears in the list.

Of course there are other ways. This is programming and, generally speaking, you can find several valid solutions to any problem.

If the list is sorted, there are shortcuts. You can split the list and look in the part of the list where you know something is. You do this when you look through any alphabetized anything. You know not to look for something that starts with a  in the r section.

i’ve got a secret

I said before Hangman is about lists. One of those lists is a list of blank letters that represent the word the player is trying to guess. The easiest way to deal with this is to write a custom block that generates a list of as many blanks as there are letters in the word.

In programming, there are always several right ways, however, there are usually many more wrong ones. Here is one idea of what that custom block to make a list of blanks might look like.

Someone could also build a word of blanks and then use this block from the Import Tools to turn it into a list.

However you build the custom block to make your list of blanks, to say three blanks the script would look like this:

In Hangman, the list of <length> blanks block would the beginning of my display word, before any letters were guessed. If the word were cat it would be three letters. Or run.

To code it, I’d make a variable to store that list. and then replace the blanks with letters as they were guessed correctly.The list variable would belong to all sprites so the host could do the work of checking it and the assistant could do the work of saying it.

every rose has a thorn … every script has a sprite

Cliches get that way by being true. People remember them for the next situation because they have an established track record.

Every script has a sprite that it’s attached to.  If you want to do something in Snap, you pretty much need to hang it on a sprite (or the stage, but that’s a special case). It’s a feature of Snap.   Sprites don’t need scripts.  You can have a sprite sit there on the stage and look pretty and do nothing but you can’t have a script run without there being a sprite for it.

This is important because if you want to do something in a Snap project, you have to decide which sprite is going to be in charge of it.  Suppose there is a game where the player tries to catch falling raindrops in a cup.  If a raindrop lands in the cup, a message is broadcast to add a point to the score. Which sprite sends that broadcast?  The landing raindrop or the receiving cup? There are good arguments either way. It’s up to the coder to decide.  But the coder has to attach it to some sprite.

That’s why I say every script has a sprite.

planning and implementation

One of the hardest lessons to learn is that it’s easier to work from a plan than to wing it. Most things that most people do most of the time don’t require a lot of planning or forethought because they’re repetitive tasks that have been done before. For example, I don’t make a plan to drive to my grocery store because I know the route, I know what I’m likely to encounter, etc.

The truth is, the first time, I didn’t know.  And when you’re creating a project, it’s the first time. When I go to a new place for the first time, I use Google to map it, I figure out how long it will take to get there so I can leave at the right time, I maybe even use the street view to see what the landmarks might be.  A planning worksheet is an equivalent for a software project.  It’s a way to break down the problem of how do I get from nothing to a solution in steps.

Depending on the complexity, a planning sheet will have different kinds of information but since Hangman is my current project, I’m going to frame this topic from that perspective.  There is a worksheet that was thoughtfully provided, but what if there hadn’t been?  What kinds of things would need to be on it?

Well, looking at the rubric, Hangman has to be able to do a lot of things. 

So any planning sheet has to account for that. Since this is Snap, there will be sprites and the stage.  Every script will belong to either a sprite or the stage so sprite ownership is something that needs to be part of the planning sheet.  Also, information will need to be stored. That means variables will be there, too.  I’ll need a column for pseudocode and a column for issues or pitfalls or whatever I want to call the potential snags I’ll run into.  This is not what the planning sheet looks like that was handed out.  
This is a matrix with two examples filled in. Kinda like a spreadsheet.  The first column is the rubric item and the rest are the columns I identified that I, as the project coder, would need to cut the project down to size.  Other people might see the project with different frameworks and have different columns.

The important thing is to have a plan.  And a planning sheet gives you a good place to create one. The better you plan, the less actual work you have to do (at least that’s the theory).  And in my experience, I’ve found it true often enough that I make it a habit to plan many facets of my life, not only my programming projects.

it’s all about the lists

Fundamentally, Hangman is a game of lists.  You have the alphabet, the list of letters that make up a secret word and a list of letters that have been guessed.

This is why it was chosen as the project for the list unit in our curriculum. Snap has a lot of blocks to deal with lists but if you use the import Tools, you get access to even more.  My favorites are andThe first one turns a word into a list and the second one turns a list into a sentence.  Both are useful for Hangman.

making a list, checking it once

Checking a list is called traversal (actually, going through the list and doing anything to it is).  Here’s an example of a list and a traversal of it.  These are the Great Lakes of North America.

The script variable is a variable that does not belong to any sprite and is used only in the script.  This code only says the names of the Great Lakes, but if you take out that purple block and put something else in there, you could do other things.  For example, if you wanted to see which lake had the longest name, you could have a script variable called longest and store the first name there. Then, instead of saying the name, you could have an if block that checks if the current name is longer than the longest  name and if it is, make that name the longest name..  Then put your purple say block at the end and say longest for 2 secs.

back in business

I am back,  I’ve hiatused long enough and I have fixed the images and code snippets.  What you’ve missed is lists.  Traversals and sequential searches of same.  I’ll cover them as part of Hangman, which I wrote in Snap, but as my students are writing it now, I’ll not be posting mine quite yet.

I’ll post my platform game though. CatStuff.  Here’s a screenshot.Basically, the cat can jump over the saucerbot but if they collide, the game is over.  That’s it.  It wraps. It’s boring.

background check

How do you get the background to move in Snap? How do you get trees or rocks or buildings to move when your hero goes across the stage? The quick answer is, you don’t. The stage doesn’t move. So how do you have moving tress, rocks, or buildings?  You make them sprites.

Making something a sprite doesn’t move it though, you need a script.  You also need to do some things outside of the script to ensure your sprites do what you want.  First of all, make sure that your sprite can only face one direction.  There are three buttons at the top that control the direction(s) your sprite can face.  Make sure to click the bottom one.That arrow pointing to the right means that the sprite can only point one way, and that’s the way the costume points by default.

Next, make sure the Draggable box is unchecked.  That keeps the player from moving your sprite around.

Now you’re ready for your script.  The important principle here is that when your hero moves right, your background sprites scroll to the left.  If you have a “forever” level, they need to also disappear and reappear when they get to the edge.  Scrolling is perfect for a custom block because you’re going to have more than one background sprite and you can reuse the same code with different speeds for the different sprites.   Sprites that are intended to be closer to the viewer move faster (and are generally lower on the screen) than sprites farther away.

Here is code for a sprite scrolling left.  Note that like any other code, it’s only one possible way,  not the only way.This allows you to plug in two parameters, the speed and the height.

Here, kitty-kitty-kitty

I have a platform game.  It doesn’t have a game name yet; the file name is CatStuff and it’s a cat and a zoomba (kinda like a  cross between a trademarked robot vacuum cleaner and a drone).  I have some very cool code in my pocket for this game.  Jumping.  for example.

In Snap, you animate by changing costume and moving the sprite. My cat has 12 costumes—eight walking and four jumping.  Walking animation is pretty simple.  You cycle through the costumes and change the x-coordinate by an amount.  depending on how fast you want to move.  Jumping involves the y-coordinate as well and you have to do both at the same time.  It’s tricky.  Here’s my code to walk right.

You see the variable for the number of steps to move.  That can be set for all the walking the cat does (and it figures into jumping too) in one place.  That’s the point of having it there.  -240 and 240 are magic numbers in Snap as they are the farthest left and right x-coordinates on the standard stage.

Now for jumping.  Because jumping is not an even process, I couldn’t easily abstract it into a loop like I did walking.  It’s long messy code.  I don’t have comments in the code I’m posting here because it makes it difficult to share the images neatly.You’ll notice there’s an if-else at the bottom.  This is to deal with repeating presses of the space bar that don’t resolve fluidly.  The cat will fall if too high and if somehow it gets below ground, it’ll pop back up.   That’s an in-case because I’ve not observed it, but it is theoretically possible.

Jumping is faster than walking but not twice as fast and I wanted a finagle factor so there’s a place to set one.  I’m math-impaired (really) so my formulas aren’t always mathematically sound.  They work for the game though.

Next time, I’ll talk about scrolling the scenery.