During the week of abnormal programming (as
noted by phpcmsdev ) decided to write a game in 30 lines of code. Since Tetris, Snake and Arkanoid have already been made, the choice fell on the racers that were included in the standard set of
portable games .

Link to
fiddle .
(function(elid, width, height, speed, strength){ var canvas = document.querySelector(elid), ctx = canvas.getContext("2d"), pos = 0, blocks = []; canvas.width = width; canvas.height = height; ctx.fillStyle = "black"; var game = setInterval(function(){ if( Math.random() < strength) blocks.push([Math.random()*(width-10),-10]); ctx.clearRect(0,0,width,height); ctx.fillRect(pos,height-50,10,40); for(var i = 0; i < blocks.length; i++){ ctx.fillRect(blocks[i][0],blocks[i][1],10,10); if( blocks[i][1] > height - 60 && blocks[i][1] < height - 10 && Math.abs( pos - blocks[i][0]) < 10 ){ clearInterval(game); alert("Game over. You have " + Math.floor(1000 * strength) + " points."); } if( blocks[i][1] > height - 5 ){ blocks.splice( i, 1); i--; } else { blocks[i][1] += 5; } } strength += 0.001; },speed); document.addEventListener('mousemove', function (e) { pos = (e.pageX > 0) ? ((e.pageX < width) ? e.pageX : width-10) : 0; }, false); })("#canvas",400,300,33,0.05);
')
Special features
- Truly 30 lines of code: 1 line of HTML, 29 lines of JavaScript. True, without wrapping it in the
<script>
, but I think this assumption is not so scary :). - Canvas. I love Canvas! Drawing simple shapes on canvas is definitely easier than moving divs. It turns out not so beautiful, but you do not need to worry once again about the displacement of elements.
- Increasing difficulty and points. In fact, this is one variable, but who cares how the points are considered? The main thing is that there would be more!
- setInterval. It was decided not to refuse requestAnimationFrame because of the extra line, but because the game logic is described together with the drawing.
Conclusion
Thanks to
jeston for the
info on Tetris,
zag2art for the
translation about Excel,
DjComandos for
Snake and
linoleum for
Arkanoid .
Guys, do some normal programming.
UPD
The user
MinimaJack, in turn, sent a version of the game Ball, but he cannot post it himself due to objective reasons.
Link to
fiddle .
