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 281 282 283
| #include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
SDL_Rect pos,postir,posalien,pospoint[5];
SDL_Surface *ecran=NULL, *carre=NULL, *tir=NULL ,*alien=NULL;
SDL_Surface *point[10]={NULL};
SDL_Event event;
int main (int argc,char **argv)
{
int continuer=1,j=0,i;
int v=0;
for (i=0;i<10;i++)
{
point[i]=(SDL_Surface*) malloc (10 *sizeof(SDL_Surface)); // initialisation des points
}
pos.x=(800/2) -(50/2); // initialisation de la position du viseur carré
pos.y=(600/2) -(50/2);
for( i=0;i<10;i++)
{
pospoint[i].x=j; // initialisation des positions de chaque point
pospoint[i].y=0;
j +=30;
}
int temps_actuel =0,temps_precedent=0;// initialisation du temps pour lançer aleatoirement l'image alien
alien=SDL_LoadBMP("/home/domnik/alien.bmp"); // initialisation et chargement de l'image alien
for (i=0;i<10;i++)
{
point[i]=SDL_CreateRGBSurface(SDL_HWSURFACE,20,20,32,0,0,0,0); // creation des points
}
tir =SDL_CreateRGBSurface(SDL_HWSURFACE,10,10,32,0,0,0,0); // creation du tir (point rouge)
carre=SDL_CreateRGBSurface(SDL_HWSURFACE,50,50,32,0,0,0,0); // creation du viseur (point blanc)
SDL_Init(SDL_INIT_VIDEO); // initialisation de la video
if (SDL_Init(SDL_INIT_VIDEO)==-1) // test de la video
{
fprintf(stderr,"error initialisation :%s",SDL_GetError());
return (EXIT_FAILURE);
}
ecran=SDL_SetVideoMode(800,600,32,SDL_HWSURFACE | SDL_DOUBLEBUF); // creation de la video
SDL_ShowCursor(SDL_DISABLE); // curseur de la souris invisible
while (continuer) // evenement
{
SDL_WaitEvent(&event); // attente d'une action
switch(event.type)
{
case SDL_QUIT :continuer=0;break; // si appuiyer sur la croix de la fenetre (quiter)
case SDL_MOUSEMOTION : pos.x=event.motion.x; // si la souris bouge alors bouger aussi le viseur
pos.y=event.motion.y;break;
case SDL_MOUSEBUTTONDOWN : postir.x=event.motion.x+22; // si on clic sur la souris
postir.y=event.motion.y+22;
// la position du tir doit etre au millieu
SDL_BlitSurface(alien,NULL,ecran,&posalien); // du viseur ,
SDL_FillRect(tir,NULL,SDL_MapRGB(tir->format,255,0,0)); //dessin le tir et apllique sur l'ecran
SDL_BlitSurface(carre,NULL,ecran,&pos);
SDL_BlitSurface(tir,NULL,ecran,&postir);
SDL_Flip(ecran); // mise a jour de l'ecran
switch(v) // v etant un indice si
{
case 0: if ((((postir.x >=posalien.x))&& (postir.x<=(posalien.x+100))&&(postir.y >=(posalien.y)&&(postir.y<=posalien.y+100)))) // si la position du tir et entre l'image
{
v=1; // alors mais l'indice a 1
}
break;
case 1: if ((((postir.x >=posalien.x))&& (postir.x<=(posalien.x+100))&&(postir.y >=(posalien.y)&&(postir.y<=posalien.y+100))))
{
v=2;
}
break;
case 2: if ((((postir.x >=posalien.x))&& (postir.x<=(posalien.x+100))&&(postir.y >=(posalien.y)&&(postir.y<=posalien.y+100))))
{
v=3;
}
break;
case 3: if ((((postir.x >=posalien.x))&& (postir.x<=(posalien.x+100))&&(postir.y >=(posalien.y)&&(postir.y<=posalien.y+100))))
{
v=4;
}
break;
case 4: if ((((postir.x >=posalien.x))&& (postir.x<=(posalien.x+100))&&(postir.y >=(posalien.y)&&(postir.y<=posalien.y+100))))
{
v=5;
}
break;
case 5: if ((((postir.x >=posalien.x))&& (postir.x<=(posalien.x+100))&&(postir.y >=(posalien.y)&&(postir.y<=posalien.y+100))))
{
v=6;
}
break;
case 6: if ((((postir.x >=posalien.x))&& (postir.x<=(posalien.x+100))&&(postir.y >=(posalien.y)&&(postir.y<=posalien.y+100))))
{
v=7;
}
break;
case 7: if ((((postir.x >=posalien.x))&& (postir.x<=(posalien.x+100))&&(postir.y >=(posalien.y)&&(postir.y<=posalien.y+100))))
{
v=8;
}
break;
case 8: if ((((postir.x >=posalien.x))&& (postir.x<=(posalien.x+100))&&(postir.y >=(posalien.y)&&(postir.y<=posalien.y+100))))
{
v=9;
}
break;
case 9: if ((((postir.x >=posalien.x))&& (postir.x<=(posalien.x+100))&&(postir.y >=(posalien.y)&&(postir.y<=posalien.y+100))))
{
v=10;
}
break;
}
break;
}
temps_actuel=SDL_GetTicks(); // si je n'attents pas d'evenement alors
if (temps_actuel - temps_precedent >800) // si le temps actuel est superieur a 800 ms alors change la position
{ // de l'image alien
posalien.x=rand()%600;posalien.y=rand()%400;
temps_precedent=temps_actuel;
}
SDL_FillRect(ecran,NULL,SDL_MapRGB(carre->format,2,25,255)); // mise a jour du viseur ,image alien et l'ecran
SDL_FillRect(carre,NULL,SDL_MapRGB(carre->format,255,255,255));
SDL_BlitSurface(alien,NULL,ecran,&posalien);
SDL_BlitSurface(carre,NULL,ecran,&pos);
SDL_Flip(ecran);
switch(v) // test indice v si
{
case 1: // v==1 alors dessine le point sur l'ecran
SDL_FillRect(point[0],NULL,SDL_MapRGB(point[0]->format,25,25,25));
SDL_BlitSurface(point[0],NULL,ecran,&pospoint[0]);
SDL_Flip(ecran); // mise a jour
break;
case 2 :
for (int i=0;i<2;i++) // si v==2 alors dessine les deux points sur l'ecran etc..
{
SDL_FillRect(point[i],NULL,SDL_MapRGB(point[i]->format,25,25,25));
SDL_BlitSurface(point[i],NULL,ecran,&pospoint[i]);
}
SDL_Flip(ecran);break;
case 3 :
for (int i=0;i<3;i++)
{
SDL_FillRect(point[i],NULL,SDL_MapRGB(point[i]->format,25,25,25));
SDL_BlitSurface(point[i],NULL,ecran,&pospoint[i]);
}
SDL_Flip(ecran);
break;
case 4 :
for (int i=0;i<4;i++)
{
SDL_FillRect(point[i],NULL,SDL_MapRGB(point[i]->format,25,25,25));
SDL_BlitSurface(point[i],NULL,ecran,&pospoint[i]);
}
SDL_Flip(ecran);
break;
case 5 :
for (int i=0;i<5;i++)
{
SDL_FillRect(point[i],NULL,SDL_MapRGB(point[i]->format,25,25,25));
SDL_BlitSurface(point[i],NULL,ecran,&pospoint[i]);
}
SDL_Flip(ecran);
break;
case 6 :
for (int i=0;i<6;i++)
{
SDL_FillRect(point[i],NULL,SDL_MapRGB(point[i]->format,25,25,25));
SDL_BlitSurface(point[i],NULL,ecran,&pospoint[i]);
}
SDL_Flip(ecran);
break;
case 7 :
for (int i=0;i<7;i++)
{
SDL_FillRect(point[i],NULL,SDL_MapRGB(point[i]->format,25,25,25));
SDL_BlitSurface(point[i],NULL,ecran,&pospoint[i]);
}
SDL_Flip(ecran);
break;
case 8 :
for (int i=0;i<8;i++)
{
SDL_FillRect(point[i],NULL,SDL_MapRGB(point[i]->format,25,25,25));
SDL_BlitSurface(point[i],NULL,ecran,&pospoint[i]);
}
SDL_Flip(ecran);
break;
case 9:
for (int i=0;i<9;i++)
{
SDL_FillRect(point[i],NULL,SDL_MapRGB(point[i]->format,25,25,25));
SDL_BlitSurface(point[i],NULL,ecran,&pospoint[i]);
}
SDL_Flip(ecran);
break;
case 10 : v=0;break;
}
}
for (int i=0;i<10;i++) // effacement des points,image,tir,viseur et quitter
{
SDL_FreeSurface(point[i]);
}
SDL_FreeSurface(alien);
SDL_FreeSurface(tir);
SDL_FreeSurface(carre);
SDL_Quit();
return (EXIT_SUCCESS);
}
merci ! |
Partager