The point of this game is to direct the train to the correct station by toggling the railroad switches. Each of the switches in the green circles has two available settings (positions) - for example 'up' and 'left'.
Once the train makes it to the station, you get a point and a new destination is selected. How many stations can you visit before the time runs out?
It is a simplified remake of a minigame featured in the 1984 Commodore 64 hit "Donald Duck's Playground". The original is much more playable than my version and looks better, but I'm pretty sure it has literally thousands of lines of machine code:
Interestingly enough, the game was ported a couple of years later to 16-bit platforms, but never to other 8-bit machines (Atari/Spectrum etc.)
The full source code is below and here is a brief explanation:
Lines [1-6] set up the HTML5 Canvas.
[7-8] The background image:
(more about this below).
[9-10] The spritesheet used to draw the train and the switches:
[11-12] 'Click' and 'Point' sounds.
[13-25] The railroad grid (map):
I shamelessly ripped the C64 image:
and converted it into a grid:
So the JavaScript grid is an exact replica of the original.
These small images (320x96) took up practically half of C64's screen - it's highest (and only) resolution mode was 320x200. To make it more readable on modern screens, I doubled the size of a grid element from 8x8 to 16x16.
The game program doesn't really use the dots or horizontal/vertical rails - theoretically we could replace them with spaces or any other character.
But I kept them because the background drawing program (below) does use the rails and the dots made it easier for me to draw the grid.
[26-32] Coordinates and alternative settings of the switches.
[33] Time allotted for one round.
[34] The number of cars (the locomotive counts as a car).
[35] The offset of the train images in the tiles spreadsheet - when drawing the train, the program will skip the four arrow images.
[36] Station names.
[37] Size of every tile (they're all square).
[38] An array of objects - each object is an independent vehicle.
[39] 'grid2' is basically a copy of the 'grid' created only for technical reasons. 'grid' is an array of strings and unfortunately there is no easy way to replace one character in a string.
I decided to split every string into substrings of characters. When a switch setting is changed, we update just that one character.
[40] Counter - elapsed time.
Mode - game mode (active play or the 'game over' screen)
targetStation - current destination
points - total points so far
[42-45] The function that changes the canvas dimensions to the window dimensions.
[47-49] Randomly select a new destination.
[51-65] Set initial values of variables when the game starts.
[67-146] Main game loop:
[68] If the game is active (ie. we're not in the 'gameOver' mode):
[69] Draw the background to erase the previous animation from the screen.
[70-88] Draw all the switches (arrows) using the spritesheet. We calculate the offset of the arrow image (left/right/etc.) corresponding to the letter in grid2.
[86] Copy the arrow image from 'tiles.png' to the canvas.
[89-92] Draw the cars on the canvas.
[93-102] Check if the locomotive reached the destination. If so, play a sound, add a point and select a new destination.
[103-124] If a car arrived at a switch, change the direction of the train accordingly.
spriteRow determines if in lines [103-105] the train is drawn pointing up/down etc.
[125-129] If it's a railcar, realign it to the previous one. This prevents them from separating if the player changes the switch between the locomotive and the railcars.
[131-132] The train keeps on moving.
[135-136] The time keeps running out.
[137-139] Update the scoreboard.
[140-143] Switch to gameOver mode if the time ran out.
[145] Get ready for the next animation frame.
[148-170] Handle mouse clicks and touch screen:
[150-151] Since the 'resize' function modified the display dimensions of the canvas, we have to translate the coordinates of the event to our original 608x208 image.
[152-165] Have any of the green buttons been clicked? If so, switch the current value of the button with the alternative one [165-167].
[166-167] If we're in gameOver mode, restart the game.
[173-174] Split grid into grid2 - an array of 1-character strings.
[177-178] Cosmetics.
[179-180] Let's start!
Below is a small code snippet that generates the background image.
It has its own tile set:
Theoretically this code could become part of the game and be executed in every animation frame (instead of a one-time import in lines [5-6]), but that wouldn't be very efficient.
Please also note that this image can not be transparent in the game (otherwise the train would leave an ugly trail). If you just run the program below and right-click to save the image, it will save as a PNG with transparent background.
To eliminate the transparency, I just saved it as JPG.
This section in gray font is just a small rant about the buttons. Feel free to ignore it.
Initially I started out with two grids - one for the stations and switches (this grid remains unchanged in the current version of the game) and another one for alternative settings for the switches.
The player could only toggle the switch by clicking the exact box in the grid. The program was shorter (and I think more elegant), because it just converted the click coordinates to grid coordinates and checked if the second grid has a value in the given box. There was no need to calculate any distance.
However I quickly noticed that this solution does not work well on mobile devices - it is difficult to click exactly the right box. So instead of the second array of characters, I introduced an array of objects ('buttons'), and the program now calculates the distance between the click and each button. This allows us to 'accept' clicks which are close, but not necessarily exactly on the grid element with the switch. The 'acceptance' area is represented by the green circles.