Return of the Kaossilator

The Korg Kaossilator is a nifty device for making weird sound effects. I got one quite a while back and it has provided the sounds for quite a few projects. Today I was working on Begin To Code Python and I needed a suitably awakening tone for an alarm program that I'm writing. So I dug out Mr. Kaoss.

I remember years ago that one of the marketing pitches behind Duracell batteries was that they didn't leak.

That is not the case now. Ugh. But after a thorough clean up we are back in business and making amazing sounds again. If you're interested, the sound I ended up with is here:

Monday Snaps: Cheese Lander Game Reset

Welcome back to our weekly Snaps session, which has the ultimate aim of creating the awesome gaming experience that is Cheese Lander . Last week we got the bread, cheese and background screen drawing nicely, this week we are going to place the bread and cheese at their starting positions. So, if someone asks you "Who moved the cheese?" you can say it was you.

We're going to start with a bit of gentle re-factoring. Re-factoring is a posh word for "putting things in the place that they should have been from the start". Computer programmers are lucky in this respect. Doing "refactoring" in other professions means moving physical things around. Refactoring a brick wall is hard. But with code it's easy. What I want to do is create some methods that will deal with my game objects. To do this I'm going to have to move the game objects into the enclosing class, so that they are visible to all the methods in that class. 

public class MyProgram
{
    ImageSprite cheese, bread, background;

    void setupSprites()
    {
        SnapsEngine.StartGameEngine(fullScreen: false, framesPerSecond: 60);
        background = new ImageSprite(imageURL: "ms-appx:///Images/Background.png");
        background.Height = SnapsEngine.GameViewportHeight;
        background.Width = SnapsEngine.GameViewportWidth;

        cheese= new ImageSprite(imageURL: "ms-appx:///Images/cheese.png");
       
cheese.ScaleSpriteWidth(SnapsEngine.GameViewportWidth / 20);

        bread = new ImageSprite(imageURL: "ms-appx:///Images/bread.png");
        bread.ScaleSpriteWidth(SnapsEngine.GameViewportWidth / 8);

        SnapsEngine.AddSpriteToGame(background);
        SnapsEngine.AddSpriteToGame(bread);
        SnapsEngine.AddSpriteToGame(
cheese);
    }

    void gameLoop()
    {
        while (true)
        {
            SnapsEngine.DrawGamePage();
        }
    }

    public void StartProgram()
    {
        setupSprites();

        gameLoop();

    }
}

