5 void move_car(struct _car *car,int keys,SDL_Surface *fun)
13 /* get the pixel color under the center of car in the function map */
14 c=getpixel(fun,car->x,car->y);
15 /* red layer (checkpoints) */
16 /* green layer (road quality) */
17 /* blue layer (grip) */
18 SDL_GetRGB(c,fun->format,&r,&g,&b);
20 if (keys & 8) /* up */
21 car->speed+=0.01*2*COEFF;
22 if (keys & 4) /* down */
23 car->speed-=0.01*COEFF;
24 if (keys & 2) /* left */
27 car->angle+=0.01*(255-b)/255*COEFF;
29 car->angle-=0.01*(255-b)/255*COEFF;
31 if (keys & 1) /* right */
34 car->angle-=0.01*(255-b)/255*COEFF;
36 car->angle+=0.01*(255-b)/255*COEFF;
39 /* limit angle between 0 and 2*pi */
40 if (car->angle<0) car->angle+=2*M_PI;
41 if (car->angle>2*M_PI) car->angle-=2*M_PI;
43 /* update the speed depending on the road quality */
44 car->speed-=car->speed*(255-g)/1000;
46 /* if it is a wall we move back to the last position */
54 /* save the old position and compute the new one */
58 car->x-=cos(car->angle)*car->speed;
59 car->y-=sin(car->angle)*car->speed;
61 /* collision with the border of the screen */
62 if (car->x<car->w/2 || car->x>fun->w-car->w/2 || car->y<car->h/2 || car->y>fun->h-car->h/2)
70 /* if we are on the next checkpoint, validate it */
71 if (r/8==car->lastcheck+1)
73 /* If we validate a missed checkpoint */
74 if (car->lapflag==3) car->lapflag=4;
78 /* if we missed a checkpoint */
79 if ((r/8>car->lastcheck+1) && (car->lastcheck!=0)) car->lapflag=3;
81 /* if we validate all and start over, we complete a turn */
82 if (r/8==0 && car->lastcheck==31)
84 /* reset turn variables */
90 /* if we are at the start but not each checkpoint validate, it's an incomplete lap */
91 if (r/8==0 && r!=0 && car->lastcheck!=31 && car->lastcheck>0)
97 /* if the car is braking, display red lights */
98 if (keys & 4 && car->speed>0.1) car->lights_brake=1; else car->lights_brake=0;
100 /* if the car is going backwards, display white lights */
101 if (car->speed<-0.1) car->lights_backwards=1; else car->lights_backwards=0;
103 /* if the car is stopped, then warning */
104 if (car->speed>=-0.1 && car->speed<=0.1)
105 car->lights_warning=1;
107 car->lights_warning=0;