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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
| #include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <FMOD/fmod.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include "constantes.h"
#include "jeu.h"
void jouer(SDL_Surface* ecran)
{
SDL_Surface *mario[4] = {NULL}; // 4 surfaces pour chacune des directions de mario
SDL_Surface *mur = NULL, *caisse = NULL, *caisseOK = NULL, *objectif = NULL, *marioActuel = NULL;
SDL_Rect position, positionJoueur;
TTF_Font *police = NULL;
SDL_Surface *texte = NULL;
SDL_Event event;
SDL_Color couleurNoire = {0, 0, 0}, couleurBlanche = {255, 255, 255};
int tempsActuel = 0, tempsPrecedent = 0, compteur = 0;
char temps[20] = ""; /* Tableau de char suffisamment grand */
int continuer = 1, objectifsRestants = 0, i = 0, j = 0;
int carte[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR] = {0};
FSOUND_STREAM *mus_pas = NULL;
mus_pas = FSOUND_Stream_Open("422.mp3", FSOUND_LOOP_NORMAL, 0, 0);
FSOUND_STREAM *oh = NULL;
oh = FSOUND_Stream_Open("oh.mp3", FSOUND_LOOP_NORMAL, 0, 0);
// Chargement des sprites (décors, personnage...)
mur = IMG_Load("mur.jpg");
police = TTF_OpenFont("angelina.ttf", 65);
caisse = IMG_Load("caisse.jpg");
caisseOK = IMG_Load("caisse_ok.jpg");
objectif = IMG_Load("objectif.png");
mario[BAS] = IMG_Load("mario_bas.gif");
mario[GAUCHE] = IMG_Load("mario_gauche.gif");
mario[HAUT] = IMG_Load("mario_haut.gif");
mario[DROITE] = IMG_Load("mario_droite.gif");
marioActuel = mario[BAS];// Mario sera dirigé vers le bas au départ
// Chargement du niveau
if (!chargerNiveau(carte))
exit(EXIT_FAILURE); // On arrête le jeu si on n'a pas pu charger le niveau
// Recherche de la position de Mario au départ
for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
{
for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
{
if (carte[i][j] == MARIO) // Si Mario se trouve à cette position sur la carte
{
positionJoueur.x = i;
positionJoueur.y = j;
carte[i][j] = VIDE;
}
}
}
tempsActuel = SDL_GetTicks();
sprintf(temps, "Temps : %d", compteur);
texte = TTF_RenderText_Shaded(police, temps, couleurNoire, couleurBlanche);
// Activation de la répétition des touches
SDL_EnableKeyRepeat(100, 100);
tempsActuel = SDL_GetTicks();
if (tempsActuel - tempsPrecedent >= 100) //Si 100ms au moins se sont écoulées
{
compteur += 100; // On rajoute 100ms au compteur
sprintf(temps, "Temps : %d", compteur); // On écrit dans la chaîne "temps" le nouveau temps
SDL_FreeSurface(texte); // On supprime la surface précédente de la mémoire avant d'en charger une nouvelle (IMPORTANT)
texte = TTF_RenderText_Shaded(police, temps, couleurNoire, couleurBlanche); // On écrit la chaine temps dans la SDL_Surface
tempsPrecedent = tempsActuel; // On met à jour le tempsPrecedent
}
position.x = 180;
position.y = 210;
SDL_BlitSurface(texte, NULL, ecran, &position); /* Blit du texte contenant le temps */
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
FSOUND_Stream_SetLoopCount(oh, 0);
FSOUND_Stream_Play(FSOUND_FREE, oh);
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
continuer = 0;
break;
case SDLK_UP:
marioActuel = mario[HAUT];
deplacerJoueur(carte, &positionJoueur, HAUT);
deplacerJoueur:
FSOUND_Stream_SetLoopCount(mus_pas, 1);
FSOUND_Stream_Play(FSOUND_FREE, mus_pas);
break;
case SDLK_DOWN:
marioActuel = mario[BAS];
deplacerJoueur(carte, &positionJoueur, BAS);
FSOUND_Stream_SetLoopCount(mus_pas, 1);
FSOUND_Stream_Play(FSOUND_FREE, mus_pas);
break;
case SDLK_RIGHT:
marioActuel = mario[DROITE];
deplacerJoueur(carte, &positionJoueur, DROITE);
FSOUND_Stream_SetLoopCount(mus_pas, 1);
FSOUND_Stream_Play(FSOUND_FREE, mus_pas);
break;
case SDLK_LEFT:
marioActuel = mario[GAUCHE];
deplacerJoueur(carte, &positionJoueur, GAUCHE);
FSOUND_Stream_SetLoopCount(mus_pas, 1);
FSOUND_Stream_Play(FSOUND_FREE, mus_pas);
break;
}
break;
}
// Effacement de l'écran
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
// Placement des objets à l'écran
objectifsRestants = 0;
for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
{
for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
{
position.x = i * TAILLE_BLOC;
position.y = j * TAILLE_BLOC;
switch(carte[i][j])
{
case MUR:
SDL_BlitSurface(mur, NULL, ecran, &position);
break;
case CAISSE:
SDL_BlitSurface(caisse, NULL, ecran, &position);
break;
case CAISSE_OK:
SDL_BlitSurface(caisseOK, NULL, ecran, &position);
break;
case OBJECTIF:
SDL_BlitSurface(objectif, NULL, ecran, &position);
objectifsRestants = 1;
break;
}
}
}
// Si on n'a trouvé aucun objectif sur la carte, c'est qu'on a gagné
if (!objectifsRestants)
continuer = 0;
// On place le joueur à la bonne position
position.x = positionJoueur.x * TAILLE_BLOC;
position.y = positionJoueur.y * TAILLE_BLOC;
SDL_BlitSurface(marioActuel, NULL, ecran, &position);
SDL_Flip(ecran);
}
// Désactivation de la répétition des touches (remise à 0)
SDL_EnableKeyRepeat(0, 0);
// Libération des surfaces chargées
SDL_FreeSurface(mur);
SDL_FreeSurface(caisse);
SDL_FreeSurface(caisseOK);
SDL_FreeSurface(objectif);
for (i = 0 ; i < 4 ; i++)
{
SDL_FreeSurface(mario[i]);
}
TTF_CloseFont(police);
}
void deplacerJoueur(int carte[][NB_BLOCS_HAUTEUR], SDL_Rect *pos, int direction)
{
switch(direction)
{
case HAUT:
if (pos->y - 1 < 0) // Si le joueur dépasse l'écran, on arrête
break;
if (carte[pos->x][pos->y - 1] == MUR) // S'il y a un mur, on arrête
break;
// Si on veut pousser une caisse, il faut vérifier qu'il n'y a pas de mur derrière (ou une autre caisse, ou la limite du monde)
if ((carte[pos->x][pos->y - 1] == CAISSE || carte[pos->x][pos->y - 1] == CAISSE_OK) &&
(pos->y - 2 < 0 || carte[pos->x][pos->y - 2] == MUR ||
carte[pos->x][pos->y - 2] == CAISSE || carte[pos->x][pos->y - 2] == CAISSE_OK))
break;
// Si on arrive là, c'est qu'on peut déplacer le joueur !
// On vérifie d'abord s'il y a une caisse à déplacer
deplacerCaisse(&carte[pos->x][pos->y - 1], &carte[pos->x][pos->y - 2]);
pos->y--; // On peut enfin faire monter le joueur
break;
case BAS:
if (pos->y + 1 >= NB_BLOCS_HAUTEUR)
break;
if (carte[pos->x][pos->y + 1] == MUR)
break;
if ((carte[pos->x][pos->y + 1] == CAISSE || carte[pos->x][pos->y + 1] == CAISSE_OK) &&
(pos->y + 2 >= NB_BLOCS_HAUTEUR || carte[pos->x][pos->y + 2] == MUR ||
carte[pos->x][pos->y + 2] == CAISSE || carte[pos->x][pos->y + 2] == CAISSE_OK))
break;
deplacerCaisse(&carte[pos->x][pos->y + 1], &carte[pos->x][pos->y + 2]);
pos->y++;
break;
case GAUCHE:
if (pos->x - 1 < 0)
break;
if (carte[pos->x - 1][pos->y] == MUR)
break;
if ((carte[pos->x - 1][pos->y] == CAISSE || carte[pos->x - 1][pos->y] == CAISSE_OK) &&
(pos->x - 2 < 0 || carte[pos->x - 2][pos->y] == MUR ||
carte[pos->x - 2][pos->y] == CAISSE || carte[pos->x - 2][pos->y] == CAISSE_OK))
break;
deplacerCaisse(&carte[pos->x - 1][pos->y], &carte[pos->x - 2][pos->y]);
pos->x--;
break;
case DROITE:
if (pos->x + 1 >= NB_BLOCS_LARGEUR)
break;
if (carte[pos->x + 1][pos->y] == MUR)
break;
if ((carte[pos->x + 1][pos->y] == CAISSE || carte[pos->x + 1][pos->y] == CAISSE_OK) &&
(pos->x + 2 >= NB_BLOCS_LARGEUR || carte[pos->x + 2][pos->y] == MUR ||
carte[pos->x + 2][pos->y] == CAISSE || carte[pos->x + 2][pos->y] == CAISSE_OK))
break;
deplacerCaisse(&carte[pos->x + 1][pos->y], &carte[pos->x + 2][pos->y]);
pos->x++;
break;
}
}
void deplacerCaisse(int *premiereCase, int *secondeCase)
{
if (*premiereCase == CAISSE || *premiereCase == CAISSE_OK)
{
if (*secondeCase == OBJECTIF)
*secondeCase = CAISSE_OK;
else
*secondeCase = CAISSE;
if (*premiereCase == CAISSE_OK)
*premiereCase = OBJECTIF;
else
*premiereCase = VIDE;
}
} |
Partager