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
| #include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL_image.h>
#include "constantes.h"
#include "jeu.h"
void play(SDL_Surface* screen)
{
SDL_Surface *mario[4]={NULL};
SDL_Surface *wall=NULL, *box=NULL, *boxOK=NULL, *target=NULL, *current_mario=NULL;
SDL_Rect position, player_position;
SDL_Event event;
int continu=1, left_target=0, i=0, j=0;
int map[NB_BLOCKS_WIDTH][NB_BLOCKS_HEIGHT]={0};
/*Chargement des sprites*/
wall = IMG_Load("mur.jpg");
box = IMG_Load("caisse.jpg");
boxOK = IMG_Load("caisse_ok.jpg");
target = IMG_Load("objectif.png");
mario[DOWN] = IMG_Load("mario_bas.gif");
mario[LEFT] = IMG_Load("mario_gauche.gif");
mario[UP] = IMG_Load("mario_haut.gif");
mario[RIGHT] = IMG_Load("mario_droite.gif");
current_mario=mario[DOWN];
player_position.x=11;
player_position.y=11;
for(i=0;i<NB_BLOCKS_HEIGHT;i++)
{
for(j=0;j<NB_BLOCKS_WIDTH;j++)
{
if(map[i][j]==MARIO)
{
player_position.x=i;
player_position.y=j;
map[i][j]=BLANK;
}
}
}
SDL_EnableKeyRepeat(100,100);
while(continu)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continu=0;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
continu=0;
break;
case SDLK_UP:
current_mario = mario[UP];
movePlayer(map,&player_position,UP);
break;
case SDLK_DOWN:
current_mario = mario[DOWN];
movePlayer(map,&player_position,DOWN);
break;
case SDLK_LEFT:
current_mario = mario[LEFT];
movePlayer(map,&player_position,LEFT);
break;
case SDLK_RIGHT:
current_mario = mario[RIGHT];
movePlayer(map,&player_position,RIGHT);
break;
}
break;
}
}
/*Effacement de l'écran*/
SDL_FillRect(screen,NULL,SDL_MapRGB(screen->format,255,255,255));
/*Placement des oblets sur l'écran*/
left_target=0;
for(i=0;i<NB_BLOCKS_WIDTH;i++)
{
for(j=0;j<NB_BLOCKS_HEIGHT;j++)
{
position.x= i*BLOCK_SIZE;
position.y= j*BLOCK_SIZE;
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);
break;
}
}
}
if(!left_target)
continu=0;
position.x= player_position.x * BLOCK_SIZE;
position.y= player_position.y * BLOCK_SIZE;
SDL_BlitSurface(current_mario,NULL,screen,&position);
SDL_Flip(screen);
/*Désactivation de la répétition des touches*/
SDL_EnableKeyRepeat(0,0);
/*Libération des surfaces chargées*/
SDL_FreeSurface(wall);
SDL_FreeSurface(box);
SDL_FreeSurface(boxOK);
SDL_FreeSurface(target);
for(i=0;i<4;i++)
SDL_FreeSurface(mario[i]);
}
void movePlayer(int map[][NB_BLOCKS_HEIGHT], SDL_Rect *pos, int direction)
{
switch(direction)
{
case UP:
if(pos->y-1<0)/*Si le joueur dépasse l'écran, on arrête*/
break;
if(map[pos->x][pos->y-1]==WALL)/*S'il y a un mur, on arrête*/
break;
/*On vérifie qu'il n'y a pas un mur, ou la fin du monde ou une autre caisse devant la caisse qu'on veut bouger*/
if(map[pos->x][pos->y-1]==BOX || map[pos->x][pos->y-1]==BOX_OK && map[pos->x][pos->y-2]==BOX || map[pos->x][pos->y-2]==BOX_OK)
break;
/*On déplace une caisse*/
moveBox(&map[pos->x][pos->y-1], &map[pos->x][pos->y-1]);
/*On fait monter le joueur*/
pos->y--;
break;
case DOWN:
if(pos->y+1>NB_BLOCKS_HEIGHT*BLOCK_SIZE)
break;
if(map[pos->x][pos->y+1]==WALL)
break;
/*On vérifie qu'il n'y a pas un mur, ou la fin du monde ou une autre caisse dèrriere la caisse qu'on veut bouger*/
if((map[pos->x][pos->y+1]==BOX || map[pos->x][pos->y+1]==BOX_OK) && (map[pos->x][pos->y+2]==BOX || map[pos->x][pos->y+2]==BOX_OK))
break;
/*On deplace une caisse*/
pos->y++;
break;
case LEFT:
if(pos->x-1<0)
break;
if(map[pos->x-1][pos->y]==WALL)
break;
/*On vérifie qu'il n'y a pas un mur, ou la fin du monde ou une autre caisse dèrriere la caisse qu'on veut bouger*/
if((map[pos->x-1][pos->y]==BOX || map[pos->x-1][pos->y]==BOX_OK) && (map[pos->x-2][pos->y]==BOX || map[pos->x-2][pos->y]==BOX_OK))
break;
/*On deplace une caisse*/
pos->x--;
break;
case RIGHT:
if(pos->x+1>NB_BLOCKS_WIDTH*BLOCK_SIZE)
break;
if(map[pos->x+1][pos->y]==WALL)
break;
/*On vérifie qu'il n'y a pas un mur, ou la fin du monde ou une autre caisse dèrriere la caisse qu'on veut bouger*/
if((map[pos->x+1][pos->y]==BOX || map[pos->x+1][pos->y]==BOX_OK) && (map[pos->x+2][pos->y]==BOX || map[pos->x+2][pos->y]==BOX_OK))
break;
/*On deplace une caisse*/
pos->x++;
break;
}
}
int *first_frame, *second_frame;
void moveBox(int *first_frame, int *second_frame)
{
if(*first_frame==BOX || *first_frame==BOX_OK)
{
if(*second_frame==TARGET)
*second_frame=BOX_OK;
else
*second_frame=BOX;
if (*first_frame==BOX_OK)
*first_frame=TARGET;
else
*first_frame=BLANK;
}
} |