Simon says – Time to play!

the scratch version of simple simon from MagPi magazine

the original Scratch project, courtesy of MagPi

Well, we’re back at school now and settling in.  I’m looking forward to the first few computing classes next week, but I took time out this weekend to have a play on Scratch and BYOB.  I started off with a project from the latest MagPi magazine, on building a Simple Simon game in Scratch.

The game itself was straightforward, following the code given, although I was reminded in the process exactly what the broadcast and wait block does (broadcasts a message then suspends activity on that thread until the receiving thread has finished). That might well come in handy when I sort out the animations for my virtual pet, as I’d been concerned that other scripts might interrupt any animation that’s run.

However, when I played the game, I found one thing that didn’t feel right – it generates a four tone/light sequence, asks the player to repeat it, then generates a completely new five-tone sequence, which quickly makes it very difficult to play as the difficulty increases.  I seem to remember that the original version of the game would add one to the sequence each time, rather than changing it to a new sequence, so I decided to produce my own version and move it up to BYOB in the process.

The game loaded fine from Scratch, although there were one or two issues with the length of list, which apparently is handled slightly differently between the two programs.  However, I was soon picking the code apart and working on the alterations I wanted.

the block I built to cycle through a list and broadcast each item

the block cycles through a list, broadcasting each item in turn

My first problem was that the original game would generate each tone, broadcast it and then add it to the list.  If I was going to add a number to the existing list, I would need to find a way to cycle through all the objects in the list, broadcasting them one by one.  This seemed an ideal candidate for a self-built block.

Looking around the help system for BYOB and finding words like predicates thrown around merrily made me realise that despite appearances this definitely is not just for kids – while kids can quickly get up and running doing something with Scratch, it’s capable of some pretty powerful stuff, even more so in the BYOB version, although the more high-powered you go the more fiddly it gets to produce the blocks needed, with blocks within blocks within blocks.

Simple Simon game, BYOB version

Simple Simon BYOB version

I had to modify the flow of the main program in order to set up a 4 tone sequence, broadcast it, wait for response, then check it.  If the response is correct it adds to the score, generates and adds a number to the sequence then rebroadcasts, while if the response is wrong it takes a life away and replays the sequence as it is.

I definitely needed paper to scribble my thoughts on to work out the logic of the program, but it went together fairly well.  You know when a game is playable, when you have problems forcing yourself to test it rather than just playing!

The new version is more playable than the original, it makes a little use of the extra facilities of BYOB and I learnt a lot about the use of lists in Scratch/BYOB in the process.

The game currently keeps score during the game but is reset on restarting – one improvement would be to add in a high score variable, to keep track of the highest current run. I could also start the score at 4 instead of 0, so that it reflects the number of notes in the sequence.  An alternative would be to start with a one-note sequence, but that might make the game too slow to get started.

 

Advertisements

Virtual pet version 2

Time I returned to my virtual pet!  While I have written a detailed specification for the project, I decided to put in an intermediate stage when creating, so in this version there is no day or night, and the algorithms for the change in the variables aren’t completely worked out yet.  I do now have three variables for the pet: health, happiness and hunger.  Each has its own way of interacting with it: feeding, medicine and a ball to play with.  The player has to keep hunger down and happiness and health up in order to have the pet survive.  Health and happiness link to each other – an unhappy pet loses health faster, and vice versa.

virtual pet code version 2

code for version 2

I worked in Scratch originally for version 2, then when I could see repeated code I used BYOB features to pull out that repeated code and make it into a new block, complete with a passed in parameter.  This avoided the repetition as each requirement for death meant broadcasting a message explaining the reason, making the pet disappear and then stopping the script.

I found it straightforward to create my new block, including passing in a message to say why the pet died, and I also like the way that BYOB will change the colour of nested blocks, making it easier to see how they are put together.

Adding a timer to the game made it a little more interesting, as now it will record the length of time the previous pet lasted, but I still need to do a lot of work on the graphics, in order to have the pet show visually what his problems are.  Once I’ve done that, it may be possible to remove all displayed variables from on screen, to make the player look carefully at the sprite rather than just checking the numbers.

die block, including a passed in parameter

custom made “die” block

While the code for the project is becoming more complicated, it is still fairly straightforward, with one script per sprite.  Breaking into more than one script could lead to complications as different scripts running at the same time could interfere with each other, so I’m trying to be careful – for example I could have scripts to run animations for different interactions, but it might be possible to run two or more at the same time, with unintended results.

The project is coming along nicely, so now I need to reproduce this version in App Inventor. I also need to look back at the specification I wrote to see how it fits in with what I’ve done so far and then move on to develop it further, including day/night settings.

I’m still not happy with the gameplay and amount of interaction compared to feedback.  It’s a playable game, but without enough real challenge or interest yet to keep a player interested.  Thinking back to the original tamagotchi, that would beep for attention, and there were mini-games to play.  It was also a much longer-lived game, lasting for days and even weeks.  That’s not possible in Scratch, as it has no file-saving capabilities, but it would be possible in an android app, which is more likely to be the end format of this project.

It would also be possible in a Python project, which is something else I might try at some stage – that would make it usable on a PC, rather than on a mobile device.

Before I do much more with coding, though, I need to work on developing graphics and sound, as a game is so much more than just programming in the behaviours.

 

Virtual pet – BYOB and App Inventor

It’s time to try out my project idea in different software.  I’m working on the virtual pet idea, and first creating in BYOB, and then in MIT App Inventor.

The starting point for the code is the project found at interactiveclassroom.net, but I have developed this a little and will be continuing to develop further as the project goes on.

BYOB virtual pet code

The starting point for the pet code

Scratch/BYOB

This is what the pet sprite code looks like in BYOB (more or less just Scratch, here; no extra features of BYOB used yet).  The timing is very short at the moment to allow for efficient testing.

BYOB feed me button code

The code to feed the pet

There is a button to click to provide food.  The variable hunger is set to apply to all sprites (global variable). Another option is to make it apply only to the pet, but in that case the button sprite would not be able to change the hunger value and would have to send a message to the pet instead, asking it to change its own code.

BYOB virtual pet stage

BYOB version game stage

This is what the game looks like so far: very simple.  The pet’s hunger is displayed onscreen and gradually goes up to 10.  If it gets to 10, he disappears. If the feed me button is clicked, this decreases the hunger by 1.  The next development will be to have a value for health, which will gradually decrease by a random amount and will require medicine to be given.

 

 

MIT App Inventor

The next task is to create the same project but in MIT App Inventor.  It says a lot for the usability of this system that I felt confident enough to try this project so early on in my learning.

virtual pet code in the app inventor

virtual pet code

The system itself was again straightforward: I designed the screen layout, and then used the blocks editor to build the code.  The only complication was that I could not find anything to reset the game, which in Scratch/BYOB I would do by clicking the green flag again, so I built in a reset button as well.  I defined my own variable, hunger, as a global variable as in Scratch, and defined a procedure block called showHunger, which would display the current hunger level in the label element.  This was then available to use whenever I needed it.  The clock element fires very frequently at the moment, again for testing purposes, and when it fires the hunger increases until it reaches 10, at which point the pet disappears, a message appears saying the pet is dead and the reset button becomes active.  Pressing it redisplays the pet and sets its hunger back to 0.

virtual pet running in the emulator

Virtual pet on the emulator

I had a few problems getting the blocks editor open this time, possibly because I was working on a different machine with slightly different security settings, but generally doing all the creation online makes it straightforward to work on my project wherever I want.

This shows the game in action so far.  The red button will reduce the hunger, which otherwise grows to 10.  Once it reaches 10 the pet disappears and the reset button becomes active.  Pressing that will bring the pet back and reset hunger.

So far so good: I have the raw bones of my project up and running in two different software platforms.  It’s playable, if rather simple and uninteresting, and I’m ready to start adding in new features.  Of course, it’s important to make sure that the new features don’t interfere with the original ones!

If anyone would like more details of the design side of the app, or any other information on my projects, or you just want to tell me if you’re finding it useful, feel free to comment on the post (link at top).

Introduction to BYOB (build your own blocks)

byob using square block

This shows the creation and use of a Square block

Scratch itself is very simple to use: click blocks together to build scripts for the sprites (graphic items) to follow onscreen.  There are lots of support materials for this on the MIT Scratch site, but most primary school children will be playing around clicking blocks together within a few minutes of seeing the program.  All blocks are colour coded according to use, and shaped specifically so they can only be put together in certain ways.  This helps with correct syntax and takes away the agony of spelling commands correctly and getting punctuation right.

BYOB is the big brother of Scratch: With BYOB, you have the option of creating your own blocks as well, defining subroutines or functions, and then calling them from the main program.

My first block – moving in a square

In this example, I have used the block editor to create a block called SQUARE, which takes one input, SIZE, and moves the sprite in a square – REPEAT 4 TIMES: move SIZE steps, turn 90 degrees. The default value for SIZE is 50 (click on the images to see a larger version).

This means I can not only move in a square whenever I want to by using my custom block, but I can also adjust the size of the square from outside the block.

I have used the PEN DOWN block so that the sprite leaves a trail behind.

byob polygon block in action

This will allow you to specify number of sides as well

Developing – moving in a polygon

This time I have developed the block to take two parameters, size and number of sides.  I have asked for and stored both these values as variables, then used them when I called for the polygon.  A simple maths formula works out the angle of turn.

I found this a little fiddly to start with, but soon got the hang of it.  It promises to streamline Scratch code, which can get a little unwieldy at times, and offers lots of possibilities.  There are other new features in BYOB that I haven’t explored yet, but it looks as though they have good support materials for them.

Now I’ve seen how to use custom blocks in BYOB, the next step is to start using it for my virtual pet game – I’ll go straight into using BYOB rather than Scratch, as it’s basically the same thing but with extra features.