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.
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..
 
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:
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
	}
 
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.
 
Hi,

How to do this??

I think link list is made in C++?

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 */
};
 
Last edited:
No.. you can do linked lists in C. Google for "linked list in C".

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

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

Ok.. I wrote the struct for you, but your task is to write the add_pixels -function.. see comments in the 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: **broken link removed**
 
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 normal process for moving a snake is:
Code:
snakeFifo.push(headLocation);
snakeFifo.pull();
The process for the snake growing is (notice the pull is absent):
Code:
snakeFifo.push(headLocation);
 
Code:
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???
pix = x%8;
use of this modulus??
 
Last edited:
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.
 
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
}


Thanks for explaining it.....
This part also need to be explained!!
 
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.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top