IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Développement 2D, 3D et Jeux Discussion :

Problème de tire et de deplacement avec SDL


Sujet :

Développement 2D, 3D et Jeux

  1. #1
    Nouveau Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Avril 2015
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2015
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Problème de tire et de deplacement avec SDL
    Bonjour,
    je suis débutante en C et j ai des soucis avec la SDL, en faite je suis gravement en retard par rapport à mes collègue , je suis bloquée sur deux problème: le déplacement de mon sprite et le tire.
    Mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    #include "SDL.h"
    #define SCREEN_WIDTH  640
    #define SCREEN_HEIGHT 480
    #define SPRITE_SIZE    32
    #define ASTEROIDE1_SIZE 32
    #define ASTEROIDE2_SIZE 16
    #define ASTEROIDE3_SIZE 64
    #define BULLET_SIZE 8
     
     
     
    int gameover;
     
    /* source and destination rectangles */
    SDL_Rect rcArc1,rcArc2, rcArc3, rcBrc, rcSrc, rcSprite, rcBullet, rcAsteroide1, rcAsteroide2, rcAsteroide3;
    void HandleEvent(SDL_Event event);
     
    void HandleEvent(SDL_Event event)
    {
      switch (event.type) {
        /* close button clicked */
        case SDL_QUIT:
          gameover = 1;
          break;
     
        /* handle the keyboard */
        case SDL_KEYDOWN:
          switch (event.key.keysym.sym) {
            case SDLK_ESCAPE:
            case SDLK_q:
              gameover = 1;
              break;
          case SDLK_LEFT:
    	  rcSrc.x +=32;
    	  if (rcSrc.x == 1152)
    	    rcSrc.x = 32;
    	  break;
          case SDLK_RIGHT:
    	  rcSrc.x -=32;
    	  if (rcSrc.x == 32)
    	    rcSrc.x = 1152;
    	  break;
          case SDLK_UP:
    	  rcSrc.x = 288;
    	  rcSprite.y -= 10;
    	  while (rcSprite.y <= 0)
    	    rcSprite.y = 480;
    	  break;
          case SDLK_DOWN:
    	  rcSrc.x = 864;
    	  rcSprite.y += 10;
    	  while ( rcSprite.y >= 480)
    	    rcSprite.y = 0;
    	  break;
          }
          break;
      }
    }
     
     
      int main ( int argc, char *argv[] )
    {
      SDL_Surface *screen, *temp, *sprite, *Bullet, *grass,*asteroide1,*asteroide2,*asteroide3;
      int colorkey;
     
      /* initialize SDL */
      SDL_Init(SDL_INIT_VIDEO);
     
      /* set the title bar */
      SDL_WM_SetCaption("SDL Animation", "SDL Animation");
     
      /* create window */
      screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
     
      /* set keyboard repeat */
      SDL_EnableKeyRepeat(70, 70);
     
     
     
     /* load sprite */
      temp   = SDL_LoadBMP("greenship-v1.bmp");
      sprite = SDL_DisplayFormat(temp);
      SDL_FreeSurface(temp);
     
      /* load bullet */
      temp   = SDL_LoadBMP("bullet02.bmp");
      Bullet = SDL_DisplayFormat(temp);
      SDL_FreeSurface(temp);
     
     
      /* load asteroidmodel1 */
      temp   = SDL_LoadBMP("asteroid-model1-32_32x32.bmp");
      asteroide1 = SDL_DisplayFormat(temp);
      SDL_FreeSurface(temp);
     
      /*load asteroidmodel2*/
      temp   = SDL_LoadBMP("asteroid-model1-32_16x16.bmp");
      asteroide2 = SDL_DisplayFormat(temp);
      SDL_FreeSurface(temp);
     
      /*load asteroidmodel3*/
      temp   = SDL_LoadBMP("asteroid-model1-32_64x64.bmp");
      asteroide3 = SDL_DisplayFormat(temp);
      SDL_FreeSurface(temp);
     /*_______________________________________________________________  
      
      
      /* setup sprite colorkey and turn on RLE */
      colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
      SDL_SetColorKey(sprite, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
     
      /* setup bullet colorkey and turn on RLE */
      colorkey = SDL_MapRGB(screen->format, 255, 125, 32);
      SDL_SetColorKey(Bullet, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
     
      /* setup asteroide1 colorkey and turn on RLE */
      colorkey = SDL_MapRGB(screen->format, 0, 255, 255);
      SDL_SetColorKey(asteroide1, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
     
      /* setup asteroide2 colorkey and turn on RLE */
      colorkey = SDL_MapRGB(screen->format, 0, 255, 255);
      SDL_SetColorKey(asteroide2, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
     
      /* setup asteroide3 colorkey and turn on RLE */
      colorkey = SDL_MapRGB(screen->format, 0, 255, 255);
      SDL_SetColorKey(asteroide3, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);
     
     
      /* load grass */
      temp  = SDL_LoadBMP("backgroundlvl1.bmp");
      grass = SDL_DisplayFormat(temp);
      SDL_FreeSurface(temp);
     
     
      /* set sprite position */
      rcSprite.x = (SCREEN_WIDTH/2-SPRITE_SIZE /2);
      rcSprite.y = (SCREEN_HEIGHT/2-SPRITE_SIZE/2);
     
      /* set bullet  position */
      rcBullet.x = 0;
      rcBullet.y = 0;
     
      /* set asteroide1 position */
      rcAsteroide1.x = 300;
      rcAsteroide1.y = 300;
     
      /* set asteroide2 position */
      rcAsteroide2.x = 200;
      rcAsteroide2.y = 200;
     
      /* set asteroide3 position */
      rcAsteroide3.x = 400;
      rcAsteroide3.y = 400;
     
     
     
     
      /* set animation frame sprite */
      rcSrc.x = 288;
      rcSrc.y = 0;
      rcSrc.w = SPRITE_SIZE;
      rcSrc.h = SPRITE_SIZE;
     
      /* set animation frame bullet */
      rcBrc.x = 0;
      rcBrc.y = 0;
      rcBrc.w = BULLET_SIZE ;
      rcBrc.h = BULLET_SIZE ;
     
      /* set animation frame asteroide1 */
      rcArc1.x = 32;
      rcArc1.y = 0;
      rcArc1.w = ASTEROIDE1_SIZE;
      rcArc1.h = ASTEROIDE1_SIZE;
     
     /* set animation frame asteroide2 */
      rcArc2.x = 48;
      rcArc2.y = 0;
      rcArc2.w = ASTEROIDE2_SIZE;
      rcArc2.h = ASTEROIDE2_SIZE;
     
      /* set animation frame asteroide3 */
      rcArc3.x = 192;
      rcArc3.y = 0;
      rcArc3.w = ASTEROIDE3_SIZE;
      rcArc3.h = ASTEROIDE3_SIZE; 
     
      /*_____________________________________________________________*/
     
      gameover = 0;
     
      /* message pump */
      while (!gameover)
        {
          int count = 0;
          SDL_Event event;
     
          /* look for an event */
          if (SDL_PollEvent(&event)) {
    	HandleEvent(event);
          }
     
          /* rotation asteroides*/
          if (count == 0){
     
    	/*rotation asteroid1*/
            rcArc1.x += 32;
    	while( rcArc1.x == 1024  ){
    	  rcArc1.x = 32;
    	  rcAsteroide1.y +=1; 
    	  if(rcAsteroide1.y == 480){
    	    rcAsteroide1.y = 0;
    	  }
    	}
            /*rotation asteroid2*/
    	rcArc2.x += 16;
    	while( rcArc2.x == 512){
    	  rcArc2.x = 16;
    	  rcAsteroide2.y -=1; 
    	  if(rcAsteroide2.y == 0){
    	    rcAsteroide2.y = 480;
    	  }
    	}
    	/*rotation asteroid3*/
    	rcArc3.x += 64;
    	while( rcArc3.x == 2048){
    	  rcArc3.x = 64;
    	  rcAsteroide3.y +=1; 
    	  if(rcAsteroide3.y == 480){
    	    rcAsteroide3.y = 0;
    	  }
    	}
     
          /* draw the grass */
          SDL_BlitSurface(grass, NULL, screen, NULL);
     
          /* draw the sprite */
          SDL_BlitSurface(sprite, &rcSrc, screen, &rcSprite);
     
          /* draw the bullet */
          SDL_BlitSurface(Bullet, &rcBrc, screen, &rcBullet);
     
          /* draw the asteroidemodel1 */
          SDL_BlitSurface(asteroide1, &rcArc1, screen, &rcAsteroide1);
     
          /* draw the asteroidemodel2 */
          SDL_BlitSurface(asteroide2, &rcArc2, screen, &rcAsteroide2);
     
          /* draw the asteroidemodel3 */
          SDL_BlitSurface(asteroide3, &rcArc3, screen, &rcAsteroide3);
     
     
     
           /* update the screen */
          SDL_UpdateRect(screen, 0, 0, 0, 0);
          count ++;
          if ( count == 5)
    	count = 0;
          }
        }
      /* clean up */
      SDL_FreeSurface(asteroide1);
      SDL_FreeSurface(asteroide2);
      SDL_FreeSurface(asteroide3);
      SDL_FreeSurface(sprite);
      SDL_FreeSurface(Bullet);
      SDL_FreeSurface(grass);
      SDL_Quit();
     
      return 0;
    }
    je vous en supplie aidez moi

  2. #2
    Expert éminent sénior
    Avatar de Kannagi
    Homme Profil pro
    cyber-paléontologue
    Inscrit en
    Mai 2010
    Messages
    3 214
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : cyber-paléontologue

    Informations forums :
    Inscription : Mai 2010
    Messages : 3 214
    Points : 10 140
    Points
    10 140
    Par défaut
    Alors il y a la balise [ CODE][ /CODE] , si tu veux mettre du code sur le forum.

    Ensuite c'est quoi ton souci exactement ? le déplacement du perso c'est une position x et y.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    SDL_BlitSurface(sprite, &rcSrc, screen, &rcSprite);
    Comme ton rcSprite.x et rcSprite.y , pour faire un déplacement c'est événement clavier + incrémentation de la position (mais si tu veux gérer des collisions il faut mieux incrémenté une vitesse).

    Les tirs suivent la même logique sauf que c'est un gérer avec un tableau (de position donc).

    Ensuite je vois pas de SDL_Flip(ecran);.

Discussions similaires

  1. Deplacement de sprite avec SDL en langage c
    Par fab101 dans le forum SDL
    Réponses: 3
    Dernier message: 16/12/2013, 20h53
  2. Problème avec SDL et OCaml
    Par Rippalka dans le forum Caml
    Réponses: 2
    Dernier message: 15/10/2009, 11h21
  3. Problème de compilation avec sdl en c++
    Par Jean_guy dans le forum SDL
    Réponses: 6
    Dernier message: 05/02/2008, 15h51
  4. Réponses: 4
    Dernier message: 04/10/2007, 14h42
  5. Petit problème avec SDL
    Par Nyarlathotep dans le forum C
    Réponses: 10
    Dernier message: 01/07/2005, 09h10

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo