| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- //
- // game.c
- // Mario Sokoban
- //
- // Created by Benoit Sida on 2014-02-16.
- // Copyright (c) 2014 Benoit Sida. All rights reserved.
- //
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <SDL2/SDL.h>
- #include <SDL2_image/SDL_image.h>
- #include "constantes.h"
- #include "level.h"
-
- void mooveMario(int map[][NB_BLOCS_HAUTEUR], SDL_Rect*, int);
- void mooveBox(int*, int*);
-
- void game(int diff, SDL_Window *ecran, SDL_Surface *screen)
- {
- SDL_Surface *mario[4] = {NULL};
- SDL_Surface *wall = NULL, *marioActuel = NULL, *box = NULL, *target = NULL, *boxOK = NULL, *win = NULL, *commandes = NULL;
- SDL_Rect position, posMario;
- SDL_Event event;
- int running = 1, target_r = 0, i = 0, j = 0, returnMenu = 0, continuer = 0, niveau = 0, nbLevel = 0;
- int map[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR] = {0};
- wall = IMG_Load("sprites/mur.jpg");
- box = IMG_Load("sprites/caisse.jpg");
- boxOK = IMG_Load("sprites/caisse_ok.jpg");
- target = IMG_Load("sprites/objectif.png");
- win = IMG_Load("sprites/win.png");
- commandes = IMG_Load("sprites/commandes.png");
- mario[HAUT] = IMG_Load("sprites/mario_haut.gif");
- mario[BAS] = IMG_Load("sprites/mario_bas.gif");
- mario[GAUCHE] = IMG_Load("sprites/mario_gauche.gif");
- mario[DROITE] = IMG_Load("sprites/mario_droite.gif");
- marioActuel = mario[BAS];
- nbLevel =loadLevel(niveau, diff, map);
- if (nbLevel == 0) {
- exit(EXIT_FAILURE);
- } // Charge le niveau
- for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
- {
- for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
- {
- if (map[i][j] == MARIO)
- {
- posMario.x = i;
- posMario.y = j;
- map[i][j] = VIDE;
- }
- }
- } // Position du Joueur au départ
- SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 139, 111, 71));
- 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(map[i][j])
- {
- case WALL:
- SDL_BlitSurface(wall, NULL, screen, &position);
- break;
- case BOX:
- SDL_BlitSurface(box, NULL, screen, &position);
- break;
- case BOX_OK:
- SDL_BlitSurface(boxOK, NULL, screen, &position);
- break;
- case TARGET:
- SDL_BlitSurface(target, NULL, screen, &position);
- target_r = 1;
- break;
- }
- }
- } // Dessine la map
- position.x = 0;
- position.y = 0;
- SDL_BlitSurface(commandes, NULL, screen, &position);
- position.x = posMario.x * TAILLE_BLOC;
- position.y = posMario.y * TAILLE_BLOC;
- SDL_BlitSurface(marioActuel, NULL, screen, &position); // Place le joueur
- SDL_UpdateWindowSurface(ecran);
- while (!continuer) {
- SDL_WaitEvent(&event);
- switch(event.type)
- {
- case SDL_QUIT:
- exit(EXIT_SUCCESS);
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- running = 0;
- returnMenu = 1;
- break;
- case SDLK_RETURN:
- continuer = 1;
- break;
- case SDLK_SPACE:
- continuer = 1;
- break;
- }
- break;
- } // Acton du joueur
- }
- while (running) {
- if (!loadLevel(niveau,diff, map)) {
- exit(EXIT_FAILURE);
- } // Charge le niveau
- for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
- {
- for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
- {
- if (map[i][j] == MARIO)
- {
- posMario.x = i;
- posMario.y = j;
- map[i][j] = VIDE;
- }
- }
- } // Position du Joueur au départ
- continuer = 0;
- while (!continuer) {
- SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 139, 111, 71));
- target_r = 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(map[i][j])
- {
- case WALL:
- SDL_BlitSurface(wall, NULL, screen, &position);
- break;
- case BOX:
- SDL_BlitSurface(box, NULL, screen, &position);
- break;
- case BOX_OK:
- SDL_BlitSurface(boxOK, NULL, screen, &position);
- break;
- case TARGET:
- SDL_BlitSurface(target, NULL, screen, &position);
- target_r = 1;
- break;
- }
- }
- } // Dessine la map
- if (!target_r) {
- continuer = 1;
- niveau++;
- }// change de niveau
- if (niveau == nbLevel + 1) {
- running = 0;// Test de victoire
- }
- position.x = posMario.x * TAILLE_BLOC;
- position.y = posMario.y * TAILLE_BLOC;
- SDL_BlitSurface(marioActuel, NULL, screen, &position); // Place le joueur
- SDL_UpdateWindowSurface(ecran);
- SDL_WaitEvent(&event);
- switch(event.type)
- {
- case SDL_QUIT:
- exit(EXIT_SUCCESS);
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- continuer = 1;
- running = 0;
- returnMenu = 1;
- break;
- case SDLK_UP:
- marioActuel = mario[HAUT];
- mooveMario(map, &posMario, HAUT);
- break;
- case SDLK_DOWN:
- marioActuel = mario[BAS];
- mooveMario(map, &posMario, BAS);
- break;
- case SDLK_RIGHT:
- marioActuel = mario[DROITE];
- mooveMario(map, &posMario, DROITE);
- break;
- case SDLK_LEFT:
- marioActuel = mario[GAUCHE];
- mooveMario(map, &posMario, GAUCHE);
- break;
- }
- break;
- } // Acton du joueur
- }
- } // Boucle principale
- while (!returnMenu) {
- position.x = 0;
- position.y = 0;
- SDL_BlitSurface(win, NULL, screen, &position);
- SDL_UpdateWindowSurface(ecran);
- SDL_WaitEvent(&event);
- switch(event.type)
- {
- case SDL_QUIT:
- exit(EXIT_SUCCESS);
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- returnMenu = 1;
- break;
- case SDLK_RETURN:
- returnMenu = 1;
- break;
- }
- break;
- } // Acton du joueur
- }
- SDL_FreeSurface(wall);
- SDL_FreeSurface(box);
- SDL_FreeSurface(boxOK);
- SDL_FreeSurface(target);
- for (i = 0 ; i < 4 ; i++)
- SDL_FreeSurface(mario[i]); // Libère la mémoire
- }
-
- void mooveMario(int map[][NB_BLOCS_HAUTEUR], SDL_Rect *pos, int direction)
- {
- switch (direction) {
- case HAUT:
- if (pos->y - 1 < 0)
- break;
- if (map[pos->x][pos->y - 1] == WALL)
- break;
- if ((map[pos->x][pos->y - 1] == BOX || map[pos->x][pos->y - 1] == BOX_OK) && (pos->y - 2 < 0 || map[pos->x][pos->y - 2] == WALL || map[pos->x][pos->y - 2] == BOX || map[pos->x][pos->y - 2] == BOX_OK))
- break;
- mooveBox(&map[pos->x][pos->y - 1], &map[pos->x][pos->y - 2]);
- // 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
- pos->y--; // On peut enfin faire monter le joueur (oufff !)
- break;
- case BAS:
- if (pos->y + 1 > 12)
- break;
- if (map[pos->x][pos->y + 1] == WALL)
- break;
- if ((map[pos->x][pos->y + 1] == BOX || map[pos->x][pos->y + 1] == BOX_OK) && (pos->y + 2 > 12 || map[pos->x][pos->y + 2] == WALL || map[pos->x][pos->y + 2] == BOX || map[pos->x][pos->y + 2] == BOX_OK))
- break;
- mooveBox(&map[pos->x][pos->y + 1], &map[pos->x][pos->y + 2]);
- pos->y++;
- break;
- case GAUCHE:
- if (pos->x - 1 < 0)
- break;
- if (map[pos->x - 1][pos->y] == WALL)
- break;
- if ((map[pos->x - 1][pos->y] == BOX || map[pos->x - 1][pos->y] == BOX_OK) && (pos->x - 2 < 0 || map[pos->x - 2][pos->y] == WALL || map[pos->x - 2][pos->y] == BOX || map[pos->x - 2][pos->y] == BOX_OK))
- break;
- mooveBox(&map[pos->x - 1][pos->y], &map[pos->x - 2][pos->y]);
- pos->x--;
- break;
- case DROITE:
- if (pos->x + 1 > 12)
- break;
- if (map[pos->x + 1][pos->y] == WALL)
- break;
- if ((map[pos->x + 1][pos->y] == BOX || map[pos->x + 1][pos->y] == BOX_OK) && (pos->x + 2 > 12 || map[pos->x + 2][pos->y] == WALL || map[pos->x + 2][pos->y] == BOX || map[pos->x + 2][pos->y] == BOX_OK))
- break;
- mooveBox(&map[pos->x + 1][pos->y], &map[pos->x + 2][pos->y]);
- pos->x++;
- break;
- }
- }
-
- void mooveBox(int *premiereCase, int *secondeCase)
- {
- if (*premiereCase == BOX || *premiereCase == BOX_OK)
- {
- if (*secondeCase == TARGET)
- *secondeCase = BOX_OK;
- else
- *secondeCase = BOX;
-
- if (*premiereCase == BOX_OK)
- *premiereCase = TARGET;
- else
- *premiereCase = VIDE;
- }
- }
|