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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| void play(SDL_Surface *screen)
{
SDL_Surface *snake[4] = {NULL};
SDL_Surface *snakeActu = NULL, *mur = NULL, *orange= NULL, *pommeVerte = NULL, *queue = NULL;
SDL_Rect positionJoueur, position;
SDL_Rect positionQueue[NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR] = {0};
SDL_Event event;// on initialise tout
int continuer = 1, i = 0, j = 0, objetRestant = 1, tempsActu = 0, tempsPrecedent = 0, v = 0, taille = 10;
int map[NB_BLOCS_HAUTEUR][NB_BLOCS_LARGEUR] = {0};
snake[BAS] = IMG_Load("Snake_Bas.bmp");
snake[HAUT] = IMG_Load("Snake_Haut.bmp");
snake[DROITE] = IMG_Load("Snake_Droit.bmp");
snake[GAUCHE] = IMG_Load("Snake_Gauche.bmp");
mur = IMG_Load("mur.png");
orange = IMG_Load("Orange.bmp");
pommeVerte = IMG_Load("PommeVerte.bmp");
queue = IMG_Load("Snake_Queue.bmp");//on charge les images
for (i = 0; i<4; i++)
SDL_SetColorKey(snake[i] , SDL_SRCCOLORKEY, SDL_MapRGB(snake[i]-> format, 2, 0, 255));
SDL_SetColorKey(orange, SDL_SRCCOLORKEY, SDL_MapRGB(orange-> format, 2, 0, 255));
SDL_SetColorKey(pommeVerte, SDL_SRCCOLORKEY, SDL_MapRGB(pommeVerte-> format, 2, 0, 255));
SDL_SetColorKey(queue, SDL_SRCCOLORKEY, SDL_MapRGB(queue-> format, 2, 0, 255));// on rend tout transparent
snakeActu = snake[BAS];// on commence par partir vers le bas
if(!loadLevel(map))// on charge la map
return 0;
for (i=0; i<NB_BLOCS_LARGEUR; i++)
{
for(j=0; j<NB_BLOCS_HAUTEUR; j++)
{
if(map[i][j] == TETE)
{
positionJoueur.x = i;
positionJoueur.y = j;
map[i][j] = VIDE;
}
}
}//On recherche la positition dujoueur
SDL_EnableKeyRepeat (100, 100);
while(continuer)
{
tempsActu = SDL_GetTicks();
objetRestant = 0;
SDL_PollEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
continuer = 0;
break;
case SDLK_UP:
snakeActu = snake[HAUT];
break;
case SDLK_DOWN:
snakeActu = snake[BAS];
break;
case SDLK_LEFT:
snakeActu = snake[GAUCHE];
break;
case SDLK_RIGHT:
snakeActu = snake[DROITE];
break;// on place le snake dans une certaine direction
default:
break;
}
}
if(tempsActu - tempsPrecedent > 100 && snakeActu == snake[HAUT])
{
moveSnake(map, &positionJoueur, HAUT);
tempsPrecedent = tempsActu;
}
if(tempsActu - tempsPrecedent > 100 && snakeActu == snake[BAS])
{
moveSnake(map, &positionJoueur, BAS);
tempsPrecedent = tempsActu;
}
if(tempsActu - tempsPrecedent > 100 && snakeActu == snake[GAUCHE])
{
moveSnake(map, &positionJoueur, GAUCHE);
tempsPrecedent = tempsActu;
}
if(tempsActu - tempsPrecedent > 100 && snakeActu == snake[DROITE])// on le fait bouger^^
{
moveSnake(map, &positionJoueur, DROITE);
tempsPrecedent = tempsActu;
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 100, 178, 200));// on efface l'ecran a chaque tour de boucle
for(i=0; i<NB_BLOCS_LARGEUR; i++)
{
for(j=0; j<NB_BLOCS_HAUTEUR; j++)
{
position.x = i * TAILLE_CASE;
position.y = j * TAILLE_CASE;
switch(map[i][j])
{
case MUR:
SDL_BlitSurface(mur, NULL, screen, &position);
break;
case ORANGE:
SDL_BlitSurface(orange, NULL, screen, &position);
objetRestant = 1;// on met objet Restant a 1 si il reste des objets
break;
case POMME_VERTE:
SDL_BlitSurface(pommeVerte, NULL, screen, &position);
break;//On blit tous les objets
}
}
}
position.x = positionJoueur.x * TAILLE_CASE;
position.y = positionJoueur.y * TAILLE_CASE;
for(v = 0; v<NB_BLOCS_HAUTEUR * NB_BLOCS_LARGEUR; v++)
{
if(v == 0)
{
if(snakeActu == snake[HAUT])
{
positionQueue[0].x = (positionJoueur.x) * TAILLE_CASE;
positionQueue[0].y = (positionJoueur.y + 1) * TAILLE_CASE;
}
else if(snakeActu == snake[BAS])
{
positionQueue[0].x = (positionJoueur.x) * TAILLE_CASE;
positionQueue[0].y = (positionJoueur.y - 1) * TAILLE_CASE;
}
else if(snakeActu == snake[GAUCHE])
{
positionQueue[0].x = (positionJoueur.x + 1) * TAILLE_CASE;
positionQueue[0].y = positionJoueur.y * TAILLE_CASE;
}
else if(snakeActu == snake[DROITE])
{
positionQueue[0].x = (positionJoueur.x - 1) * TAILLE_CASE;
positionQueue[0].y = positionJoueur.y * TAILLE_CASE;
}
}
else
{
if(snakeActu == snake[GAUCHE])
{
positionQueue[v].x = positionQueue[v - 1].x + 1 * TAILLE_CASE;
positionQueue[v].y = positionQueue[v - 1].y;
}
if(snakeActu == snake[DROITE])
{
positionQueue[v].x = positionQueue[v - 1].x - 1 * TAILLE_CASE;
positionQueue[v].y = positionQueue[v - 1].y;
}
if(snakeActu == snake[BAS])
{
positionQueue[v].x = positionQueue[v - 1].x;
positionQueue[v].y = positionQueue[v - 1].y - 1 * TAILLE_CASE;
}
if(snakeActu == snake[HAUT])
{
positionQueue[v].x = positionQueue[v - 1].x;
positionQueue[v].y = positionQueue[v - 1].y + 1 * TAILLE_CASE;
}
}
}// On place la queue a ma facon(qui n'est pas terrible^^)
for(v=0; v<taille; v++)
SDL_BlitSurface(queue, NULL, screen, &positionQueue[v]);
SDL_BlitSurface(snakeActu, NULL, screen, &position);
SDL_Flip(screen);// On blit le tout
if(!objetRestant)
continuer = 0;// si il n'y a plus d'objet on arrete et on a gaggner^^
}
SDL_FreeSurface(snakeActu);
SDL_FreeSurface(queue);
SDL_FreeSurface(mur);
SDL_FreeSurface(pommeVerte);
SDL_FreeSurface(orange);
for ( i=0; i<4; i++)
SDL_FreeSurface(snake[i]);
} |
Partager