I seen many videos in U-tube of effect/animation like snake .......
How to make these classic snake moving effect on display like here ...what is code algorithm to move it?? https://www.youtube.com/user/theFloe1?feature=watch
For a snake algorithm you need a data-structure to hold the coordinates of each body "pixel". Then you just move the last talepiece as a new headpiece (up, down, left or right of the last headpiece. Depending on the direction you want the snake to move).. and lastly check for collisions.
I would make the snake a linked list and allocate enough links in advance.
When you draw the snake you just traverse the linked list and plot a pixel in your LED diplay buffer on each snake-piece coordinates.
No.. you can do linked lists in C. Google for "linked list in C".
C:
/* Structure for snake parts */
struct part
{
int x_pos, y_pos;
struct part *next_part; /* Pointer to next part */
struct part *prev_part; /* Pointer to prev part */
};
/* Snake structure */
struct snake
{
struct part *first_part; /* Pointer to the first part of the snake */
enum Estate {RIGHT, LEFT, UP, DOWN} state; /* State of the snake */
};
Didn't give this too much thought yet, but I think without structures (linked list) keeping track of the snake "body parts" gets very messy and complicated. Linked lists are the way to go. Or maybe a ring-buffer..
Ritesh: If you don't have the time or patience to really learn C-programming, I think you should forget this snake thingy. Maybe do something simpler first.
OK, tell me any simple problem which can be solved by struct.
I have readied my C book but i am not getting what to make using struct because in book the example are shown like in bank_data containing data all array char, int,etc like that....
OK, tell me any simple problem which can be solved by struct.
I have readied my C book but i am not getting what to make using struct because in book the example are shown like in bank_data containing data all array char, int,etc like that....
Is this to be implemented on a microcontroller? If you're going to store coordinates of each dot within the snake's body, then a FIFO is more suitable than a linked list.
Is this to be implemented on a microcontroller? If you're going to store coordinates of each dot within the snake's body, then a FIFO is more suitable than a linked list.
I can't quite see that. Could you explain how this would work.. assuming the snake eats an apple and grows 5 pixels... or not, if you assumed the snake is fixed length.
I can't quite see that. Could you explain how this would work.. assuming the snake eats an apple and grows 5 pixels... or not, if you assumed the snake is fixed length.
The << means shift left and the >> means shift right. They are the equivalent of multiplication and integer division by a power of 2. e.g. x << 1 multiplies by 2, whereas x << 2 multiplies by 4 and x >> 2 divides by 4
The modulus (x % 8) extracts the lower 3 bits of the pixel x position. These 3 bits have a value from 0 to 7 and address the bit within the byte to be modified. This is done because each pixel is stored as a bit in a byte array.
Thanks for helping Dougy.... I may not be the best at explaining...
XOR... This takes the existing LED status and only toggles the one that is selected.. ie if the LED is on it will be switched off.
AND... This takes the existing LED status and switches it off ( you need to complement the LED data first ).
OR..... This just switches the LED on even if it was on.
If you study the datasheets for graphical screens.. These are the functions provided.