This is my re-factored game class. It now has two methods. One is called setupSprites, and it sets the sprites up. The other is called gameLoop. This runs the game loop (remember that a game is all about repeatedly drawing and updating. I've also renamed the ball object to cheese. It seemed the right thing to do.

Now things are all tidy I can start building up my methods that will make the game work. When the game resets (i.e. at the start of a new game) I want the bread to be placed somewhere on the bottom of the screen. The aim of the game is then to steer the cheese onto the bread. A method called resetBread would seem to make sense. If you're asking why I'm not calling it setupBread, then I hear you. The answer is that I'm being careful with my names here. setup is something that you do once in the lifetime of the program. Reset is something you do at the start of each game. This is just my standard, yours is allowed to be different, as long as you have a standard.

Moving the bread to the bottom of the screen is easy. We just set the bottom of the bread to the height of the screen:

 bread.Bottom = SnapsEngine.GameViewportHeight;

Moving the bread to a random position across the screen is a bit harder. For a start we'll need a random number. We can get random numbers from the Random class in the System namespace. I can create an instance of this in the class for the game to use:

System.Random breadRandom = new System.Random();

I'm going to use a different random number generator for each object that needs random behaviour. This is because that way I can convert any of them to produce the same sequence (simply by adding a seed):

System.Random breadRandom = new System.Random(1);

Now my bread would be placed at the same sequence of positions when the game is played. This is a good idea because it helps the player learn how the game plays, and encourages them to come back and have another go. In the case of the bread, I'll probably make it completely random.

Anyhoo, once I've got my random number generator I can place the bread somewhere random across the width of the screen:

 bread.Left = breadRandom.Next((int)(SnapsEngine.GameViewportWidth - bread.Width));

This code is a bit of a mess to be honest. The Next method of the Random class can generate an integer in a range from 0 to an upper limit. The upper limit I want is the width of the screen minus the width of the bread. I can calculate this, but Snaps graphics work with double precision numbers. So I have to convert the result of this calculation into an integer. 

void resetBread()
{
    bread.Bottom = SnapsEngine.GameViewportHeight;
    bread.Left = breadRandom.Next((int)(SnapsEngine.GameViewportWidth - bread.Width));

}

This is my resetBread method. It places the bread nicely at the bottom of the screen. I can now steal this technology (ooooh, that sounds so cool) to position the cheese at a random position on the top of the screen:

void resetCheese()
{
    cheese.Top = 0;
    cheese.Left = cheeseRandom.Next((int)(SnapsEngine.GameViewportWidth - cheese.Width));
}

After these reset behaviours I have a game screen that looks like this:

The entire program is here:

 using SnapsLibrary;

public class MyProgram
{
    ImageSprite cheese, bread, background;

    void setupSprites()
    {
        SnapsEngine.StartGameEngine(fullScreen: false, framesPerSecond: 60);
        background = new ImageSprite(imageURL: "ms-appx:///Images/Background.png");
        background.Height = SnapsEngine.GameViewportHeight;
        background.Width = SnapsEngine.GameViewportWidth;

        cheese = new ImageSprite(imageURL: "ms-appx:///Images/cheese.png");
        cheese.ScaleSpriteWidth(SnapsEngine.GameViewportWidth / 20);

        bread = new ImageSprite(imageURL: "ms-appx:///Images/bread.png");
        bread.ScaleSpriteWidth(SnapsEngine.GameViewportWidth / 8);

        SnapsEngine.AddSpriteToGame(background);
        SnapsEngine.AddSpriteToGame(bread);
        SnapsEngine.AddSpriteToGame(cheese);
    }

    System.Random breadRandom = new System.Random();

    void resetBread()
    {
        bread.Bottom = SnapsEngine.GameViewportHeight;
        bread.Left = breadRandom.Next((int)(SnapsEngine.GameViewportWidth - bread.Width));

    }
    System.Random cheeseRandom = new System.Random();

    void resetCheese()
    {
        cheese.Top = 0;
        cheese.Left = cheeseRandom.Next((int)(SnapsEngine.GameViewportWidth - cheese.Width));
    }

    void gameLoop()
    {
        while (true)
        {
            SnapsEngine.DrawGamePage();
        }
    }

    public void StartProgram()
    {
        setupSprites();

        resetBread();

        resetCheese();

        gameLoop();

    }
}

Come back next week for more. And remember that you can get the full details of gaming with Snaps from my book:

 

 

 

 

 

 

Oresome Birthday

I stole this picture off their web site....

I stole this picture off their web site....

We had a significant birthday in the family this week. Ending in zero. No, not mine, that's next week. 

Anyhoo, as a present we organised a session at Oresome in Hull. You start with a strip of silver and finish three hours later with a really nice ring that you made yourself. It turns out that the process is more complex than you might think, and gives you a lot of respect for people who make things like this. 

It seems that the trend today is for experiences rather than just buying stuff. I reckon that this kind of thing does both. They are lovely folk at Oresome and took the trouble to make the event even more special for our lucky birthday person. And apparently you get to use a hammer, which is always good. Strongly recommended.

Bye Bye Windows Phone 8.1

Three and a half years ago I got the best phone I've ever had. A Lumia 1520 running Windows 8. Amazing screen, snappy performance, brilliant battery life, fantastic camera. And now Windows Phone 8 is dead. 

Sigh.

Ten years ago number one son was kind enough to bring me back a first generation iPhone from his travels in the US. It was the cheapest version with only 4G of storage, no 3G, no GPS, no proper email, and barely enough applications to fill the home screen. One of the first apps was a compass, that's how desperate they were to get things to put on there. And yet it was obviously the future.

I'd show mine to Microsoft folks at conferences and tell them it was going to eat their lunch and the response was usually that they knew this, but the folks upstairs didn't see it as a threat.  And anyway, how can you sell more copies of Windows and Office using a mobile phone? 

After a year version 2 of the iPhone brought 3G, GPS, an app store and proper email. I was second in the queue to get one at the O2 store. Around this time Microsoft seemed to think that the future was hexagonal menus.

I got more worried. I even emailed Steve Balmer (and, to his credit, got a response). But nothing happened. The version of Windows Mobile that was supposed to solve all problems and re-discover the high-ground was suddenly pulled and replaced with not a lot.

Finally, Windows Phone 7 came along. I loved it. A genuine attempt to move the field forward. Quite a few others loved it too. Not enough to get back the all market that Windows Mobile used to have, but enough to build a sizeable user-base. 

Then, just as Windows Phone 7 was starting to get traction (I thought), along came Windows Phone 8. This was a staggering technical achievement. We now had "full fat" windows running on a phone platform. And it worked really well. Snag was, it needed new hardware. And Windows Phone 7 owners suddenly found that they didn't own the future any more. 

And by now Android was taking over where Windows Phone should have been, picking up the 80% of the market that Apple don't tend to bother with. Microsoft hit back by making compelling, wonderful, devices like my Lumia 1520. But it was all for nothing in the end. 

I still have my Lumia 1520. It now runs Windows 10, wonderfully well. The user interface is streets ahead of my iPhone. I can't use it in real life of course, because lots of the things that I want to use on a daily basis are either unavailable or don't work properly. But I'm going to hang on to it to show people when they pull our their latest "wonder phone".

Spider-Man: Homecoming = good film

After yesterday's web picture, today finds us at the Spider-Man: Homecoming movie.

It's a good film. Probably the best Spider-Man film I've seen. Maybe it's the hyphen. But then again maybe it's Marvel, who seem to have a knack of creating slick tales with just he right amount of mayhem, slow bits, plausible (ish) baddies and plot twists. 

Everyone involved delivers and the Marvel universe is looking quite a nice place to be at the moment. 

Creating Cheese Lander in Snaps

Yes. Cheese Lander is back! I'll be using the mostly world famous game in my Monday Games Snap series to demonstrate how to make a Windows program using Snaps that will almost certainly not make you rich and famous. But it might make you poor and notorious. The aim of the game is simple. Move the bread to catch the cheese before it reaches the bottom of the screen. If you miss the cheese, you lose. If your bread is moving too fast when it hits the cheese, you lose. If I'm in a bad mood, you lose (actually this might not be the case). 

Last week we took a look at one of the Snaps sample game programs (read that before you read this). This week we're going to take that game code and move it into the "startup" program that runs when a Snaps application starts running. Then we're going to add some bread, cheese and a suitably starry background with a view to getting things moving next week. 

Open up the BeginToCodeWithCSharp sample project and take a look at the Solution Explorer in the top right hand corner:

Open the BeginToCodeWithCSharp project and then find the My Snaps Apps folder. Inside that you'll find a source file called MyProgram.cs. Open this file.

This is the StartProgram method that runs when a Snaps application starts up. It's the one that runs first because it is in a class called MyProgram and that is always the one that contains the initial StartProgram. Let's replace this StartProgram with the game code that we created last time:

public void StartProgram()
    {
        SnapsEngine.StartGameEngine(fullScreen: false, framesPerSecond: 60);

        ImageSprite ball =

                new ImageSprite(imageURL: "ms-appx:///Images/ball.png");

        SnapsEngine.AddSpriteToGame(ball);

        while (true)
        {
            SnapsEngine.DrawGamePage();
        }
    }

Copy the above method out of this text and paste it over the StartProgram in the Visual Studio editor so that it looks like this.

Now, when we run the program we get a nice round ball in the top left hand corner:

Balls are OK, but we want cheese. I've put all the image assets for the game into a file you can download from here. The images include the cheese, the bread, the starfield background and the game message screens. Download the zip file and put the images from it somewhere (I put mine on the desktop). 

Now find the click on the cheese in your download folder and then drag it into the Images folder for the solution. 

If you do the drag correctly you'll get a warning message because the demo programs (like all my products) come with cheese pre-loaded. However, we want to use the newer cheese because it is the right way up, so click yes to overwrite:

Now we can change our program to draw cheese rather than a ball.

ImageSprite ball =

        new ImageSprite(imageURL: "ms-appx:///Images/cheese.png");

Now, when you run the program you'll see a piece of cheese, rather than a ball. Yay!

Now drag all the other images assets into the images folder.If you find that moving individual items is too much of a drag (ho ho) you can select multiple items and drag them all at once. 

Now you've got the image assets, lets add the background to the game. We can use a new ImagesSprite for that. We can call the new sprite background:

ImageSprite background =
    new ImageSprite(imageURL: "ms-appx:///Images/Background.png");
background.Height = SnapsEngine.GameViewportHeight;
background.Width = SnapsEngine.GameViewportWidth;

This creates a new sprite called background. It also sets the width and the height of the sprite to match the size of the game viewport, so that it exactly fills the screen, which is just what we want the background to do. We can add the new sprite to the game, remembering that sprites are drawn in the order that they are added to the game. So we draw the background first, and then put the cheese on top.

SnapsEngine.AddSpriteToGame(background);
SnapsEngine.AddSpriteToGame(ball);

 

The complete program looks like this:

using SnapsLibrary;

public class MyProgram
{
    public void StartProgram()
    {
        SnapsEngine.StartGameEngine(fullScreen: false, framesPerSecond: 60);
        ImageSprite background =
                new ImageSprite(imageURL: "ms-appx:///Images/Background.png");
        background.Height = SnapsEngine.GameViewportHeight;
        background.Width = SnapsEngine.GameViewportWidth;

        ImageSprite ball =

                new ImageSprite(imageURL: "ms-appx:///Images/cheese.png");

        SnapsEngine.AddSpriteToGame(background);
        SnapsEngine.AddSpriteToGame(ball);
        while (true)
        {
            SnapsEngine.DrawGamePage();
        }
    }
}

Run this and we'll see some cheese against a starry backdrop. But the cheese looks a bit big for the screen. Now it turns out that a recent meeting of the International Standards Organisation for Cheese Related Gaming has standardised cheese and bread sizes for the cheese landing games as a twentieth of the width of the screen and an eighth of the width of the screen respectively. Fortunately the Snaps framework provides a method that will scale a sprite and retain it's aspect ratio:

ball.ScaleSpriteWidth(SnapsEngine.GameViewportWidth / 20);

Add the statement to scale your cheese, and then add the bread sprite and scale that too. This will put bread and cheese on the screen. Next week we'll make them move about a bit. 

See the Lego Batman Movie

The Lego Batman movie is way, way better than it needs to be. Hanging off two solid franchises (the clue's in the name) it could confidently expect to make a fortune just by showing up. At least for one episode at least. 

However, it is very, very, good. Just the right balance of action, story and knowing self-reference to keep everyone happy. I don't think I've laughed out loud at a film so many times for a while.

Very strongly recommended.

Reckitt Benckiser at c4di

David Keel of c4di explains what we are about

David Keel of c4di explains what we are about

Spent an afternoon talking about robots and chatbots. In other words, a good afternoon. We had folks from Reckitt Benckiser over to talk about a new partnership with c4di. We were showing off stuff what we had made and I brought along a few Hull Pixelbots. 

Another satisfice Hull Pixelbot customer...

Another satisfice Hull Pixelbot customer...

I really love it when I show someone a bit of tech and they go "Oh, so that means we could use this for......." That's a big chunk of what c4di is all about, and there was a lot of it about this afternoon.

Then we went straight into a Hardware meetup. It was great to see everyone come along, Ross brought his beautifully made Hull Pixelbots and we had a good natter about how to control them. 

Great stuff. 

Hull Pixelbot Rumble at c4di today (Thursday)

Finally, a blog post that isn't a thinly disguised plug for one of my books....

Anyhoo, today I'm plugging the Hardware Meetup at c4di tomorrow (Thursday). Ross is going to bring his robots, I'm going to bring some of mine, and we are going to do some robot things that might include rumbling.

You're welcome to come along and marvel. Sign up here. The meetups are in the ground floor of the awesome c4di building. We start at 6:00 pm and go on until 8:00 or until we run out of things to talk about (which usually means a bit later..)

If you've any interest in hardware (computer or otherwise) then it would be great to see you. 

Monday Games Snap

You might have heard of my Snaps framework. Then again you might not. I created it for my Begin to Code with C# book

The idea behind Snaps is that learning to create Windows 10 Universal Applications is hard because it takes a while to get to the point where you understand enough to have a bit of fun writing C#. 

So I created a bunch of helper functions that grew into an entire library for app and game development. 

As a celebration of being awarded another year as an MVP (thanks Microsoft) I thought I'd put the whole Snaps framework up on GitHub and then people can download and play with it, and maybe even take it further. 

Every week I'll take a Snaps function and explain how to use it, along with a bit of detail about how it works. Monday is now officially "Snaps Day". I'm going to start with the games creation framework and go on from there. 

This week I'm going to tell you how to get started and make your first sprite. You need Visual Studio 2015 or Visual Studio 2017 (it works with either). You'll also need to be running Windows 10 64 bit edition.

You can download the entire framework from GitHub. It's a single Visual Studio Solution that you just have to open. You might get warnings about the dangers of loading projects downloaded from the internet. Ignore those in this case. 

If you run the solution you'll get the Snaps main menu:

On the left you can select the book chapter. On the right you can pick an exercise from the book to run. Pick the one you can see above (Ch15_04_CompleteGame from Chapter 15). It's a complete game with moving sprites and all sorts. Click "Run and App" and the game will start. 

The graphics aren't the best to be honest. But you can control the game by the cursor keys, or mouse/touch on the cursor pad on the lower right hand edge. One sprite will chase you while the others look on. Everything is running under WPF and I'm very impressed with the performance. 

If you want to get ahead of the game you can grab the book and see exactly how the code works. For now though, use Visual Studio to stop the game (go back to VS and press the red Stop button), and take a look at the Solution Explorer on the top right. If you open up the Chapters folder you can find a folder for each chapter:

Open up a chapter and you can find all the sample code. The names of the samples are keyed to the ones you can find when you run the program. Take a look in Chapter 15:

Open up the one indicated (Ch12_02_BallSprite) It creates a single ball sprite and displays it.:

    public void StartProgram()
    {
        SnapsEngine.StartGameEngine(fullScreen: false, framesPerSecond: 60);

        ImageSprite ball = new ImageSprite(imageURL: "ms-appx:///Images/ball.png");

        SnapsEngine.AddSpriteToGame(ball);

        while (true)
        {
            SnapsEngine.DrawGamePage();
        }
    }

All the action takes place in the StartProgram method. It starts the game engine, creates a sprite and adds it to the game. Then, once we've made the sprites, we start a game loop which continuously draws the game page. It does this 60 times a second, because that is the frame rate requested at the start. The image for the ball is an asset which is in the Images folder in the project:

You can use your own images if you like. Drag them to the images folder and make sure that their build action is set to Content (as below). Then use the appropriate name to draw them.

If you run the framework and select this example (Ch12_02_BallSprite) you can see the ball on the screen. If you change the above code and use the handy "Run That Again" button you can see your changes. Don't worry about breaking the examples. You can always download a fresh set from GitHub if it all goes horribly wrong.

You might like to add more sprites, or fiddle with the properties of the existing sprite, in which case Intellisense is your friend:

See what adding :

ball.CenterX++;

.. to the while loop does. You should have:

while (true)
 {
        ball.CenterX++;
        SnapsEngine.DrawGamePage();
 }

If you make anything especially impressive take a video of it, send me the link and I'll start a hall of fame on this page. Because this is a Universal Application it can run on any Windows 10 device, including Xbox One and Raspberry Pi (although it is not as fast on the Pi)

Of course the best way to find out all these things (and learn to program as well) is to buy my book. 

But if you don't want to do that, then I'll have another Monday Snap for you next week.

Jeff Lynne's ELO are Awesome

I've liked ELO since, well, forever. And when I found out they were coming to Hull, I got tickets. They were playing at the KC Stadium. It's big. And it was full.

ELO did not disappoint. The support from The Shires and Tom Chaplin deserves special mention too for being excellent too. 

At the end they created enormous plumes of fire in time with the music. Awesome. 

A fantastic night that was only slightly let down by the one hour we spent in the car park trying to get out. The guide for the event said that "entry is gained by cash payment to the cash handlers". Sure enough, on arrival there were people in high-visibility jackets to take our money and direct us to our parking spaces. Unfortunately the guide also contained the ominous phrase "Vehicles exiting the car park are allowed to self regulate". What this meant was that come closing time all the chaps in high visibility jackets had disappeared (presumably to count their money) so there was nobody to direct the traffic, resulting in an enormous log-jam of cars going nowhere.  But it was still a great concert though.