Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
.model small
.data
snake dw ?,3,offset snake+18,-160,40,0,2000, 2160, 2320, 2480, 100 dup(0),0, -160,0,0,-2,0,2,0,0,160,25173,80*24*2
.stack 10h
.code
draw_elem: mov cx, 4
mov bp, 2403h
while: mov ax, snake
imul snake[240]
add ax, 13849
mov snake, ax
xor dx, dx
div snake[242]
mov si, dx
cmp es:[si], word ptr 2500h
jne while
mov es:[si], bp
mov bp, 200fh
loop while
ret
update: mov si, snake[4]
mov di, [si]
mov es:[di], 2500h
mov cx, snake[2]
for1: mov ax, [si-2]
mov [si], ax
sub si, 2
loop for1
mov di, [si]
add di, snake[6]
mov [si], di
cmp es:[di], 2403h
jne endif3
mov si, snake[4]
mov bx, [si]
mov [si+2], bx
add snake[4], 2
inc snake[2]
call draw_elem
endif3: cmp es:[di], byte ptr 0eh
ja game_over
cmp di, 0
jl game_over
cmp di, 80*25*2
jg game_over
mov es:[di], word ptr 06dbh
ret
main: cld
mov ax, @data
mov ds, ax
mov ax, 3
int 10h
int 1ah
mov snake, dx
mov ax, 0b800h
mov es, ax
mov ax, 2500h
xor di, di
mov cx, 80*25
rep stosw
call draw_elem
begin: xor ah, ah
int 1ah
cmp dx, snake[220]
jb endif1
add dx, 2
mov snake[220], dx
call update
endif1: mov ah, 6
mov dl, 0ffh
int 21h
mov bl, al
cmp bl, 48h
jb endif2
cmp bl, 50h
ja endif2
xor bh, bh
shl bx, 1
mov ax, [bx+offset snake-2*48h+222]
mov snake[6], ax
endif2: cmp al, 1bh
jne begin
game_over: mov ax, 4c00h
int 21h
end main
var h_pos=f_pos=score=0, len=5, move=1, snake=[0], dx=15, dy=10, dxy=150;
function _$ (body) { return eval("(function (a, b) { return "+body+"; })"); }
elem = _$("document.getElementById (a)");
set_class = _$("elem('cell_'+a).className = 'cell'+ (b ? ' '+b : '')");
is_snake = _$("elem('cell_'+a).className.indexOf(' s') >= 0");
new_food = _$("is_snake (f_pos = Math.floor(Math.random() * a) + b) \
? new_food(a,b) : f_pos");
new_cell = _$("'<div id=cell_'+a+' class=\"cell'+(a ? '': ' s')+'\"></div>'");
new_line = _$("a ? '<div class=line></div>' : ''");
line_change = _$("dx * (parseInt ((a + b%dx)/dx) - parseInt (a/dx))");
for (var i=0; i<dxy; ++i)
elem ("main").innerHTML += new_cell (i) +new_line(i%dx == dx-1);
document.body.onkeydown = function(e) {
move = ([-1, -dx, 1, dx])[e.keyCode-37];
};
set_class (new_food(dxy-2, 1), 'f');
var interval = setInterval (function () {
h_pos = (h_pos +dxy + move - line_change (h_pos, move)) %dxy;
if ( is_snake(h_pos) && !clearInterval(interval) )
alert('Game Over! Your score: ' + score);
set_class (h_pos, 's');
f_pos == h_pos && new_food(dxy-1, 0)>=0
&& ++len && ++score && set_class (f_pos, 'f');
snake.push (h_pos);
snake.length>=len && set_class (snake.shift());
}, 300);
<html>
<head>
<meta charset="utf-8">
<title>SNAKE</title>
<script src="snake.js"></script>
</head>
<body onload='main()'>
<canvas id="snake">mf</canvas>
</body>
</html>
function update (direction, snake, apple) {
var eaten = false,
gameover = false,
prev = snake[0],
current = {'x': snake[0].x + direction.x, 'y': snake[0].y + direction.y};
if (current.x === 15) current.x = 0;
if (current.x === -1) current.x = 14;
if (current.y === 10) current.y = 0;
if (current.y === -1) current.y = 9;
snake[0] = current;
if (snake[0].x === apple.x && snake[0].y === apple.y) eaten = true;
for (var i = 1, len = snake.length; i < len; i++)
if (snake[0].x === snake[i].x && snake[0].y === snake[i].y) gameover = true;
for (var i = 1, len = snake.length; i < len; i++) {
current = snake[i];
snake[i] = prev;
prev = current; }
if (eaten) {
snake.push(current);
apple.x = Math.random() * 15 | 0;
apple.y = Math.random() * 9 | 0; }
if (gameover) {
alert('GAME OVER!');
location.reload(); }
};
function draw (context, snake, apple) {
for (var i = 0, len = snake.length; i < len; i++) {
context.fillStyle = (i === 0) ? '#000' : '#666';
context.fillRect(snake[i].x * 32, snake[i].y * 32, 32, 32); }
context.fillStyle = '#933';
context.fillRect(apple.x * 32, apple.y * 32, 32, 32);
};
function main () {
var canvas = document.getElementById('snake'),
context = canvas.getContext('2d'),
snake = [{'x': 5, 'y': 3}, {'x': 4, 'y': 3}, {'x': 3, 'y': 3}],
direction = {'x': 1, 'y': 0},
apple = {'x': Math.random() * 15 | 0, 'y': Math.random() * 9 | 0};
canvas.width = 480; canvas.height = 320;
alert('GET READY!');
setInterval(function () {
context.clearRect(0, 0, canvas.width, canvas.height);
update(direction, snake, apple);
draw(context, snake, apple);
}, 1000 / 4);
document.onkeydown = function (event) {
if (event.keyCode === 37)
if (direction.x !== 1) direction = {'x': -1, 'y': 0};
if (event.keyCode === 38)
if (direction.y !== 1) direction = {'x': 0, 'y': -1};
if (event.keyCode === 39)
if (direction.x !== -1) direction = {'x': 1, 'y': 0};
if (event.keyCode === 40)
if (direction.y !== -1) direction = {'x': 0, 'y': 1};
};
}
main();
✗ ...
✔ пройти змейку до конца;
✗ ...

Крошечная змейка на JavaScript (30 строк кода)