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
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
int height=50, width=100, fruitx, fruity,flag=0,score=0, program_shut_down=0;
int gameover=0;
enum symbols{WALL ='#', FRUIT='O', SNAKE='*'};
typedef struct snake
{
int x;
int y;
struct snake* next;
}snake;
snake* head=NULL;
void map()
{
int i,j;
snake* parcours=head;
system("cls");
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
char symbol = ' ';
if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
{
symbol = WALL;
}
else if (i == fruity && j == fruitx)
{
symbol = FRUIT;
}
else
{
while(parcours!=NULL)
{
if (i == parcours->y && j == parcours->x)
{
symbol = SNAKE;
break;
}
parcours=parcours->next;
}
parcours=head;
}
printf("%c", symbol);
}
printf("\n");
}
}
void input()
{
if (kbhit()) {
switch (getch()) {
case 75:
flag = 1;
break;
case 80:
flag = 2;
break;
case 77:
flag = 3;
break;
case 72:
flag = 4;
break;
case 'x':
gameover = 1;
break;
}
}
}
int controle_fruit()
{
int valide=0;
snake* parcours=head;
while(parcours!=NULL)
{
if(fruitx==parcours->x && fruity==parcours->y)
valide=1;
parcours=parcours->next;
}
return(valide);
}
void ajout_fruit()
{
srand(time(NULL));
do
{
fruitx= rand()%99 + 1;
fruity= rand()%49 + 1;
}while(controle_fruit()!=0);
printf("Hello y zebi");
}
void snake_movement(int mov_x, int mov_y)
{
int x=head->x;
int y=head->y;
head->x=head->x+ mov_x;
head->y=head->y+ mov_y;
snake* current = head->next;
while(current!=NULL)
{
int temp_x= current->x;
int temp_y= current->y;
current->x=x;
current->y=y;
x=temp_x;
y=temp_y;
current=current->next;
}
}
void program_shut_down_procedure()
{
printf("\nVoulez vous relancer une partie ?\n");
char c=getch();
if(c=='S'||c=='s')
program_shut_down=1;
}
void setup()
{
head=NULL;
head=malloc(sizeof(snake));
head->x=50;
head->y=25;
ajout_fruit();
flag=0;
score=0;
}
void ajout_tete_fruit()
{
snake* nouveau=malloc(sizeof(snake));
nouveau->x=fruitx;
nouveau->y=fruity;
nouveau->next=head;
head=nouveau;
}
void logic()
{
Sleep(100);
switch (flag)
{
case 1:
snake_movement(-1,0);
break;
case 2:
snake_movement(0,1);
break;
case 3:
snake_movement(1,0);
break;
case 4:
snake_movement(0,-1);
break;
default:
break;
}
if(head->x==fruitx && head->y==fruity)
{
ajout_tete_fruit();
ajout_fruit();
score+=10;
}
if(head->x==width)
snake_movement(-width+1,0);
if(head->y==height)
snake_movement(0,-height+1);
if(head->x==0)
snake_movement(width-1,0);
if(head->y==0)
snake_movement(0,height-1);
}
int main()
{
while(!program_shut_down)
{
setup();
while(!gameover)
{
map();
input();
logic();
}
program_shut_down_procedure();
}
return 0;
} |
Partager