PROGRAMMING CHALLENGE
specification by Edward de Jong, version 1
Screenshot:
:
The game runs at 6 frames per second, and the snake moves one cell per frame. If the snake moves into the apple the length is increased by one, a crunch sound is emitted, and the apple is moved to a new cell. If the snake crosses over itself, then a beep is emitted and the part of the snake from that cell onward is erased. The snake wraps around the board in all four directions.
The playing board, drawn solid black, is subdivided into square cells of 42 points at the screen resolution. Since the window size will not be an even number of cells, the cells are stretched slightly so that all screen space is used by the grid. At the start, the snake is set to length 1 and positioned at cell (4,4). The apple, which is the goal of the snake to eat, is one cell drawn as a circle in HTML color crimson, and is placed at a random location on the board.
At the start of the game the snake is paused. As soon as an arrow key is pressed, the snake begins to move in that direction, and leaves a trail behind. Initially the trail is limited to 5 cells total. If the snake moves into the Apple, the snake's length grows by 1. Once the apple is eaten, it is immediately respawned into a random new cell. The apple may end up on occasion placed inside the body of the snake, however, the snake only is considered to have eaten the apple if the head moves onto the apple. The snake cells are drawn as 40 point squares with 2pts on the right and bottom to create a separation. The snake cells are drawn in alternating colors lime green and lawn green. The head of the snake is drawn as a rounded rectangle with a corner radius of 8 pt and a border of 2 pt dark green. The remaining cells of the snake are drawn as a rounded rectangle with 2 pt corner radius. If the snake cells had to be reduced from 42 pt due to window size limitations, then the radii and border thickness are reduced accordingly.
The only input to the game is the keyboard. The arrow keys change the direction of the snake, however, to prevent frustration the player is not allowed to move the snake head back into itself. So if the snake is traveling east, any attempted movement west is ignored. A command to move in the direction already in motion is ignored. To permit fast maneuvers the direction inputs are queued, so that the player can do WEST - SOUTH - EAST to perform a 180 degree turn in 3 consecutive frames. Pressing the space bar pauses or resumes the game.
As the snake grows and shrinks, the current size and high score is reported at the top of the screen as a topmost layer at 30% opacity, centered in 28 pt black text, for example: "8 high:22".
The default starting window size is 700 x 700 interior pixels. If the user resizes the window, the game is reset back to the starting state.
Note: since the window size will not usually be an even multiple of 42 points, the cells are slightly stretched to not have any dead space leftover. If the given window size is too small to accommodate at least 10 cells in both directions, the cellsize is reduced. So the program must first figure out how many the proper cell size such that at least 10 cells can fit in the X and Y directions, but no larger than 42 pts in size, then divide the width and height by the number of cells in each direction, and then pad the cells by the leftover amount so that the cell grid fills the space perfectly.
External resources:
n crunch.mp3 sound
n beep.mp3 sound
n you are given a window size in pixels, and the resolution of the device.
o device resolution might vary from 86 dpi (desktop), to 500+ dpi (mobile)
An example of a subset of this game, written in real time in under 5 minutes in JavaScript can be seen on youtube at: https://www.youtube.com/watch?v=xGmXxpIj6vs
The 5 minute coding example has no scaling of the grid, doesn't keep score, and a few other things, but does show impressive coding speed.
Edward
I have ported the original js code to Haxe, it's possible it
could be improved, Timer may not be ideal, but I did not want to
spend too long, others are free to improve. Obviously this is
very Javascript Haxe specific version I could look at cross target
implementations but this should atleast show differences between
Javascript and Haxe Javascript.
https://try.haxe.org/#D0324
I don't have enough interest to create an extended version, but should give you ideas on using Haxe.
Best
Justin