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
|
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL.h>
#include <time.h>
#include "main.h"
void deplacerROBOT(int *direction,SDL_Rect *position,int taille,int *contacteZone);
int main ( int argc, char** argv )
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *ecran=NULL,*robot=NULL;
FILE *fichier=NULL;
int continu=1;/*boucle */
int direction[4]={2,3,1,0};/*les quatre direction droit gauche bas haut*/
int tab[4]={0};
int taille=4;
int *contacteZone=&tab[0];/*pointeur permet de modifier la valeur de la variable tab */
int i,j=0,longeur=0,largeur=0;
SDL_Event event;
SDL_Rect position;
position.x=0;
position.y=0;
ecran=SDL_SetVideoMode(LARGEUR_FENETRE,LONGEUR_FENETRE,32,SDL_HWSURFACE);
robot=SDL_LoadBMP("robot aspi.bmp");
while (continu)
{
direction[j];/*on envoi la premier valeur du tableau direction qui est 2 "droit"*/
deplacerROBOT(&direction[j],&position,taille,contacteZone);
for (i=0;i<taille;i++)/*boucle pour regarder qu'elle est la position du tableau mi a 1*/
{
if (tab[i]==1)/*condition pour changer de direction */
{
tab[i]=0;/*on remette tout les valeur de tab[i] a zero*/
j++;/*on incrément la position du tableau direction*/
if (j==4)/*condition quand on atteint la limite de la position du tableau direction*/
{
j=0;/*on remet la position du tableau de direction a zero*/
}
}
}
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
continu=0;
break;
}
SDL_FillRect(ecran,NULL,SDL_MapRGB((*ecran).format,255,255,255));
position.x*TAILLE_BLOC;
position.y*TAILLE_BLOC ;
SDL_BlitSurface(robot,NULL,ecran,&position);
SDL_Flip(ecran);
}
SDL_FreeSurface(robot);
}
void deplacerROBOT(int *direction,SDL_Rect *position,int taille,int *contacteZone)
{
int i;
for (i=0;i<taille;i++)
{
switch (*direction)
{
case HAUT:
if ((*position).y<0)
{
contacteZone[i]=1;
break;
}
(*position).y--;
break;
case BAS:
if ((*position).y+50>=LARGEUR_FENETRE)
{
contacteZone[i]=1;
break;
}
(*position).y++;
break;
case DROIT:
if ((*position).x+50>=LONGEUR_FENETRE)
{
contacteZone[i]=1;
break;
}
(*position).x++;
break;
case GAUCHE:
if ((*position).x<0)
{
contacteZone[i]=1;
break;
}
(*position).x--;
break;
}
}
} |
Partager