Continue to Site

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.

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

How to make snake on LEd display....

Status
Not open for further replies.
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.
 
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.

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

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:
OK, Now i am going step by step ( making code is so difficult sometimes)....
The ball bouncing on screen will be my first step....
 
I got bored and was feeling kind of bad about what I said so I wrote you a very basic snake:

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:
What is going on???
Over my head you all are expert programmer....anyway i am reading it again and again.!!
 
Providing all the code for him is not teaching. You are teaching him to be lazy.
I got bored and was feeling kind of bad about what I said so I wrote you a very basic snake:

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:
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!!!
 
You need to make your mind up!!

I know.. I know :) But, like I said.. I was bored and got inspired to write this. Nice little project for sunday.

You shouldn't post untested code misterT... The code you posted won't compile...

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.

You have defined
Code:
int tale;
as global

Yet you are using tail in your functions...

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

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

If we talk about pix what it is value and from where it comes??
 
No.. Its not

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..
 
OK, I am just thinking how to use i mean setting parameters for ball height and height lite bit confusing....
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top