[A83] Re: nibbles / snake


[Prev][Next][Index][Thread]

[A83] Re: nibbles / snake




You might check out my Snake86 that I wrote a couple of years ago.  It was
basically a sit down and figure out how to write nibbles.  The code is
pretty small (around 250 lines), and should be easy to understand:

http://david.acz.org/prog.html

It has one bug that I never bothered to fix: the dot can appear on top of
the snake's body, and thus you never see it.  Perhaps a good exercise would
be to fix the bug and port it to the 83 :)

It checks collision with the screen buffer, so if you wanted to add levels,
you just draw them, and it will automatically detect collisions.  That is
how it detects collisions with the snake body and walls.  The dot is checked
before collisions.

It keeps track of the body by using a ringbuffer.  A ringbuffer is an array
with a pointer for the front, and a pointer for the back.  A ringbuffer is
also called a FIFO queue, which would be different than a LIFO queue,
otherwise known as a stack.

When the snake is moving, both pointers are updated.  The head pointer is
incremented, and the new position of the snake is inserted.  The new pixel
is also drawn on the screen.  The tail pointer is incremented, and the last
position is removed.  The last pixel is erased from the screen.

To grow the snake, there is a grow counter.  Every frame that it is not 0,
it is decremented, and the snake is grown.  When it is growing, the tail
pointer is not changed.  Thus the front moves, but the back doesn't, so it
gets longer.

If the snake gets too long, then the back will reach the front.  But the
buffer is of sufficient size that it should never happen.  To keep it a
"ring", when either pointer reaches the end of the buffer, it is wrapped
around to the beginning.  This is done by resetting a high bit of the high
address byte.  I believe the code in there is wrong (bit 4 would make it
$8000, not $9000), but it works anyway, since the whole page is free on the
86.

> I'm going to make my own nibbles/snake clone. But I got
> a lot of questions for that:





References: