1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#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,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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,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,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,}
};
#define MAXLENGTH 30 //snake's max length
/*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!");
}
}
}
|