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. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Ritesh, you should really write your own code instead of copy-pasting pieces from the internet and trying to make them work for you. No wonder you are so confused with C.
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    It's OK misterT... its my code he's trying to understand... I am trying to teach him C.. ( I'm not a very good teacher ). Hopefully we'll get there in the end..
     
  3. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Providing all the code for him is not teaching. You are teaching him to be lazy. I've been following the other threads too and I'm fed up how he doesn't put any effort to study. Even you said that structures and linked lists are too advance for him.. You have to be either stupid or extremely lazy not to learn structures in 30 minutes. Linked lists are another 30 minutes.
     
    Last edited: Apr 7, 2013
  4. dave

    Dave New Member

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


     
  5. koolguy

    koolguy Active Member

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

    OK, Now i am going step by step ( making code is so difficult sometimes)....
    The ball bouncing on screen will be my first step....
     
  6. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I got bored and was feeling kind of bad about what I said so I wrote you a very basic snake:

    Code (C):

    #define BUFFER_SIZE 100
    #define UP 0
    #define RIGHT 1
    #define DOWN 2
    #define LEFT 3

    #define RING_NEXT(INDEX)    ((INDEX+1)%BUFFER_SIZE)

    /* Snake coordinate buffer */
    int snake_x[BUFFER_SIZE];
    int snake_y[BUFFER_SIZE];

    /* Snake head and tail indices */
    int head;
    int tail;

    /* Function prototypes */

    /* Add a snake head to coordinates (x, y) */
    void add_head(int x, int y);

    /* Remove snake tail piece */
    void remove_tail(void);

    /* Move snake one step, 0 = up, 1 = right, 2 = down, 3 = left */
    /* if grow = 0 the snake does not grow. Otherwise it grows one piece */
    void move_snake(int direction, int grow);

    /* Calculates the length of the snake */
    int snake_size();

    void
    main(void)
    {
        head = -1;
        tail = 0;
       
        /* Initialize 5 piece long snake */
        add_head(50,46);
        move_snake(UP, 1);
        move_snake(UP, 1);
        move_snake(UP, 1);
        move_snake(UP, 1);
       
        while(1)
        {
            /* Now you can move the snake by calling the move_snake function */
            move_snake(UP, 0);
           
            /* This piece of code loops through the snake parts from tail to head */
            for (int i = tail; i != RING_NEXT(head); i = RING_NEXT(i)){
                /* x-coordinate */
                snake_x[i];
                /* y-coordinate */
                snake_y[i];
            }
        }
    }
    /******************************************************************************/

    void
    add_head(int x, int y)
    /* Add a snake head to coordinates (x, y) */
    {
        /* Make sure there is space in the buffer */
        if (snake_size() >= BUFFER_SIZE){
            return;
        }
       
        head = RING_NEXT(head);
       
        snake_x[head] = x;
        snake_y[head] = y;
    }
    /******************************************************************************/

    void
    remove_tail()
    /* Remove snake tail piece */
    {
        /* If snake is only one piece long, we won't remove its head */
        if (snake_size() <= 0){
            return;
        }
        tail = RING_NEXT(tail);
    }
    /******************************************************************************/

    void
    move_snake(int direction, int grow)
    /* Move snake one step, 0 = up, 1 = right, 2 = down, 3 = left */
    {
        /* Make sure we have enough space if the snake wants to grow */
        if ((snake_size() >= BUFFER_SIZE) && grow){
            return;
        }
       
        if(direction == UP) {
            add_head(snake_x[head], snake_y[head]+1);
           
        } else if (direction == RIGHT) {
            add_head(snake_x[head]+1, snake_y[head]);
           
        } else if (direction == DOWN) {
            add_head(snake_x[head], snake_y[head]-1);
           
        } else if (direction == LEFT) {
            add_head(snake_x[head]-1, snake_y[head]);
        }
       
        if (!grow) {
            remove_tail();
        }
    }
    /******************************************************************************/

    int
    snake_size()
    /* Calculates the length of the snake */
    {  
        return (tail<head) ? ((head-tail)+1) : ((BUFFER_SIZE-head+tail)+1);
    }
    /******************************************************************************/
     
    I did not test it so it may/will have bugs.
     
    Last edited: Apr 7, 2013
  7. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    What is going on???
    Over my head you all are expert programmer....anyway i am reading it again and again.!!
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    You need to make your mind up!!

    Teaching people like Ritesh is what I like to do... If didn't want to do it I would stop...

    You shouldn't post untested code misterT... The code you posted won't compile... You have defined
    Code (text):
    int tale;
    as global

    Yet you are using tail in your functions...

    Other than that!! Good example.... We need to teach Ritesh to walk first!!!
     
  9. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I know.. I know :) But, like I said.. I was bored and got inspired to write this. Nice little project for sunday.

    True, but I don't have anything to test it on right now. And it is a simple little program so the bugs are easy to fix.. I keep editing the code.

    What do you mean? I don't see what is the problem using global variables in a function.
    EDIT: Ok.. got it. It was a typo. Fixed.
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    MisterT Believe me when I say that I'm not having a go... Your input to this site is extremely valuable...

    I am just able to tolerate people who seem to find it hard to grasp the syntax of C... We all have to start somewhere.

    Put it this way... I would cost a lot of Rupees to fail a college course.... If I can help I will....
     
  11. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I like to help people too.. that is why I like ETO. Keeps me refreshing my knowledge and I keep learning new things almost from every thread here. I do get frustrated sometimes when I try to help and there is no effort from the other side. I'm not saying that Ritesh has no effort.. I think he is too excited to get things done that there is no patience for original work..
     
    Last edited: Apr 7, 2013
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    We diverse..... Lets try to keep the thread on track...
     
  13. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    Hi
    I am thinking to bounce the ball is there any need of eq of motion?
     
  14. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    In the code I supplied there is a bouncing ball demo.... What's eq of motion?
     
  15. koolguy

    koolguy Active Member

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

    I was just asking is there any need of using equation for it???
    can you please explain me the method of doing it (working ).
     
  16. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    I just increase x and y by one if y gets bigger than height... decrease y.. If x gets bigger than the width decrease x..
    bounce.png

    I used a direction control... either 1 or -1.. Then toggle the direction when you hit an edge..
     
    Last edited: Apr 9, 2013
  17. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    But how to make this in code...?
    with for loop and ++ and -- Little confusing..
     
  18. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    If we talk about pix what it is value and from where it comes??
     
  19. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    No.. Its not

    Code (C):
    struct{
       signed  int x;
       signed int y;
    } ball;

    If ball.y > height - ballheight
       y_direction = -1;
    if ball.y < 0
       y_direction = 1;
    If ball.x > width - ballwidth
       x_direction = -1;
    if ball.x < 0
       x_direction = 1;

    ball.x += x_direction;
    ball.y += y_direction;
    This way the ball will stay in play..
     
  20. koolguy

    koolguy Active Member

    Joined:
    Aug 24, 2010
    Messages:
    2,196
    Likes:
    9
    OK, I am just thinking how to use i mean setting parameters for ball height and height lite bit confusing....
     
  21. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,305
    Likes:
    914
    Location:
    Rochdale UK
    When you check the new system in your other thread .... IF you get it working you will see the bouncing ball for your self... You can check out MY code for this simple trick..
     

Share This Page