Esempio n. 1
0
/**
 * \brief      Main pour lancer le jeu
 * \details    Gestion des evenements (clavier) de l'utilisateur pour lancer le jeu
 */
int main(int argc, char *argv[])
{
    score=0;

    // Variables pour ecriture avec TTF
    TTF_Font *police, *police2, *police3 = NULL;
    SDL_Color couleurBlanche = {255, 255, 255};
    SDL_Color couleurScore = {0, 0, 0};
    SDL_Surface *texteP1, *texteP2, *texteP3, *texteScore, *texteScore2 = NULL;

    TTF_Init();

    if(TTF_Init() == -1)
    {
        fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
        exit(EXIT_FAILURE);
    }

    /* Chargement des polices */
    police = TTF_OpenFont("ARCHRISTY.ttf", 65);
    police2 = TTF_OpenFont("ARCHRISTY.ttf", 35);
    police3 = TTF_OpenFont("ARCHRISTY.ttf", 26);
    /* Écriture du texte dans la SDL_Surface texte en mode Blended (optimal) */
    texteP1 = TTF_RenderText_Blended(police2, "HOW PLAY TO SQUIRREL ACORN : ", couleurBlanche);
    texteP2 = TTF_RenderText_Blended(police3, "PRESS 1 : NORMAL MODE", couleurBlanche);
    texteP3 = TTF_RenderText_Blended(police3, "PRESS 2 : NO CONSTRAINTS MODE (without obstacles)", couleurBlanche);
    // Init des positions des cases et du personnage
    initilisationPositions();

    SDL_Init(SDL_INIT_VIDEO);

    ecran = SDL_SetVideoMode(LARGEUR_FENETRE, HAUTEUR_FENETRE, TAILLE_SPRITE, SDL_HWSURFACE); // Ouverture de la fenêtre
    if (ecran == NULL)
    {
        fprintf(stderr, "Impossible de charger le mode video : %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    SDL_WM_SetCaption("2D GAME | SQUIRREL ACORN", NULL);

    // Autres variables pour les positions des ecritures
    SDL_Rect positionT;
    SDL_Rect positionT2;
    SDL_Rect positionT3;
    SDL_Rect positionT4;
    SDL_Rect positionT5;
    SDL_Rect positionT6;
    SDL_Rect positionScore;
    SDL_Rect positionScore2;
    positionT.x = 70;
    positionT.y = 185;
    positionT2.x = 45;
    positionT2.y = 265;
    positionT3.x = 27;
    positionT3.y = 365;
    positionT4.x = 70;
    positionT4.y = 215;
    positionT5.x = 170;
    positionT5.y = 275;
    positionT6.x = 20;
    positionT6.y = 330;
    positionScore.x = 75;
    positionScore.y = 265;
    positionScore2.x = 150;
    positionScore2.y = 345;

    // Affichage du texte
    SDL_BlitSurface(texteP1, NULL, ecran, &positionT4); /* Blit du texte */
    SDL_BlitSurface(texteP2, NULL, ecran, &positionT5); /* Blit du texte */
    SDL_BlitSurface(texteP3, NULL, ecran, &positionT6); /* Blit du texte */

    // Initialisation du tableau avec la position des ressources
    int tabR[4];
    tabR[0]=14;
    tabR[1]=138;
    tabR[2]=242;
    tabR[3]=359;

    SDL_Flip(ecran);

    //Gestion des evèvements ( inputs )
    while (exec)
    {
        SDL_WaitEvent(&event);
        switch(event.type)
        {
            case SDL_QUIT:
                exec=false;
                break;
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym)
                {
                case SDLK_UP:
                    versHaut();
                    break;
                case SDLK_DOWN:
                    versBas();
                    break;
                case SDLK_RIGHT:
                    versDroite();
                    break;
                case SDLK_LEFT:
                    versGauche();
                    break;
                case SDLK_1:
                    // Affichage de la carte ...
                    formationCarte();
                    // ... et des objets.
                    placementObjets(1);
                    SDL_BlitSurface(perso, &positionPerso, ecran, &posPerso);
                    SDL_Flip(ecran);
                    SDL_Delay(2000);
                    // Lancement de l'algo
                    score=dijkstra1(tabR,score);
                    if(score!=0){
                        // Affichage du score
                        char sc[13];
                        char sco[3];
                        strcat(sc,"You win ! Score : ");
                        sprintf(sco, "%d", score);
                        strcat(sc,sco);
                        /* Écriture du texte dans la SDL_Surface texte en mode Solid (rapide) */
                        texteScore = TTF_RenderText_Solid(police, sc, couleurScore);
                        SDL_BlitSurface(texteScore, NULL, ecran, &positionScore); /* Blit du texte */;
                        texteScore2 = TTF_RenderText_Solid(police2, "(PRESS ESCAPE TO QUIT)", couleurScore);
                        SDL_BlitSurface(texteScore2, NULL, ecran, &positionScore2); /* Blit du texte */;
                        SDL_Flip(ecran);}
                    break;
                case SDLK_KP1:
                    // Affichage de la carte ...
                    formationCarte();
                    // ... et des objets.
                    placementObjets(1);
                    SDL_BlitSurface(perso, &positionPerso, ecran, &posPerso);
                    SDL_Flip(ecran);
                    SDL_Delay(2000);
                    // Lancement de l'algo
                    score=dijkstra1(tabR,score);
                    if(score!=0){
                        // Affichage du score
                        char sc[13];
                        char sco[3];
                        strcat(sc,"You win ! Score : ");
                        sprintf(sco, "%d", score);
                        strcat(sc,sco);
                        /* Écriture du texte dans la SDL_Surface texte en mode Solid (rapide) */
                        texteScore = TTF_RenderText_Solid(police, sc, couleurScore);
                        SDL_BlitSurface(texteScore, NULL, ecran, &positionScore); /* Blit du texte */;
                        texteScore2 = TTF_RenderText_Solid(police2, "(PRESS ESCAPE TO QUIT)", couleurScore);
                        SDL_BlitSurface(texteScore2, NULL, ecran, &positionScore2); /* Blit du texte */;
                        SDL_Flip(ecran);}
                    break;
                case SDLK_2:
                    // Affichage de la carte ...
                    formationCarte();
                    // ... et des objets.
                    placementObjets(1);
                    SDL_BlitSurface(perso, &positionPerso, ecran, &posPerso);
                    SDL_Flip(ecran);
                    SDL_Delay(2000);
                    // Lancement de l'algo
                    score=dijkstra2(tabR,score);
                    if(score!=0){
                        // Affichage du score
                        char sc[13];
                        char sco[3];
                        strcat(sc,"You win ! Score : ");
                        sprintf(sco, "%d", score);
                        strcat(sc,sco);
                        /* Écriture du texte dans la SDL_Surface texte en mode Solid (rapide) */
                        texteScore = TTF_RenderText_Solid(police, sc, couleurScore);
                        SDL_BlitSurface(texteScore, NULL, ecran, &positionScore); /* Blit du texte */;
                        texteScore2 = TTF_RenderText_Solid(police2, "(PRESS ESCAPE TO QUIT)", couleurScore);
                        SDL_BlitSurface(texteScore2, NULL, ecran, &positionScore2); /* Blit du texte */;
                        SDL_Flip(ecran);}
                    break;
                case SDLK_KP2:
                    // Affichage de la carte ...
                    formationCarte();
                    // ... et des objets.
                    placementObjets(1);
                    SDL_BlitSurface(perso, &positionPerso, ecran, &posPerso);
                    SDL_Flip(ecran);
                    SDL_Delay(2000);
                    // Lancement de l'algo
                    score=dijkstra2(tabR,score);
                    if(score!=0){
                        // Affichage du score
                        char sc[13];
                        char sco[3];
                        strcat(sc,"You win ! Score : ");
                        sprintf(sco, "%d", score);
                        strcat(sc,sco);
                        /* Écriture du texte dans la SDL_Surface texte en mode Solid (rapide) */
                        texteScore = TTF_RenderText_Solid(police, sc, couleurScore);
                        SDL_BlitSurface(texteScore, NULL, ecran, &positionScore); /* Blit du texte */;
                        texteScore2 = TTF_RenderText_Solid(police2, "(PRESS ESCAPE TO QUIT)", couleurScore);
                        SDL_BlitSurface(texteScore2, NULL, ecran, &positionScore2); /* Blit du texte */;
                        SDL_Flip(ecran);}
                    break;
                    case SDLK_ESCAPE:
                        // Pour quitter le jeu
                        exit(0);
                    break;
            }
            break;
        }
    }

    // Liberation de la surface
    SDL_FreeSurface(sprites);

    TTF_CloseFont(police);
    TTF_Quit();

    SDL_Quit();


    return EXIT_SUCCESS; // Fermeture du programme
}
Esempio n. 2
0
void traverse(Location* currentLocation, int* orientation, Location mazeCoords[], int dijkstraPath[]) {
	int i, j;
	
	int visited[17] = {0};
	visited[0] = 1;
	
	int seen[17] = {0};
	seen[0] = 1;
	
	int stack[1000] = {0};
	int stackPointer = 0;
	
	// Initialise mazeGraph to all zeroes
	int mazeGraph[17][17];
	for (i = 0; i < 17; i++) {
		for (j = 0; j < 17; j++) {
			mazeGraph[i][j] = 0;
		}
	}
	
	int currentNode = 0;
	
	int adjacentNodes[3];
	int pathToNext[17] = {0};
	
	stackPointer++;
	stack[stackPointer] = 1;
	
	mazeGraph[0][1] = 1;
	mazeGraph[1][0] = 1;
	
	seen[1] = 1;
	
	while (stackPointer >= 0) {
		// Reset the path to next node array
		for (i = 0; i < 17; i++) {
			pathToNext[i] = 0;
		}
		
		printf("\n");
		printf("Stack: ");
		for (i = 0; i <= stackPointer; i++) {
			printf(" %d", stack[i]);
		}
		printf("\n");
		
		dijkstra2(currentNode, pathToNext, mazeGraph, stack[stackPointer]);
		goThroughPoints(pathToNext, mazeCoords, currentLocation, orientation, &currentNode, stack[stackPointer]);
		
		visited[stack[stackPointer]] = 1;
		stackPointer--;
		findAdjacentNodes(currentNode, *orientation, adjacentNodes);
		
		for (i = 0; i < 3; i++) {
			if (adjacentNodes[i] >= 0) {
				mazeGraph[currentNode][adjacentNodes[i]] = 1;
				mazeGraph[adjacentNodes[i]][currentNode] = 1;
			}
		}
		
		for (i = 2; i >= 0; i--) {
			if (adjacentNodes[i] >= 0 && visited[adjacentNodes[i]] == 0 && seen[adjacentNodes[i]] == 0) {
				seen[adjacentNodes[i]] = 1;
				stackPointer++;
				stack[stackPointer] = adjacentNodes[i];
			}
		}
	}
	
	dijkstra2(0, dijkstraPath, mazeGraph, 16);
}