#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>
#define LEVELWIDTH 28
#define LEVELHEIGHT 30
// define the level with a 2d array. 1s are walls, 0s are the void where the snake moves
int nivel[LEVELWIDTH][LEVELHEIGHT] = { {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
{1,0,0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/*snake[0]['x'] //la cabeza de la snake
snake[x][0] = 0 // posicion x
snake[x][1] = 1 // posicion y */
int snake[MAXLENGTH + 1][2];int snakelength = 0;int vX,vY; // direction where the snake moves
void resetsnake()
{
snakelength = 0; // resets the snake to only its head
//resets to head only at the begginning of each level
snake[0][0] = LEVELWIDTH / 2;
snake[0][1] = LEVELHEIGHT / 2;
}
void increasesnake(int i)
{
if(snakelength+ i <= MAXLENGTH)
snakelength+=i;
else
snakelength = MAXLENGTH;
}
void decreasesnake(int i)
{
if(snakelength - i >= 0)
snakelength -=i;
else
snakelength = 0;
}
void movesnake()
{
int i;
for(i = snakelength ; i > 1; i--) //i < 0
{
snake[i][0] = snake[i - 1][0];
snake[i][1] = snake[i - 1][1];
}
snake[0][0] += vX;
snake[0][1] += vY;
}
int collisiondetect() // returns 1 if it detects collision with the wall or with itself, otherwise returns 0
{
int i;
int hit;
hit = 0;
//checks if it leaves the level
if(snake[0][0] < 0 || snake[0][0] > LEVELWIDTH || snake[0][1] < 0 || snake[0][1] >LEVELHEIGHT) hit = 1;
//checks if it reaches a wall
if(nivel[snake[0][1]][snake[0][0]] == 1) hit = 1;
//checks if it crashes with itself
for(i = 1; i < snakelength; i++)
{
if(snake[0][0] == snake[i][0] && snake[0][1] == snake[i][1]) hit = 1;
}
return hit;
}
#define TILE_WALL (1)
#define POWERUP_PILL (2)
#define POWERUP_FOO (3)
void moveup()
{
vX = 0;
vY = -1;
}
void movedown()
{
vX = 0;
vY = 1;
}
void moveleft()
{
vX = -1;
vY = 0;
}
void moveright()
{
vX = 1;
vY = 0;
}
//draws the snake onscreen
void draw()
{
int x,y;
printf("woot");
for(y = 0; y < LEVELHEIGHT; y++)
{
for(x = 0; x < LEVELWIDTH; x++)
{
switch(nivel[y][x])
{
case TILE_WALL:
printf("*",x,y); // ASCII character that makes up wall
break;
case POWERUP_PILL:
printf("&",x,y); // ASCII character that makes up powerup
break;
}
}
}
for(x = 0; x < snakelength; x++)
{
printf("*",snake[x][0],snake[x][1]); // ASCII character that makes up the snake's body
}
}
void main()
{
resetsnake();
while(1)
{
draw();
movesnake();
if(collisiondetect() != 1)
{
movesnake();
char key;
key=getch();
//checks which key the user presses to move the snake in that direction
if(tecla=='H') moveup();
if(tecla=='P') movedown();
if(tecla=='K') moveleft();
if(tecla=='M') moveright();
}
else
{
printf("You lose!");
}
}
}
If you have any questions regarding what any line does let me know. Please, ill appreciate any help i can getThanks
frankie


Sign In
Create Account

Back to top









