Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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..
#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);
}
/******************************************************************************/
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:
int tale;
You need to make your mind up!!
You shouldn't post untested code misterT... The code you posted won't compile...
You have defined
as globalCode:int tale;
Yet you are using tail in your functions...
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.
What's eq of motion?
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.
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;