1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

How to make snake on LEd display....

Discussion in 'Microcontrollers' started by koolguy, Apr 2, 2013.

  1. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    The code I posted on the other thread had the line algorithm so you could make the snake game...

    When you get to this size of LED... ie 100x100 or so.. the screen can be manipulated EXACTLY the same way as the common LCD screens...

    The snake game is just lines.. you control the direction using a keypad... it's not a very difficult program..
     
  3. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,

    I think you are talking of this....
    but i am not getting how to make snake can you give me hint for coding it...




    Code (text):

    void pixel(signed char x,signed char y,int cond)
        {
        int tmp;
        char pix,msk;
        if(x<0 || y<0) return;          // outside drawing limits negative
        if(x>31 || y>7) return;         // outside drawing limits positive
        tmp = (y << 2) + (x>>3);        // Linear position
        pix = x%8;                      // pixel required
        pix = pow[ pix];
        msk = backbuffer[tmp];          // get exsisting data
        if(cond == 2)
            pix ^= msk;                 // XOR data to screen
        if (cond == 1)
            {
            pix = ~pix;
            pix &= msk;                 // AND data to screen
            }
        if(cond == 0)
            pix |= msk;                 // OR data to screen
        backbuffer[tmp] = pix;          // apply changes
        }
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland

    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.
     
  6. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi,
    How to do this??
    I think link list is made in C++?
     
  7. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    No.. you can do linked lists in C. Google for "linked list in C".

    Code (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 */
    };
     
     
    Last edited: Apr 4, 2013
    • Like Like x 1
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    You should have stayed with the pixel at the rear now at the front method... I think structures are quite a way off for Ritesh..
     
  9. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    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.
     
    Last edited: Apr 4, 2013
  10. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    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....
     
  11. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Ok.. I wrote the struct for you, but your task is to write the add_pixels -function.. see comments in the code.

    Code (C):

    struct pixel
    {
        int x; // x-coordinate
        int y; // y-coordinate
    };

    void main
    {
        /* declare two pixels */
        struct pixel p1;
        struct pixel p2;
       
        /* assign values to the pixels */
        p1.x = 2;
        p1.y = 10;
       
        p2.x = 5;
        p2.y = 7;
       
        /* add the coordinates of p2 to p1 */
        add_pixels(&p1, &p2); // WRITE THIS FUNCTION, the structures are passed as pointers.
    }
     
    Here is some reading: http://beej.us/guide/bgc/output/html/multipage/structs.html
     
  12. dougy83

    dougy83 Well-Known Member

    Joined:
    May 18, 2008
    Messages:
    2,672
    Likes:
    215
    Location:
    Brisbane, Australia
    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.
     
  13. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    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.
     
  14. dougy83

    dougy83 Well-Known Member

    Joined:
    May 18, 2008
    Messages:
    2,672
    Likes:
    215
    Location:
    Brisbane, Australia
    The normal process for moving a snake is:
    Code (text):
    snakeFifo.push(headLocation);
    snakeFifo.pull();
    The process for the snake growing is (notice the pull is absent):
    Code (text):
    snakeFifo.push(headLocation);
     
  15. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Code (text):
    tmp = (y << 2) + (x>>3);        // Linear position
        pix = x%8;                      // pixel required
        pix = pow[ pix];
        msk = backbuffer[tmp];          // get exsisting data
        if(cond == 2)
            pix ^= msk;                 // XOR data to screen
        if (cond == 1)
            {
            pix = ~pix;
            pix &= msk;                 // AND data to screen
            }
        if(cond == 0)
            pix |= msk;                 // OR data to screen
        backbuffer[tmp] = pix;          // apply changes
        }

    tmp = (y << 2) + (x>>3); // Linear position

    How this is working i mean >> greater than/ larger than in this equation???
    use of this modulus??
     
    Last edited: Apr 6, 2013
  16. dougy83

    dougy83 Well-Known Member

    Joined:
    May 18, 2008
    Messages:
    2,672
    Likes:
    215
    Location:
    Brisbane, Australia
    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.
     
  17. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9

    Thanks for explaining it.....
    This part also need to be explained!!
     
  18. dougy83

    dougy83 Well-Known Member

    Joined:
    May 18, 2008
    Messages:
    2,672
    Likes:
    215
    Location:
    Brisbane, Australia
    It does exactly what the comments say. Try thinking about it for yourself.
     
  19. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    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.
     
  20. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    What about this cond ??
    from where it is coming?
     
  21. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    You pass it from the calling routine.... cond can be 2, 1 or 0 depending on what function XOR, AND, OR respectively..
     

Share This Page