#ifndef DEF_AVENTURE
#define DEF_AVENURE

#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <FMOD/fmod.h>
#include <SDL/SDL_ttf.h>
#include <time.h>

#include "constence.h"
#include "jeu.h"

void lvl1(SDL_Surface *ecran);
void lvl2(SDL_Surface *ecran);

void lvl1(SDL_Surface *ecran)
{
    SDL_Surface *mario[4] = {NULL};
    SDL_Surface *marioActuel = NULL, *banniere = NULL, *argent = NULL, *texte = NULL, *argent2 = NULL, *tempsrestant, *gagne = NULL;
    SDL_Surface *monstre0 = NULL, *argentordi = NULL, *bonus = NULL;
    SDL_Rect positionMario, positionbann, positionArgent[150], positiontexte, positionArgent1, postemps, posgagne, posmonstre0, posargentordi;
    SDL_Rect posbonus;
    SDL_Event event;
    FMUSIC_MODULE *musique = NULL;
    FSOUND_SAMPLE *cash = NULL;
    TTF_Font *police = NULL;
    SDL_Color couleurNoire = {0, 0, 0}, couleurBlanche = {255, 255, 255};
    FILE* fichier = NULL;

    TTF_Init();
    FSOUND_Init(44100, 32, 0);

    int continuer1 = 1, i = 0;
    int direction;
    int tempsPrecedent = 0, tempsActuel = 0, tempsPrecedent1 = 0, tempsActuel1 = 0, compteurtemps = 60000;
    int tempsPrecedent2 = 0, tempsActuel2 = 0, tempsPrecedent3 = 0, tempsActuel3 = 0;
    static int cash1 = 0, cashordi = 0;
    static int point = 0, point1 = 0;
    char argent1[50] = "";
    char temps1[50] = "";
    char argentordi1[50] = "";
    bool bonus1 = false;

    positionMario.x = 200;
    positionMario.y = 200;
    positionbann.x = 0;
    positionbann.y = 406;
    postemps.x = 100;
    postemps.y = 0;
    positionArgent1.x = 302;
    positionArgent1.y = 406;
    positionArgent[i].x = (rand() % (620 - 1 + 1)) + 1;
    positionArgent[i].y = (rand() % (350 - 1 + 1)) + 1;
    event.button.x = 100;
    event.button.y = 100;
    posgagne.x = 0;
    posgagne.y = 0;
    posmonstre0.x = 10;
    posmonstre0.y = 10;
    posargentordi.x = 640/2;
    posargentordi.y = 430;

    bonus = IMG_Load("ressources/images/bonus.jpg");
    monstre0 = IMG_Load("ressources/images/monstre0.png");
    gagne = IMG_Load("ressources/images/gagne.jpg");
    argent2 = IMG_Load("ressources/images/argent2.bmp");
    mario[BAS] = IMG_Load("ressources/images/mariobas.gif");
    mario[HAUT] = IMG_Load("ressources/images/mariohaut.gif");
    mario[GAUCHE] = IMG_Load("ressources/images/mariogauche.gif");
    mario[DROITE] = IMG_Load("ressources/images/mariodroite.gif");
    banniere = IMG_Load("ressources/images/bann.jpg");
    argent = IMG_Load("ressources/images/argent.jpg");


    police = TTF_OpenFont("ressources/police/1.ttf", 16);
    cash = FSOUND_Sample_Load(FSOUND_FREE, "ressources/son/cash.wav", 0, 0, 0);
    musique = FMUSIC_LoadSong("ressources/son/musiquefond.mid");
    fichier = fopen("test.txt", "w");






    marioActuel = mario[BAS];
    sprintf(argent1, "Argent : %d", cash1);
    texte = TTF_RenderText_Shaded(police, argent1, couleurNoire, couleurBlanche);
    sprintf(temps1, "Temps restant : %d", compteurtemps);
    tempsrestant = TTF_RenderText_Shaded(police, temps1, couleurNoire, couleurBlanche);
    sprintf(argentordi1, "Argent Ordi : %d", cashordi);
    argentordi = TTF_RenderText_Shaded(police, argentordi1, couleurNoire, couleurBlanche);

    SDL_SetColorKey(mario[BAS], SDL_SRCCOLORKEY, SDL_MapRGB(mario[BAS]->format, 0, 0, 0));
    SDL_SetColorKey(mario[HAUT], SDL_SRCCOLORKEY, SDL_MapRGB(mario[HAUT]->format, 0, 0, 0));
    SDL_SetColorKey(mario[GAUCHE], SDL_SRCCOLORKEY, SDL_MapRGB(mario[GAUCHE]->format, 0, 0, 0));
    SDL_SetColorKey(mario[DROITE], SDL_SRCCOLORKEY, SDL_MapRGB(mario[DROITE]->format, 0, 0, 0));
    SDL_SetColorKey(argent, SDL_SRCCOLORKEY, SDL_MapRGB(argent->format, 255, 255, 255));
    SDL_SetColorKey(argent2, SDL_SRCCOLORKEY, SDL_MapRGB(argent2->format, 255, 255, 255));
    SDL_SetColorKey(texte, SDL_SRCCOLORKEY, SDL_MapRGB(texte->format, 255, 255, 255));
    SDL_SetColorKey(monstre0, SDL_SRCCOLORKEY, SDL_MapRGB(monstre0->format, 0, 0, 0));
    SDL_SetColorKey(argentordi, SDL_SRCCOLORKEY, SDL_MapRGB(argentordi->format, 255, 255, 255));

    FMUSIC_SetLooping(musique, 1);
    FMUSIC_PlaySong(musique);
    srand(time(NULL));
    int a = 1, b = 0;;
    SDL_EnableKeyRepeat(1, 2);
    int continuer2 = 1;
    while (continuer1)
        {
            SDL_SetColorKey(argent, SDL_SRCCOLORKEY, SDL_MapRGB(argent->format, 255, 255, 255));
            SDL_SetColorKey(argent2, SDL_SRCCOLORKEY, SDL_MapRGB(argent2->format, 255, 255, 255));

            posmonstre0.y += a;
            posmonstre0.x += b;


            if(positionArgent[i].y > posmonstre0.y)
            {
                a = 1;
            }
            if(positionArgent[i].y < posmonstre0.y)
            {
                a = -1;
            }
            if(positionArgent[i].y == posmonstre0.y && positionArgent[i].x > posmonstre0.x)
            {
                a = 0;
                b = 1;
            }
            if(positionArgent[i].y == posmonstre0.y && positionArgent[i].x < posmonstre0.x)
            {
                a = 0;
                b = -1;
            }
            if(positionArgent[i].x == posmonstre0.x)
            {
                if(positionArgent[i].y == posmonstre0.y)
                {
                    b = 0;
                    a = 0;
                }
            }



            if(posmonstre0.y == 350)
            {
                a = -1;
            }
            if(posmonstre0.y == 0)
            {
                a = 1;
            }
            if(posmonstre0.x == 600 && positionArgent[i].x < posmonstre0.x)
            {
                a = 1;
            }
            if(posmonstre0.x == 600 && positionArgent[i].x > posmonstre0.x)
            {
                a = -1;
            }
            if(posmonstre0.y == 0)
            {
                a = 1;
            }
            if(posmonstre0.y == positionArgent[i].y && posmonstre0.x == positionArgent[i].x)
            {

                FSOUND_PlaySound(FSOUND_FREE, cash);

                positionArgent[i].x = (rand() % (620 - 1 + 1)) + 1;
                positionArgent[i].y = (rand() % (350 - 1 + 1)) + 1;
                SDL_BlitSurface(argent, NULL, ecran, &positionArgent[i]);
                cashordi++;
                SDL_BlitSurface(texte, NULL, ecran, &positiontexte);


            }

            point += cash1;
            point1 += cashordi;

            SDL_Delay(5);

        if(compteurtemps == 0)
        {

            if(point > point1)
            {
                point+= cash1;
                point+= cashordi;
            }
            if(point1 == point)
            {
                point+= cash1;
                point+= cashordi;
            }
            else
            {
                point+= cash1;
                point+= cashordi;
            }
            cash1 = 0;
            cashordi = 0;

            SDL_BlitSurface(gagne, NULL, ecran, &posgagne);
            SDL_SetColorKey(gagne, SDL_SRCCOLORKEY, SDL_MapRGB(gagne->format, 255, 255, 255));
            break;
        }
        sprintf(argentordi1, "Argent Ordi : %d", cashordi);
        argentordi = TTF_RenderText_Shaded(police, argentordi1, couleurNoire, couleurBlanche);
        sprintf(argent1, "Argent : %d", cash1);
        texte = TTF_RenderText_Shaded(police, argent1, couleurNoire, couleurBlanche);
        sprintf(temps1, "Temps restant : %d", compteurtemps);
        tempsrestant = TTF_RenderText_Shaded(police, temps1, couleurNoire, couleurBlanche);
        SDL_SetColorKey(argentordi, SDL_SRCCOLORKEY, SDL_MapRGB(argentordi->format, 255, 255, 255));
        SDL_SetColorKey(texte, SDL_SRCCOLORKEY, SDL_MapRGB(texte->format, 255, 255, 255));
        SDL_SetColorKey(tempsrestant, SDL_SRCCOLORKEY, SDL_MapRGB(tempsrestant->format, 255, 255, 255));
        tempsActuel2 = SDL_GetTicks();
        if (tempsActuel2 - tempsPrecedent2 > 10)
        {
            compteurtemps-=10;
            tempsPrecedent2 = tempsActuel2;
            SDL_BlitSurface(tempsrestant, NULL, ecran, &postemps);


        }

            i = 1;
            if( positionArgent[i].y > positionMario.y && positionArgent[i].y + 1 < positionMario.y + 53)
            {
                if( positionArgent[i].x > positionMario.x && positionArgent[i].x + 1 < positionMario.x + 42)
                {

                    FSOUND_PlaySound(FSOUND_FREE, cash);

                    positionArgent[i].x = (rand() % (620 - 1 + 1)) + 1;
                    positionArgent[i].y = (rand() % (350 - 1 + 1)) + 1;
                    SDL_BlitSurface(argent, NULL, ecran, &positionArgent[i]);
                    cash1++;
                    SDL_BlitSurface(texte, NULL, ecran, &positiontexte);

                }
            }

            if( posbonus.y > positionMario.y && posbonus.y + 1 < positionMario.y + 53)
            {
                if( posbonus.x > positionMario.x && posbonus.x + 1 < positionMario.x + 42)
                {
                    bonus1 == true;
                    posbonus.y = 430;
                    posbonus.x = 100;
                    SDL_BlitSurface(bonus, NULL, ecran, &posbonus);
                }
            }
            if( posmonstre0.y > positionArgent1.y && posmonstre0.y + 1 < positionArgent1.y + 53)
            {
                if( posmonstre0.x > positionArgent1.x && posmonstre0.x + 1 < positionArgent1.x + 42)
                {
                    if(bonus1 == true)
                    {
                    cash1+=2;
                    cashordi-=2;
                    FSOUND_PlaySound(FSOUND_FREE, cash);
                    bonus1 = false;
                    }
                }
            }
            if( posmonstre0.y > positionMario.y && posmonstre0.y + 1 < positionMario.y + 53)
            {
                if( posmonstre0.x > positionMario.x && posmonstre0.x + 1 < positionMario.x + 42)
                {
                    if(bonus1 == true)
                    {
                    cash1+=2;
                    cashordi-=2;
                    FSOUND_PlaySound(FSOUND_FREE, cash);
                    bonus1 = false;
                    }
                }
            }
            if( positionArgent1.y > positionMario.y && positionArgent1.y + 1 < positionMario.y + 53)
            {
                if( positionArgent1.x > positionMario.x && positionArgent1.x + 1 < positionMario.x + 42)
                {

                    FSOUND_PlaySound(FSOUND_FREE, cash);

                    positionArgent1.x = 700;
                    positionArgent1.y = 1;
                    SDL_BlitSurface(argent2, NULL, ecran, &positionArgent1);
                    cash1+=3;
                    SDL_BlitSurface(texte, NULL, ecran, &positiontexte);

                }
            }

            SDL_PollEvent(&event);
            switch(event.type)
            {

                case SDL_QUIT:
                    continuer1 = 0;
                    break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                        case SDLK_ESCAPE:
                        //Quitter

                            break;


                        case SDLK_UP:
                        direction = HAUT;
                        marioActuel = mario[HAUT];
                        switch(direction)
                        {
                            case HAUT:
                            if (positionMario.y - 1 < 0)
                            {
                            break;
                            }
                            if (positionMario.y > 0)
                            {
                            positionMario.y -=1;
                            }
                            SDL_Delay(5);
                            break;
                        }

                        break;


                        case SDLK_DOWN:
                        direction = BAS;
                        marioActuel = mario[BAS];
                        switch(direction)
                        {
                            case BAS:
                            if (positionMario.y + 1 >= 350)
                            {
                            break;
                            }
                            if (positionMario.y <= 350)
                            {
                            positionMario.y+=1;
                            }
                            SDL_Delay(5);
                            break;
                        }

                        break;


                        case SDLK_LEFT:
                        direction = GAUCHE;
                        marioActuel = mario[GAUCHE];
                        switch(direction)
                        {

                            case GAUCHE:
                            if (positionMario.x - 1 < 0)
                            {
                            break;
                            }
                            if (positionMario.x > 0)
                            {
                            positionMario.x-=1;
                            }
                            SDL_Delay(5);
                            break;
                        }

                        break;


                        case SDLK_RIGHT:
                        direction = DROITE;
                        marioActuel = mario[DROITE];
                        switch(direction)
                        {


                            case DROITE:
                            if (positionMario.x + 1 >= 600)
                            {
                            break;
                            }
                            if (positionMario.x <= 600)
                            {
                            positionMario.x+=1;
                            }
                            SDL_Delay(5);
                            break;

                        }

                        break;
                    }
                    break;
            }




            positiontexte.x = 30;
            positiontexte.y = 430;
            SDL_BlitSurface(texte, NULL, ecran, &positiontexte);
            tempsActuel = SDL_GetTicks();
        if (tempsActuel - tempsPrecedent > 5000)
        {

            positionArgent[i].x = (rand() % (620 - 1 + 1)) + 1;
            positionArgent[i].y = (rand() % (350 - 1 + 1)) + 1;
            tempsPrecedent = tempsActuel;
            SDL_BlitSurface(argent, NULL, ecran, &positionArgent[i]);
        }
        tempsActuel1 = SDL_GetTicks();
        if (tempsActuel1 - tempsPrecedent1 > 30000)
        {

            positionArgent1.x = (rand() % (620 - 620 + 620)) + 1;
            positionArgent1.y = (rand() % (350 - 350 + 350)) + 1;
            tempsPrecedent1 = tempsActuel1;
            SDL_BlitSurface(argent2, NULL, ecran, &positionArgent1);
        }
        tempsActuel3 = SDL_GetTicks();
        if (tempsActuel3 - tempsPrecedent3 > 70000)
        {

            posbonus.x = (rand() % (620 - 620 + 620)) + 1;
            posbonus.y = (rand() % (350 - 350 + 350)) + 1;
            tempsPrecedent3 = tempsActuel3;
        }
        SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 200, 200, 255));
        SDL_BlitSurface(bonus, NULL, ecran, &posbonus);
        SDL_BlitSurface(tempsrestant, NULL, ecran, &postemps);
        SDL_BlitSurface(argent, NULL, ecran, &positionArgent[i]);
        SDL_BlitSurface(argent2, NULL, ecran, &positionArgent1);
        SDL_BlitSurface(marioActuel, NULL, ecran, &positionMario);
        SDL_BlitSurface(banniere, NULL, ecran, &positionbann);
        SDL_BlitSurface(texte, NULL, ecran, &positiontexte);
        SDL_BlitSurface(monstre0, NULL, ecran, &posmonstre0);
        SDL_BlitSurface(argentordi, NULL, ecran, &posargentordi);



        SDL_Flip(ecran);
        }


    FMUSIC_FreeSong(musique); // Déchargement de la chanson
    FSOUND_Sample_Free(cash);
    FSOUND_Close();
    TTF_CloseFont(police);
    TTF_Quit();
    SDL_FreeSurface(monstre0);
    SDL_FreeSurface(marioActuel);
    SDL_FreeSurface(banniere);
    SDL_FreeSurface(argent);
    SDL_FreeSurface(texte);
    SDL_FreeSurface(tempsrestant);
    SDL_FreeSurface(argent2);
    SDL_FreeSurface(bonus);
    SDL_FreeSurface(gagne);
}
void lvl2(SDL_Surface *ecran)
{
    SDL_Surface *mario[4] = {NULL};
    SDL_Surface *marioActuel = NULL, *banniere = NULL, *argent = NULL, *texte = NULL, *argent2 = NULL, *tempsrestant, *gagne = NULL;
    SDL_Surface *monstre0 = NULL, *argentordi = NULL, *bonus = NULL;
    SDL_Rect positionMario, positionbann, positionArgent[150], positiontexte, positionArgent1, postemps, posgagne, posmonstre0, posargentordi;
    SDL_Rect posbonus;
    SDL_Event event;
    FMUSIC_MODULE *musique = NULL;
    FSOUND_SAMPLE *cash = NULL;
    TTF_Font *police = NULL;
    SDL_Color couleurNoire = {0, 0, 0}, couleurBlanche = {255, 255, 255};


    TTF_Init();
    FSOUND_Init(44100, 32, 0);

    int continuer1 = 1, i = 0;
    int direction;
    int tempsPrecedent = 0, tempsActuel = 0, tempsPrecedent1 = 0, tempsActuel1 = 0, compteurtemps = 60000;
    int tempsPrecedent2 = 0, tempsActuel2 = 0, cash1 = 0, cashordi = 0, tempsPrecedent3 = 0, tempsActuel3 = 0;
    static int point = 0, point1 = 0;
    char argent1[50] = "";
    char temps1[50] = "";
    char argentordi1[50] = "";
    bool bonus1 = false;
    FILE* fichier = NULL;

    positionMario.x = 200;
    positionMario.y = 200;
    positionbann.x = 0;
    positionbann.y = 406;
    postemps.x = 100;
    postemps.y = 0;
    positionArgent1.x = 302;
    positionArgent1.y = 406;
    positionArgent[i].x = (rand() % (620 - 1 + 1)) + 1;
    positionArgent[i].y = (rand() % (350 - 1 + 1)) + 1;
    event.button.x = 100;
    event.button.y = 100;
    posgagne.x = 0;
    posgagne.y = 0;
    posmonstre0.x = 10;
    posmonstre0.y = 10;
    posargentordi.x = 640/2;
    posargentordi.y = 430;

    bonus = IMG_Load("ressources/images/bonus.jpg");
    gagne = IMG_Load("ressources/images/gagne.jpg");
    monstre0 = IMG_Load("ressources/images/monstre0.png");
    argent2 = IMG_Load("ressources/images/argent2.bmp");
    mario[BAS] = IMG_Load("ressources/images/mariobas.gif");
    mario[HAUT] = IMG_Load("ressources/images/mariohaut.gif");
    mario[GAUCHE] = IMG_Load("ressources/images/mariogauche.gif");
    mario[DROITE] = IMG_Load("ressources/images/mariodroite.gif");
    banniere = IMG_Load("ressources/images/bann.jpg");
    argent = IMG_Load("ressources/images/argent.jpg");


    police = TTF_OpenFont("ressources/police/1.ttf", 16);
    cash = FSOUND_Sample_Load(FSOUND_FREE, "ressources/son/cash.wav", 0, 0, 0);
    musique = FMUSIC_LoadSong("ressources/son/musiquefond2.mid");
    fichier = fopen("test.txt", "w");





    marioActuel = mario[BAS];
    sprintf(argent1, "Argent : %d", cash1);
    texte = TTF_RenderText_Shaded(police, argent1, couleurNoire, couleurBlanche);
    sprintf(temps1, "Temps restant : %d", compteurtemps);
    tempsrestant = TTF_RenderText_Shaded(police, temps1, couleurNoire, couleurBlanche);
    sprintf(argentordi1, "Argent Ordi : %d", cashordi);
    argentordi = TTF_RenderText_Shaded(police, argentordi1, couleurNoire, couleurBlanche);

    SDL_SetColorKey(mario[BAS], SDL_SRCCOLORKEY, SDL_MapRGB(mario[BAS]->format, 0, 0, 0));
    SDL_SetColorKey(mario[HAUT], SDL_SRCCOLORKEY, SDL_MapRGB(mario[HAUT]->format, 0, 0, 0));
    SDL_SetColorKey(mario[GAUCHE], SDL_SRCCOLORKEY, SDL_MapRGB(mario[GAUCHE]->format, 0, 0, 0));
    SDL_SetColorKey(mario[DROITE], SDL_SRCCOLORKEY, SDL_MapRGB(mario[DROITE]->format, 0, 0, 0));
    SDL_SetColorKey(argent, SDL_SRCCOLORKEY, SDL_MapRGB(argent->format, 255, 255, 255));
    SDL_SetColorKey(argent2, SDL_SRCCOLORKEY, SDL_MapRGB(argent2->format, 255, 255, 255));
    SDL_SetColorKey(texte, SDL_SRCCOLORKEY, SDL_MapRGB(texte->format, 255, 255, 255));
    SDL_SetColorKey(monstre0, SDL_SRCCOLORKEY, SDL_MapRGB(monstre0->format, 0, 0, 0));
    SDL_SetColorKey(argentordi, SDL_SRCCOLORKEY, SDL_MapRGB(argentordi->format, 255, 255, 255));

    FMUSIC_SetLooping(musique, 1);
    FMUSIC_PlaySong(musique);
    srand(time(NULL));
    int a = 1, b = 0;;
    SDL_EnableKeyRepeat(1, 2);
    while (continuer1)
        {
            SDL_SetColorKey(argent, SDL_SRCCOLORKEY, SDL_MapRGB(argent->format, 255, 255, 255));
            SDL_SetColorKey(argent2, SDL_SRCCOLORKEY, SDL_MapRGB(argent2->format, 255, 255, 255));
            posmonstre0.y += a;
            posmonstre0.x += b;
            if(positionArgent[i].y > posmonstre0.y)
            {
                a = 1;
            }
            if(positionArgent[i].y < posmonstre0.y)
            {
                a = -1;
            }
            if(positionArgent[i].y == posmonstre0.y && positionArgent[i].x > posmonstre0.x)
            {
                a = 0;
                b = 1;
            }
            if(positionArgent[i].y == posmonstre0.y && positionArgent[i].x < posmonstre0.x)
            {
                a = 0;
                b = -1;
            }
            if(positionArgent[i].x == posmonstre0.x)
            {
                b = 0;
            }
            if(positionArgent[i].y == posmonstre0.y)
            {
                a = 0;
            }
            if(posmonstre0.y == 350)
            {
                a = -1;
            }
            if(posmonstre0.y == 0)
            {
                a = 1;
            }
            if(posmonstre0.x == 600 && positionArgent[i].x < posmonstre0.x)
            {
                a = 1;
            }
            if(posmonstre0.x == 600 && positionArgent[i].x > posmonstre0.x)
            {
                a = -1;
            }
            if(posmonstre0.y == 0)
            {
                a = 1;
            }
            if(posmonstre0.y == positionArgent[i].y && posmonstre0.x == positionArgent[i].x)
            {

                FSOUND_PlaySound(FSOUND_FREE, cash);

                positionArgent[i].x = (rand() % (620 - 1 + 1)) + 1;
                positionArgent[i].y = (rand() % (350 - 1 + 1)) + 1;
                SDL_BlitSurface(argent, NULL, ecran, &positionArgent[i]);
                cashordi++;
                SDL_BlitSurface(texte, NULL, ecran, &positiontexte);


            }

        point += cash1;
            point1 += cashordi;

            SDL_Delay(0);

        if(compteurtemps == 0)
        {

            if(point > point1)
            {
                 if (fichier != NULL)
                            {
                                cash1+=5;
                                fprintf(fichier, "%d", cash1);
                                fclose(fichier);
                            }
                            SDL_BlitSurface(gagne, NULL, ecran, &posgagne);
                            SDL_Delay(3000);
            }
            if(point1 == point)
            {
                 if (fichier != NULL)
                            {

                                fprintf(fichier, "%d", cash1);
                                fclose(fichier);
                            }
                            SDL_BlitSurface(gagne, NULL, ecran, &posgagne);
                            SDL_Delay(3000);
            }
            else
            {
                 if (fichier != NULL)
                    {
                        cash1-=5;
                        cashordi+=5;
                        fprintf(fichier, "%d", cash1);
                        fclose(fichier);
                    }
                    SDL_BlitSurface(gagne, NULL, ecran, &posgagne);
                    SDL_Delay(3000);
            }
            SDL_BlitSurface(gagne, NULL, ecran, &posgagne);
            cash1 = 0;
            cashordi = 0;
            continuer1 = 0;
        }
        sprintf(argentordi1, "Argent Ordi : %d", cashordi);
        argentordi = TTF_RenderText_Shaded(police, argentordi1, couleurNoire, couleurBlanche);
        sprintf(argent1, "Argent : %d", cash1);
        texte = TTF_RenderText_Shaded(police, argent1, couleurNoire, couleurBlanche);
        sprintf(temps1, "Temps restant : %d", compteurtemps);
        tempsrestant = TTF_RenderText_Shaded(police, temps1, couleurNoire, couleurBlanche);
        SDL_SetColorKey(argentordi, SDL_SRCCOLORKEY, SDL_MapRGB(argentordi->format, 255, 255, 255));
        SDL_SetColorKey(texte, SDL_SRCCOLORKEY, SDL_MapRGB(texte->format, 255, 255, 255));
        SDL_SetColorKey(tempsrestant, SDL_SRCCOLORKEY, SDL_MapRGB(tempsrestant->format, 255, 255, 255));
        tempsActuel2 = SDL_GetTicks();
        if (tempsActuel2 - tempsPrecedent2 > 10)
        {
            compteurtemps-=10;
            tempsPrecedent2 = tempsActuel2;
            SDL_BlitSurface(tempsrestant, NULL, ecran, &postemps);


        }

            i = 1;
            if( positionArgent[i].y > positionMario.y && positionArgent[i].y + 1 < positionMario.y + 53)
            {
                if( positionArgent[i].x > positionMario.x && positionArgent[i].x + 1 < positionMario.x + 42)
                {

                    FSOUND_PlaySound(FSOUND_FREE, cash);

                    positionArgent[i].x = (rand() % (620 - 1 + 1)) + 1;
                    positionArgent[i].y = (rand() % (350 - 1 + 1)) + 1;
                    SDL_BlitSurface(argent, NULL, ecran, &positionArgent[i]);
                    cash1++;
                    SDL_BlitSurface(texte, NULL, ecran, &positiontexte);

                }
            }
            if( posbonus.y > positionMario.y && posbonus.y + 1 < positionMario.y + 53)
            {
                if( posbonus.x > positionMario.x && posbonus.x + 1 < positionMario.x + 42)
                {
                    bonus1 = true;
                    posbonus.y = 430;
                    posbonus.x = 100;
                    SDL_BlitSurface(bonus, NULL, ecran, &posbonus);
                }
            }
            if( posmonstre0.y > positionMario.y && posmonstre0.y + 1 < positionMario.y + 53)
            {
                if( posmonstre0.x > positionMario.x && posmonstre0.x + 1 < positionMario.x + 42)
                {
                    if(bonus1 == true)
                    {
                    cash1+=2;
                    cashordi-=2;
                    FSOUND_PlaySound(FSOUND_FREE, cash);
                    bonus1 = false;
                    }
                }
            }
            if( positionArgent1.y > positionMario.y && positionArgent1.y + 1 < positionMario.y + 53)
            {
                if( positionArgent1.x > positionMario.x && positionArgent1.x + 1 < positionMario.x + 42)
                {

                    FSOUND_PlaySound(FSOUND_FREE, cash);

                    positionArgent1.x = 700;
                    positionArgent1.y = 1;
                    SDL_BlitSurface(argent2, NULL, ecran, &positionArgent1);
                    cash1+=3;
                    SDL_BlitSurface(texte, NULL, ecran, &positiontexte);

                }
            }

            SDL_PollEvent(&event);
            switch(event.type)
            {

                case SDL_QUIT:
                    continuer1 = 0;
                    break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                        case SDLK_ESCAPE:
                        //Quitter
                            continuer1 = 0;
                            break;


                        case SDLK_UP:
                        direction = HAUT;
                        marioActuel = mario[HAUT];
                        switch(direction)
                        {
                            case HAUT:
                            if (positionMario.y - 1 < 0)
                            {
                            break;
                            }
                            if (positionMario.y > 0)
                            {
                            positionMario.y -=1;
                            }
                            SDL_Delay(0);
                            break;
                        }

                        break;


                        case SDLK_DOWN:
                        direction = BAS;
                        marioActuel = mario[BAS];
                        switch(direction)
                        {
                            case BAS:
                            if (positionMario.y + 1 >= 350)
                            {
                            break;
                            }
                            if (positionMario.y <= 350)
                            {
                            positionMario.y+=1;
                            }
                            SDL_Delay(0);
                            break;
                        }

                        break;


                        case SDLK_LEFT:
                        direction = GAUCHE;
                        marioActuel = mario[GAUCHE];
                        switch(direction)
                        {

                            case GAUCHE:
                            if (positionMario.x - 1 < 0)
                            {
                            break;
                            }
                            if (positionMario.x > 0)
                            {
                            positionMario.x-=1;
                            }
                            SDL_Delay(0);
                            break;
                        }

                        break;


                        case SDLK_RIGHT:
                        direction = DROITE;
                        marioActuel = mario[DROITE];
                        switch(direction)
                        {


                            case DROITE:
                            if (positionMario.x + 1 >= 600)
                            {
                            break;
                            }
                            if (positionMario.x <= 600)
                            {
                            positionMario.x+=1;
                            }
                            SDL_Delay(0);
                            break;

                        }

                        break;
                    }
                    break;
            }



            positiontexte.x = 30;
            positiontexte.y = 430;
            SDL_BlitSurface(texte, NULL, ecran, &positiontexte);
            tempsActuel = SDL_GetTicks();
        if (tempsActuel - tempsPrecedent > 3500)
        {

            positionArgent[i].x = (rand() % (620 - 1 + 1)) + 1;
            positionArgent[i].y = (rand() % (350 - 1 + 1)) + 1;
            tempsPrecedent = tempsActuel;
            SDL_BlitSurface(argent, NULL, ecran, &positionArgent[i]);
        }
        tempsActuel1 = SDL_GetTicks();
        if (tempsActuel1 - tempsPrecedent1 > 30000)
        {

            positionArgent1.x = (rand() % (620 - 620 + 620)) + 1;
            positionArgent1.y = (rand() % (350 - 350 + 350)) + 1;
            tempsPrecedent1 = tempsActuel1;
            SDL_BlitSurface(argent2, NULL, ecran, &positionArgent1);
        }
        tempsActuel3 = SDL_GetTicks();
        if (tempsActuel3 - tempsPrecedent3 > 70000000)
        {

            posbonus.x = (rand() % (620 - 620 + 620)) + 1;
            posbonus.y = (rand() % (350 - 350 + 350)) + 1;
            tempsPrecedent3 = tempsActuel3;
            SDL_BlitSurface(bonus, NULL, ecran, &posbonus);
        }
        SDL_BlitSurface(gagne, NULL, ecran, &posgagne);
        SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 200, 200, 255));
        SDL_BlitSurface(bonus, NULL, ecran, &posbonus);
        SDL_BlitSurface(tempsrestant, NULL, ecran, &postemps);
        SDL_BlitSurface(argent, NULL, ecran, &positionArgent[i]);
        SDL_BlitSurface(argent2, NULL, ecran, &positionArgent1);
        SDL_BlitSurface(marioActuel, NULL, ecran, &positionMario);
        SDL_BlitSurface(banniere, NULL, ecran, &positionbann);
        SDL_BlitSurface(texte, NULL, ecran, &positiontexte);
        SDL_BlitSurface(monstre0, NULL, ecran, &posmonstre0);
        SDL_BlitSurface(argentordi, NULL, ecran, &posargentordi);



        SDL_Flip(ecran);
        }


    FMUSIC_FreeSong(musique); // Déchargement de la chanson
    FSOUND_Sample_Free(cash);
    FSOUND_Close();
    TTF_CloseFont(police);
    TTF_Quit();
    SDL_FreeSurface(monstre0);
    SDL_FreeSurface(marioActuel);
    SDL_FreeSurface(banniere);
    SDL_FreeSurface(argent);
    SDL_FreeSurface(texte);
    SDL_FreeSurface(tempsrestant);
    SDL_FreeSurface(argent2);
    SDL_FreeSurface(bonus);
    SDL_FreeSurface(gagne);
}
#endif
