Click the left/right/up arrows or press cursor left/right/up to control the game.

# Tunnel run game

WARNING! This tutorial contains explicit math! Viewer discretion advised!

The idea of the game is simple: run and jump through the maze avoiding the black areas.
What makes it look cool is that the maze is wrapped inside a tunnel.
The whole trick is handled by the function tunnelCoords.
If you don't feel like digging into the details of the math, feel free to skip to the trigonometry-free section. Our game is really played on a 2d grid, but this function displays it as a 3d tunnel.

It's easier to understand when you play around with the two canvases below: if you click on the top one, a white rectangle will be drawn where you clicked.
At the same time, a smaller corresponding rectangle will be drawn on the bottom canvas.
The top canvas uses the standard JS coordinate system - the point (0,0) is in the upper left corner. The x coordinate increases as you move to the right. The y coordinate increases as you move down.
The bottom canvas uses our tunnel 'coordinates'. The tunnel consists of concentric "circles" (actually they're 16-angle polygons - "regular hexadecagons", but who gives a damn?). For x=0, the point is at the bottom (6pm) of the circle. As the x coordinate increases, the point moves counterclockwise on the circle.
The y coordinate determines which of the circles the point is on. The higher the y coordinate, the smaller the circle.
The circles for y=0 and y=1 do not fit on our canvas (they're too big). The largest circle you see is for y=2.
Try drawing a line on the top canvas from (0,1) [upper left corner] to (0,10) [bottom left corner]. (The coordinates of the points on both canvases are shown without any parenthesis.) You should see corresponding points going from 6pm towards the center of the tunnel.

Here is the program that draws these canvases:



Here's what the program does: it draws lines from (0,0) to (1,0), then to (2,0) etc on the top canvas - these create a horizontal line.
Then it gets the corresponding tunnel coordinates and draws them on the bottom canvas - they form a circle.
The rotation based on the original x coordinate happens using the sin/cos functions in lines [28-29].
The size of the circle is determined in lines [23-24] and [26]. [26] is what changes the distance between the circles - as the circles gets smaller (farther away from you), the distance between also decreases. This creates the illusion of the perspective.
Then the program increases the y coordinate and draws the lines again, in a darker color.
I purposefully stopped at x=14, so that you can see the incomplete circles to better understand how the horizontal lines correspond to the circles. In the game of course we're drawing the full circles to make sure our tunnel looks real.
Congratulations! You've battled through the hard part!

Let's look at the simpler parts of the code.

Lines [1-24] set up the HTML5 Canvas and declare the constants and the variables.
[26-29] Resize the canvas in case the user resizes the window or rotates the mobile device.
[44-67] (Re-)set the variables to the initial values (beginning of game)
[69-96] Draw a single tile. Point1 is in the upper left corner of the tile, using the classic coordinates system. Three more points are calculated, creating a square with a side length equal to 1. These points are then converted to tunnel coords and the tile is drawn.

[98-156] Main animation loop:
[100-104] The sprite is moved on the x axis (resulting in rotation of the tunnel)
[108-114] Draw all the tiles starting from one row below the sprite.
[115-122] Vertical jump
[124-132] Calculate and draw the current sprite animation frame from the spritesheet
[133-138] If the sprite is on a 'hole' in the grid, game over.
[141-146] If the sprite reached the finish line, you win.
[158-171] Handle the mouse and touch even - trigger left/right/jump based only on the x coordinate of the click/touch
[173-189] Handle the keyboard input
[191-194] Key/click/touch released
[202] Let's go!!!

Here's the full code:



GitHub repo
All the code is provided under the MIT license

Check out these programming tutorials:

JavaScript:

Optical illusion (18 lines)

Spinning squares - visual effect (25 lines)

Oldschool fire effect (20 lines)

Fireworks (60 lines)

Animated fractal (32 lines)

Physics engine for beginners

Physics engine - interactive sandbox

Physics engine - silly contraption

Starfield (21 lines)

Yin Yang with a twist (4 circles and 20 lines)

Tile map editor (70 lines)

Sine scroller (30 lines)

Interactive animated sprites

Image transition effect (16 lines)

Your first program in JavaScript: you need 5 minutes and a notepad

Fractals in Excel

Python in Blender 3d:

Domino effect (10 lines)

Wrecking ball effect (14 lines)

3d fractal in Blender Python