Ingenious games in just 13KB of JavaScript

Ingenious games in just 13KB of JavaScript

Last Spartan screenshotI’m not fond of JavaScript but I am still in awe at the ingenuity of people who created a HTML5 game in JavaScript in just 13KB. There’s an annual competition called Js13K and this year there were 220 entries and prizes of $20,000 in total.

When you think that many graphics are way bigger than 13KB this is even more of an accomplishment. It means those games have to mostly draw their own. graphics rather than use prerendered graphical images. It helps that HTML5 includes a canvas drawing area and instructions in JavaScript to draw into it.

The GitHub Blog featured the top ten from this years competition. The picture comes from The Last Spartan which I really liked. Ironically the screen grab image was 121KB, almost eight x the size of the actual game!

If you are ever stuck for ideas for games, take a look at these. Being JavaScript, you can right-click in your browser and View Source (Chrome) or View Page Source (Firefox). Of course, to achieve 13KB, many of them have been minified which means getting rid of all spaces, carriage returns and renders it rather unreadable. This is what it looks like.

Screenshot of last Spartan source code

However the internet taketh and the internet giveth. If you paste this (or any of the source listings) into an online deminifier (aka beautifier) such as beautifier.io then you get all 1622 lines laid out in immaculately indented glory.  It’s actually 73KB in size as a normal listing and still very impressive.

There’s no reason why games can’t be done this way i.e. drawing graphics in C+SDL2 though I doubt I’d get them down to 13KB. My asteroids game which was 2200 lines of C generates a 96 KB exe. But when you add in images, SDL code and sounds etc it comes out at just under 35 MB.

A list of open source physics engines

A list of open source physics engines

Chipmunk Color matchIt’s not uncommon to have 2D games (and 3D) incorporate a physics engine. So when objects move and hit each other they behave realistically. The code that deals with “physical” interaction, objects bouncing or rolling off other objects is usually all parcelled up in a game physics engine.

Doing that means the programmer doesn’t have to worry about objects interacting. Your character moves into a room and knocks a vase; the vase falls over and breaks. Imagine how complex it would be if you had to program all the interactions. Instead, all objects in the room are predefined. As objects move and hit other objects they behave according to the predetermined rules. Balls drop to the floor and bounce. Breakable objects break.

An indie game studio called Tapir Games has put together a pretty comprehensive list of open source game physics engines. There’s even a couple in C though many are programmed in C++, C# and so on.

The picture comes from Chipmunk color match, one of the games using the (C library) Chipmunk physics library.

How to measure how shuffled a deck of cards is

How to measure how shuffled a deck of cards is

Opened deck plus new deck of cardsI first thought about this when I wrote the program to shuffle  deck of cards using a riffle shuffle. If you are given a deck of cards (or pack of cards as us Brits say), how do you discern just how shuffled the pack is? Can you calculate a numeric value for it say a % ranging from 0 to 100?

I believe it’s possible.  Here’s how.

  1. Start with a default pack of cards in perfect sorted order. Out of curiosity I found an unopened deck oif Waddington’s cards and opened it as the photos show. The cards in the pack were arranged in order King, Queen, Jack down to Ace in each of the four suits Heart, Clubs, Diamonds and Spades in that order. Let’s reverse the card rank ordering so a full deck starts with Ace Hearts through to King Hearts, Ace of Clubs to King of Clubs and so on with the last card being the King of Spades.
  2. Instead of referring to cards by their rank and suite lets just number them 0-51. 0= Ace of Hearts, 51 = King of Spades.
  3. When a deck is shuffled, each card can move to any other position. So a measure of shuffledness is calculating how far the cards moved in aggregate.
  4. However the card movements have to be “normalized”. Cards 0 and 51 can move to any of 51 positions while cards 26 (King of Clubs) and 27 (Ace of Diamonds) can only move a maximum of 26 places.
  5. I’m looking at the absolute value of a movement so if card 3 moves to position 47, it has moved 44 places and likewise card 47 moving to position 3 moves 44 (not -44) places.
  6. So to normalize a card’s move, divide its move by its maximum possible distance it can move. So wherever card 0 moves divide it by 51, card 1 by 50, card 26 ‘ move by 26.
  7. Sum up all 52 normalized move’s and multiply by 100. That is the measure of shuffledness.

I’ll write a C program to measure how shuffled a deck is and publish it in a day or two. Also here is a conversation on Reddit about shuffling cards.

 

The biggest problem with C

The biggest problem with C

Wordart
Created on wordart.com

The biggest problem is that a single letter language name like C (or D) makes it almost impossible to search accurately on the web. I did a search for C jobs and found 3,602 C jobs on TotalJobs.com. (I’m not looking for a new job BTW!),

