#include #include #include #include #define TAILLEMAX 2000 enum {BAS, DROITE, HAUT, GAUCHE}; void initialiseSerpent(SDL_Rect positionSerpent[], SDL_Surface *serpent, SDL_Surface *ecran, int taille); void blitteSerpent(SDL_Surface *serpent, SDL_Surface *ecran, SDL_Rect positionSerpent[], int taille, int direction, SDL_Surface *tete[]); void decalePositions(SDL_Rect positionSerpent[], int taille); void avanceSerpent(SDL_Surface *serpent, SDL_Rect positionSerpent[], int direction); void perdu(SDL_Surface *boum, SDL_Surface *ecran, SDL_Rect positionSerpent[]); int cogne(SDL_Rect positionSerpent[], SDL_Surface *ecran, int taille); int surSerpent(SDL_Rect positionSerpent[], int taille); void demarreSdl(SDL_Surface **ecran, char titre[]) { if(SDL_Init(SDL_INIT_VIDEO) == -1) { fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError()); // Ecriture de l'erreur exit(EXIT_FAILURE); // On quitte le programme } SDL_WM_SetIcon(SDL_LoadBMP("ico.bmp"), NULL); *ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF); if(*ecran == NULL) { fprintf(stderr, "Impossible de charger le mode vidéo: %s\n", SDL_GetError()); } SDL_WM_SetCaption(titre, NULL); SDL_EnableKeyRepeat(1, 1); } void fermeSdl(SDL_Surface *serpent, SDL_Surface *boum) { SDL_FreeSurface(serpent); SDL_FreeSurface(boum); SDL_Quit(); } void evenements(SDL_Surface *ecran, SDL_Surface *serpent, SDL_Surface *boum, SDL_Surface *tete[]) { int continuer =1, direction=BAS, taille=5, i=0; SDL_Event evenement; SDL_Rect positionSerpent[TAILLEMAX]; initialiseSerpent(positionSerpent, serpent, ecran, taille); i=100; while (continuer) { SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 2, 100, 40)); blitteSerpent(serpent, ecran, positionSerpent, taille, direction, tete); SDL_Flip(ecran); if(i==100) { decalePositions(positionSerpent, taille); avanceSerpent(serpent, positionSerpent, direction); i=0; } else { i++; } if(SDL_PollEvent(&evenement)) { switch(evenement.type) { case SDL_KEYDOWN: switch(evenement.key.keysym.sym) { case SDLK_RIGHT: if(direction != GAUCHE) direction = DROITE; break; case SDLK_LEFT: if(direction != DROITE) direction = GAUCHE; break; case SDLK_UP: if(direction != BAS) direction = HAUT; break; case SDLK_DOWN: if(direction != HAUT) direction = BAS; break; case SDLK_PLUS: case SDLK_p: if(taille0) taille--; break; case SDLK_ESCAPE: continuer = 0; break; default: break; } break; case SDL_QUIT: continuer =0; break; default: break; } } if(cogne(positionSerpent, ecran, taille)) { perdu(boum, ecran, positionSerpent); continuer =0; } } } void initialise(SDL_Surface *ecran, SDL_Surface **serpent, SDL_Surface **boum, SDL_Surface *tete[]) { int i=0; char nom[10]; SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 2, 100, 40)); *serpent = SDL_LoadBMP("serpent.bmp"); *boum = SDL_LoadBMP("boum.bmp"); for(i=0; i<4; i++) { sprintf(nom, "tete%d.bmp", i); tete[i]= SDL_LoadBMP(nom); } } void initialiseSerpent(SDL_Rect positionSerpent[], SDL_Surface *serpent, SDL_Surface *ecran, int taille) { puts("foo"); int i=0; for(i=0; iw)/2)-((serpent->w)/2); positionSerpent[i].y = ((ecran->h/2)-(serpent->h/2)); } puts("bar"); } void blitteSerpent(SDL_Surface *serpent, SDL_Surface *ecran, SDL_Rect positionSerpent[], int taille, int direction, SDL_Surface *tete[]) { int i; SDL_Rect positionTete; positionTete.x = positionSerpent[0].x; positionTete.y = positionSerpent[0].y; switch(direction) { case BAS: positionTete.x = positionSerpent[0].x+tete[0]->w-serpent->w; SDL_BlitSurface(tete[0], NULL, ecran, &positionTete); break; case DROITE: positionTete.y = positionSerpent[0].y+tete[1]->h-serpent->h; SDL_BlitSurface(tete[1], NULL, ecran, &positionTete); break; case HAUT: positionTete.y = positionSerpent[0].y-tete[2]->h+serpent->h; SDL_BlitSurface(tete[2], NULL, ecran, &positionTete); break; case GAUCHE: positionTete.x = positionSerpent[0].x-tete[3]->w+serpent->w; SDL_BlitSurface(tete[3], NULL, ecran, &positionTete); break; } for(i=1; ih+1; break; case DROITE: positionSerpent[0].x+=serpent->w+1; break; case HAUT: positionSerpent[0].y-=serpent->h+1; break; case GAUCHE: positionSerpent[0].x-=serpent->w+1; break; } } int cogne(SDL_Rect positionSerpent[], SDL_Surface *ecran, int taille) { if(positionSerpent[0].x <0 || positionSerpent[0].x >= ecran->w || positionSerpent[0].y<0 || positionSerpent[0].y >= ecran->h) { return 1; } else if(surSerpent(positionSerpent, taille)) { return 1; } else { return 0; } } int surSerpent(SDL_Rect positionSerpent[], int taille) { int resultat=0, i; for(i=1; iw/2-boum->w/2; position.y=ecran->h/2-boum->h/2; printf("%d, %d:%d", SDL_BlitSurface(boum, NULL, ecran, &position), position.x, position.y); } int main(int argc, char* argv[]) { SDL_Surface *ecran, *serpent, *boum, *tete[4]; char titre[20] = "Snake"; demarreSdl(&ecran, titre); initialise(ecran, &serpent, &boum, tete); evenements(ecran, serpent, boum, tete); fermeSdl(serpent, boum); return 0; }