Bonjour a tous j’ai un problème au niveau de la gestion de mon snake (son déplacement); Le serpent en grandissant insère les élements tjrs en diagonale ce qui selon moi n’est pas Normale: Ci dessous Le Code Complet: Merci de bien vouloir me suggérer une autre façon de voir le problème si nécessaire . J’accepte tout critique !
include <stdio.h>
include <stdbool.h>
include <stdlib.h>
include <time.h>
include <SDL2/SDL.h>
include <SDL2/SDL_ttf.h>
include <SDL2/SDL_image.h>
include <SDL2/SDL_mixer.h>
define WIN_WIDTH 800
define WIN_HEIGHT 700
define N_MAX 100
define SPEED 4
enum {UP,DOWN,LEFT,RIGHT}; typedef struct { SDL_bool key[SDL_NUM_SCANCODES]; SDL_bool quit; int mousex,mousey; int mousexrel,mouseyrel; int xwheel,ywheel; SDL_bool mouse[6]; }Input;
typedef struct { SDL_Texture *image; SDL_Rect pos[N_MAX]; int direction; int taille; }Serpent;
typedef struct { SDL_Texture *image; SDL_Rect pos; }Pomme;
int init_SDL(SDL_Window window,SDL_Renderer renderer); int setWindowColor(SDL_Renderer renderer,SDL_Color color); void updateEvents(Input in); SDL_Texture loadImage(SDL_Renderer renderer,char path); int nbreAleatoire(int max,int min); int collision(SDL_Rect pos1,SDL_Rect pos2); int game_play(Input in,Serpent serpent,Pomme pomme); int affichage(Serpent serpent,Pomme pomme,SDL_Renderer *renderer);
int init_SDL(SDL_Window window,SDL_Renderer renderer) { SDL_Surface *icone = NULL; if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)) { fprintf(stderr, " Erreur SDL_Init() %s\n",SDL_GetError()); return -1; } if(!IMG_Init(IMG_INIT_JPG)) { fprintf(stderr, " Erreur IMG_Init() %s\n",IMG_GetError()); return -1; } if(Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,2,2048)) { fprintf(stderr, " Erreur Mix_OpenAudio() %s\n",Mix_GetError() ); return -1; }
1 2 3 4 5 6 7 8 9 10 11 12 | *window = SDL_CreateWindow("Test",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,WIN_WIDTH,WIN_HEIGHT,SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL); if(!*window) { fprintf(stderr, " SDL_CreateWindow() %s\n",SDL_GetError() ); return -1; } *renderer = SDL_CreateRenderer(*window,-1,SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC); if(!*renderer) { fprintf(stderr, " SDL_CreateRenderer() %s\n",SDL_GetError() ); return -1; } |
icone = IMG_Load("sdl_icone.bmp"); if(!icone) { fprintf(stderr, " Erreur IMG_Load() %s\n",IMG_GetError() ); return -1; } SDL_SetWindowIcon(window,icone); SDL_FreeSurface(icone); icone = NULL; return 0; } int collision(SDL_Rect pos1,SDL_Rect pos2) { if(pos1.x + pos1.w <= pos2.x pos1.x>= pos2.x + pos2.w) return 0; if(pos1.y + pos1.h <= pos2.y pos1.y >= pos2.y + pos2.h) return 0; return 1; } int nbreAleatoire(int max,int min) { static int initialized=0; if(!initialized) { initialized = 1; srand(time(NULL)); } return rand()%(max - min)+min; } int setWindowColor(SDL_Renderer renderer,SDL_Color color) { if(SDL_SetRenderDrawColor(renderer,color.r,color.g,color.b,color.a)) { fprintf(stderr, "%s\n", SDL_GetError()); return -1; } if(SDL_RenderClear(renderer)) { fprintf(stderr, "%s\n",SDL_GetError() ); return -1; } return 0; } int init_Game(Serpent serpent,Pomme pomme,SDL_Renderer *renderer) {
serpent->image = loadImage(renderer,"rouge.bmp"); if(SDL_QueryTexture(serpent->image,NULL,NULL,&serpent->pos[0].w,&serpent->pos[0].h)) { fprintf(stderr, " Erreur SDL_QueryTexture() %s\n", SDL_GetError()); return -1; } serpent->pos[0].x = nbreAleatoire(800,0); serpent->pos[0].y = nbreAleatoire(700,0); serpent->direction = UP; serpent->taille = 1; pomme->image = loadImage(renderer,"bleu.bmp"); if(SDL_QueryTexture(pomme->image,NULL,NULL,&pomme->pos.w,&pomme->pos.h)) { fprintf(stderr, " Erreur SDL_QueryTexture() %s\n", SDL_GetError()); return -1; } do{ pomme->pos.x = nbreAleatoire(800,0); pomme->pos.y = nbreAleatoire(700,0); }while(collision(serpent->pos[0],pomme->pos));
return 0; }
int game_play(Input in,Serpent serpent,Pomme *pomme) {
if(in->key[SDL_SCANCODE_UP]) { serpent->direction = UP; } if(in->key[SDL_SCANCODE_DOWN]) { serpent->direction = DOWN; } if(in->key[SDL_SCANCODE_RIGHT]) { serpent->direction = RIGHT; } if(in->key[SDL_SCANCODE_LEFT]) { serpent->direction = LEFT; } switch(serpent->direction) { case UP: { serpent->pos[0].y -=SPEED; } break; case DOWN: { serpent->pos[0].y +=SPEED; } break; case RIGHT: { serpent->pos[0].x +=SPEED; } break; case LEFT: { serpent->pos[0].x -=SPEED; } break; default: break; } if(collision(serpent->pos[0],pomme->pos)){ pomme->pos.x = nbreAleatoire(800,0); pomme->pos.y = nbreAleatoire(700,0); serpent->taille++; };
return 0; }
int affichage(Serpent serpent,Pomme pomme,SDL_Renderer *renderer) { if(SDL_RenderClear(renderer)) { fprintf(stderr, " Erreur SDL_RenderClear() %s\n",SDL_GetError() ); return -1; }
for (int i = 0; i <=serpent->taille; ++i) { if(i>0) { serpent->pos[i].x = serpent->pos[i-1].x; serpent->pos[i].y = serpent->pos[i-1].y; serpent->pos[i].w = serpent->pos[i-1].w; serpent->pos[i].h = serpent->pos[i-1].h; } if(SDL_RenderCopy(renderer,serpent->image,NULL,&serpent->pos[i])) { fprintf(stderr, " Erreur SDL_RenderCopy() %s\n", SDL_GetError()); return -1; }
if(SDL_RenderCopy(renderer,pomme->image,NULL,&pomme->pos)) { fprintf(stderr, "Erreur SDL_RenderCopy() %s\n", SDL_GetError()); return -1; } }
return 0; }
void updateEvents(Input *in)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT: in->quit = SDL_TRUE;
break;
case SDL_KEYDOWN:
in->key[event.key.keysym.scancode] = SDL_TRUE;
break;
case SDL_KEYUP:
in->key[event.key.keysym.scancode] = SDL_FALSE;
break;
case SDL_MOUSEMOTION:
{
in->mousex = event.motion.x;
in->mousey = event.motion.y;
in->mousexrel = event.motion.xrel;
in->mouseyrel = event.motion.yrel;
}
break;
case SDL_MOUSEWHEEL:
{
in->xwheel = event.wheel.x;
in->ywheel = event.wheel.y;
}
break;
case SDL_MOUSEBUTTONDOWN:
in->mouse[event.button.button] = SDL_TRUE;
break;
case SDL_MOUSEBUTTONUP:
in->mouse[event.button.button] = SDL_FALSE;
break;
default:
break;
1 2 | } } |
} SDL_Texture loadImage(SDL_Renderer renderer,char path) { SDL_Texture textureL = NULL; SDL_Surface *image = IMG_Load(path); if(!image) { fprintf(stderr, " Erreur IMG_Load() %s\n",IMG_GetError() ); return NULL; } if(SDL_SetColorKey(image,SDL_TRUE,SDL_MapRGB(image->format,255,255,255))) { fprintf(stderr, " Erreur SDL_SetColorKey() %s\n",SDL_GetError()); exit (EXIT_FAILURE); } textureL = SDL_CreateTextureFromSurface(renderer,image); if(!textureL) { fprintf(stderr, " Erreur SDL_CreateTextureFromSurface()%s\n",SDL_GetError()); exit(EXIT_FAILURE); } SDL_FreeSurface(image); image = NULL; return textureL; }
int main(int argc, char *argv[]) {
SDL_Window window = NULL; SDL_Renderer renderer = NULL; //SDL_Color color = {0,255,0};
Input in; Serpent serpent; Pomme pomme;
int statut = EXIT_FAILURE;
if(init_SDL(&window,&renderer)) goto Quit; memset(&in,SDL_FALSE,sizeof(Input)); if(init_Game(&serpent,&pomme,renderer)) goto Quit;
while(!in.quit) { updateEvents(&in);
if(game_play(&in,&serpent,&pomme)) goto Quit; if(affichage(&serpent,&pomme,renderer)) goto Quit;
SDL_RenderPresent(renderer); }
statut = EXIT_SUCCESS; Quit: if(renderer) SDL_DestroyRenderer(renderer); if(window) SDL_DestroyWindow(window); Mix_CloseAudio(); IMG_Quit(); SDL_Quit(); return statut;
}