Yeah I didn’t believe it either and when I looked at some of the jobs well there are a few but it also finds jobs for C# (not the same at all) or C/C++ which you just know is C++ really. So there’s probably less than a dozen jobs instead of 3602.

Also, job sites have this weird thing where they shows job adverts from multiple recruitment agencies so the same job gets advertised 3-4 times over. So my rule of job applications is divide the estimated number of found jobs by four to get a more accurate figure.

Being realistic, if I was looking for a job programming in C, I think the embedded market is probably the only main way to go and I know nothing about embedded C!  You have a far greater chance of getting a job if you program C++ or C# (if you are limiting it to programming languages that begin with C) rather than C itself.

C is one of those languages that’s really useful to know but won’t get you a job on its own.

It’s probably also the reason that longer programming languages like Go have adopted a convention of suffixing lang to their website name since about 2011. Shorts words like Go have multiple meanings and adoptions. I can’t imagine players of the game Go were too impressed when Go the programming language appeared yet at least the website is golang. The same is true for Dart which is not only best known for being a sharp pointed flying thing but is also an acronym (Dublin Area Rapid Transit) . The programming language website is dartlang.

 

 

 

Microsoft appears to reembracing game development again

Microsoft appears to reembracing game development again

Image from Microsoft learn games

They have just set up this webpage according to the announcement on one of their dev blogs. Of course there’s irony that they include MonoGame, because if it hadn’t been for MonoGame, all of the XNA library code would have been lost. XNA was Microsoft’s own game development technology that got to version 4 and was then abandoned.

There’s also a page of tutorials teaching Godot, Unity and a backend service called PlayFab. It never ceases to amaze me that Microsoft don’t invest just a little bit more into their games development. They’ve got lots of different platforms including various XBox consoles, PCs, Mobile and so on that can all be programmed using Microsoft related technologies using Microsoft supplied software that is very good and often free.  It often feels like they’ve backed off from the days when they were publishing games.

Creating a maze in C

Creating a maze in C

Backtracking mazeThe internet is well not quite awash with maze generators but there certainly are plenty of them about. This one is 100% written in C.

There’s a #define, commented out by default that enables prim’s algorithm. The default is to use depth-first search instead. If you uncomment the #define movie,. it will save out the maze as a series of .bmp files.

I’ve added the link on to the C Code Links page.

 

Curated C library

Curated C library

Rusty links
Image by Positive_Images from Pixabay

Back when I wrote the About.com website I created (curated!) a list of useful C libraries. You can still see if you look on archive.org, for instance this is it back in 2013 when I finished working on about.com.

Now that library was general stuff, not with a games (or loosely games related) like this site but many of the links still work. Back then most websites used http not https, so if a link doesn’t work, change the url to https and try it again. Some links will have ‘rotted’; its inevitable on the web (and I never miss a chance for a visual pun!) but all of it was open source so it probably has a higher chance of it still being around.

I will also be trying to salvage games/games related links from there and add them to my sites games links or C code links. For instance I found the CBM-64 BASIC and 6502 emulator in C by developer Michael Steil on GitHub and have added them to the C code links.

New C Tutorial published on arrays

New C Tutorial published on arrays

arrays
Image by Andrew Martin from Pixabay

This continues the series of C tutorials. In this one, I’m looking at arrays and how to declare them. An array is an essential part of C programming, but they are always fixed size. You can’t change the size at runtime or specify the size in a variable before declaring it unless you declare them using pointers which is a bit advanced for the fourth tutorial.

The picture is a metal grid but it illustrates what a two dimensional array [5][7] might look like. I looked but I couldn’t find any pictures of seven dimensional arrays!

Discussing the use of break in C

Discussing the use of break in C

C
Image by pramit marattha from Pixabay

On the C programming subreddit there’s a discussion where a student has been old he should never use break in C. As one comment says, “The teacher is a nitwit” and I agree with that comment.

I use break (a) to exit loops early and (b) to exit every branch of a switch statement unless I want it to drop through or else that case does a return. I use switch a few times in the asteroids source code and there’s examples of all three instances of using breaks, using returns and dropping through cases (no break).

Here’s an example of the latter. It controls the logic of what to display when you start or restart a level after losing lose a life. If you have one life left it prints “Last life” otherwise it prints how many you’ve got. Actually I think it’s a bit of an overkill and an if else would have done just as well

 

	switch (Player.lives) {
		case 1:sprintf_s(buffer, sizeof(buffer), "Last Life!");
			break;
		case 2:
		case 3:sprintf_s(buffer, sizeof(buffer), "Lives left: %d", Player.lives);
			break;
		}

So probably not the best example of using a switch! But it illustrates a point.