SAE11_2023/snake.c~

145 lines
2.9 KiB
C
Raw Normal View History

2023-12-12 16:07:54 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <graph.h>
#define LARGEUR 20
#define HAUTEUR 20
#define TAILLE_MAX 100
int main(void) {
int x, y, fruitX, fruitY, score, gameOver;
ChargerImageFond("../img/pixilart-drawing (2).png");
void initialiser() {
x = LARGEUR / 2;
y = HAUTEUR / 2;
fruitX = rand() % LARGEUR;
fruitY = rand() % HAUTEUR;
score = 0;
gameOver = 0;
}
void dessiner() {
int i, j;
for (i = 0; i < LARGEUR + 2; i++)
printf("#");
printf("\n");
for (i = 0; i < HAUTEUR; i++) {
for (j = 0; j < LARGEUR; j++) {
if (j == 0)
printf("#");
if (i == y && j == x)
printf("O");
else if (i == fruitY && j == fruitX)
printf("F");
else {
int affiche = 0;
for (int k = 0; k < longueurQueue; k++) {
if (queueX[k] == j && queueY[k] == i) {
printf("o");
affiche = 1;
}
}
if (!affiche)
printf(" ");
}
if (j == LARGEUR - 1)
printf("#");
}
printf("\n");
}
for (i = 0; i < LARGEUR + 2; i++)
printf("#");
printf("\n");
printf("Score: %d\n", score);
}
void input() {
if (_kbhit()) {
switch (_getch()) {
case 'a':
x--;
break;
case 'd':
x++;
break;
case 'w':
y--;
break;
case 's':
y++;
break;
case 'x':
gameOver = 1;
break;
}
}
}
void logique() {
int i;
int prevX = queueX[0];
int prevY = queueY[0];
int prev2X, prev2Y;
queueX[0] = x;
queueY[0] = y;
for (i = 1; i < longueurQueue; i++) {
prev2X = queueX[i];
prev2Y = queueY[i];
queueX[i] = prevX;
queueY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (_getch()) {
case 'a':
x--;
break;
case 'd':
x++;
break;
case 'w':
y--;
break;
case 's':
y++;
break;
case 'x':
gameOver = 1;
break;
}
if (x > LARGEUR || x < 0 || y > HAUTEUR || y < 0)
gameOver = 1;
for (i = 0; i < longueurQueue; i++)
if (queueX[i] == x && queueY[i] == y)
gameOver = 1;
if (x == fruitX && y == fruitY) {
score += 10;
fruitX = rand() % LARGEUR;
fruitY = rand() % HAUTEUR;
longueurQueue++;
}
}
int main() {
srand(time(NULL));
initialiser();
while (!gameOver) {
dessiner();
input();
logique();
Sleep(50);
}
printf("Game Over!\n");
printf("Votre score est: %d\n", score);
return 0;
}