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
| #include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <time.h>
const int MIN = 1, MAX=4;
int aleatoire(int NBaleatoire);//fontion qui renvoi un nobre aléatoire
void bouger_gauche(int positionCube);//fontion permettant de diriger le cube
void bouger_droite(int positionCube);
void bouger_haut(int positionCube);
void bouger_bas(int positionCube);
int main(int arcgc, char *argv[])
{
SDL_Surface *ecran = NULL, *lifeCube = NULL;
int continuer = 1;
int nombreMystere = 0;
SDL_Event event;
SDL_Rect positionCube;
positionCube.x = (640/2)-(30/2);
positionCube.y = (480/2)-(30/2);
SDL_Init(SDL_INIT_VIDEO);
ecran = SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
lifeCube = SDL_CreateRGBSurface(SDL_HWSURFACE, 30, 30, 32, 0,0,0,0);
SDL_FillRect(lifeCube,NULL,SDL_MapRGB(lifeCube->format,34,66,196));
aleatoire(nombreMystere);
while(continuer)
{
SDL_PollEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = 0;
}
if (nombreMystere==1)
{
bouger_gauche(positionCube.x);
}
else if (nombreMystere == 2)
{
bouger_droite(positionCube.x);
}
else if (nombreMystere==3)
{
bouger_haut(positionCube.y);
}
else if (nombreMystere==4)
{
bouger_bas(positionCube.y);
}
SDL_FillRect(ecran,NULL,SDL_MapRGB(ecran->format,179,255,176));
SDL_BlitSurface(lifeCube,NULL,ecran,&positionCube);
SDL_Flip(ecran);
}
SDL_FreeSurface(lifeCube);
SDL_QUIT;
}
int aleatoire(int NBaleatoire)
{
srand(time(NULL));
NBaleatoire = (rand()%(MAX-MIN+1))+MIN;
return NBaleatoire;
}
int bouger_droite(int positionCube)
{
int tempsPrecedent = 0, tempsActuel = 0;
//gérer le temps :
tempsActuel = SDL_GetTicks();
if (tempsActuel - tempsPrecedent > 30)
{
positionCube++;
tempsPrecedent = tempsActuel;
}
return positionCube;
}
int bouger_gauche(int positionCube)
{
int positionCube;
int tempsPrecedent = 0, tempsActuel = 0;
//gérer le temps :
tempsActuel = SDL_GetTicks();
if (tempsActuel - tempsPrecedent > 30)
{
positionCube--;
tempsPrecedent = tempsActuel;
}
return positionCube;
}
int bouger_haut(int positionCube)
{
int positionCube;
int tempsPrecedent = 0, tempsActuel = 0;
//gérer le temps :
tempsActuel = SDL_GetTicks();
if (tempsActuel - tempsPrecedent > 30)
{
positionCube++;
tempsPrecedent = tempsActuel;
}
return positionCube;
}
int bouger_bas(int positionCube)
{
int positionCube;
int tempsPrecedent = 0, tempsActuel = 0;
//gérer le temps :
tempsActuel = SDL_GetTicks();
if (tempsActuel - tempsPrecedent > 30)
{
positionCube--;
tempsPrecedent = tempsActuel;
}
return positionCube;
} |
Partager