Bien programmer en langage C
Vous souhaitez réagir à ce message ? Créez un compte en quelques clics ou connectez-vous pour continuer.
-24%
Le deal à ne pas rater :
PC Portable Gaming 15.6″ Medion Erazer Deputy P40 (FHD 144Hz, RTX ...
759.99 € 999.99 €
Voir le deal

SDL : jeu Morpion

2 participants

Aller en bas

SDL : jeu Morpion Empty SDL : jeu Morpion

Message  C_C Sam 4 Avr 2009 - 17:59

Bonjour !
Je suis entrain de tenter de faire un jeu de morpion ...
Seulement, j'ai un problème : c'est toujours le joueur 2 qui joue ... j'ai donc toujours des ronds, jamais de croix

Mon code est peut être encore brouillon, et surtout je n'ai pas fini, mais ce problème m'embête ...

Pouvez vous m'aider pour ça sans trop m'aider pour la suite (j'aimerais continuer tout seul)

Bien sûr s'il y a des horreurs, dites le moi :) Je commence juste la SDL ...
Code:

#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>

int main(int argc, char *argv[])
{
    SDL_Surface *ecran = NULL, *imageDeFond = NULL, *croix = NULL, *rond = NULL;
    SDL_Rect positionFond, position[9] = {0};
    SDL_Event event;
    int continuer = 1, commencer = 2, i, tableauSigne[9] = {0}, joueurCommence = 1;

    positionFond.x = 0;
    positionFond.y = 0;


    SDL_Init(SDL_INIT_VIDEO);

    SDL_WM_SetIcon(SDL_LoadBMP("morpionIco2.bmp"), NULL);

    ecran = SDL_SetVideoMode(300, 300, 32, SDL_HWSURFACE);
    SDL_WM_SetCaption("Morpion", NULL);

    imageDeFond = SDL_LoadBMP("Grille Morpion.bmp");
    SDL_BlitSurface(imageDeFond, NULL, ecran, &positionFond);

    croix = SDL_LoadBMP("Croix Morpion.bmp");
    rond = SDL_LoadBMP("Rond Morpion.bmp");

    while (continuer) // boucle de jeu
    {
        SDL_Flip(ecran);

        SDL_WaitEvent(&event);

        switch(event.type) // évènement
        {
            case SDL_QUIT:
                continuer = 0;
                break;

            case SDL_KEYDOWN:
                switch (event.key.keysym.sym)
                {
                    case SDLK_ESCAPE:
                        continuer = 0;
                        break;


                    case SDLK_1:
                        if(joueurCommence == 1)
                            tableauSigne[0] = 1;

                        else if(joueurCommence == 2)
                            tableauSigne[0] = -1;

                            position[0].x = 0;
                            position[0].y = 0;
                    break;

                    case SDLK_2:
                        if(joueurCommence == 1)
                            tableauSigne[1] = 1;

                        else if(joueurCommence == 2)
                            tableauSigne[1] = -1;

                            position[1].x = 100;
                            position[1].y = 0;

                    break;

                    case SDLK_3:
                        if(joueurCommence == 1)
                            tableauSigne[2] = 1;

                        else if(joueurCommence == 2)
                            tableauSigne[2] = -1;

                            position[2].x = 200;
                            position[2].y = 0;
                    break;

                    case SDLK_4:
                        if(joueurCommence == 1)
                            tableauSigne[3] = 1;

                        else if(joueurCommence == 2)
                            tableauSigne[3] = -1;

                            position[3].x = 0;
                            position[3].y = 100;
                    break;

                    case SDLK_5:
                        if(joueurCommence == 1)
                            tableauSigne[4] = 1;

                        else if(joueurCommence == 2)
                            tableauSigne[4] = -1;

                            position[4].x = 100;
                            position[4].y = 100;
                    break;

                    case SDLK_6:
                        if(joueurCommence == 1)
                            tableauSigne[5] = 1;

                        else if(joueurCommence == 2)
                            tableauSigne[5] = -1;

                            position[5].x = 200;
                            position[5].y = 100;
                    break;

                    case SDLK_7:
                        if(joueurCommence == 1)
                            tableauSigne[6] = 1;

                        else if(joueurCommence == 2)
                            tableauSigne[6] = -1;

                            position[6].x = 0;
                            position[6].y = 200;
                    break;

                    case SDLK_8:
                        if(joueurCommence == 1)
                            tableauSigne[7] = 1;

                        else if(joueurCommence == 2)
                            tableauSigne[7] = -1;

                            position[7].x = 100;
                            position[7].y = 200;
                    break;

                    case SDLK_9:
                        if(joueurCommence == 1)
                            tableauSigne[8] = -1;

                        else if(joueurCommence == 2)
                            tableauSigne[8] = -1;

                            position[8].x = 200;
                            position[8].y = 200;
                    break;

                    default: break;
                }
                break;
        }

        for(i=0; i<9; i++)
        {
            if(tableauSigne[i] == 1)
                SDL_BlitSurface(croix, NULL, ecran, &position[i]);


            else if(tableauSigne[i] == -1)
                SDL_BlitSurface(rond, NULL, ecran, &position[i]);
        }


        if(joueurCommence == 1) // changement de joueur
            joueurCommence = 2;

        else if(joueurCommence == 2)
            joueurCommence = 1;
    }

    SDL_FreeSurface(imageDeFond);
    SDL_FreeSurface(croix);
    SDL_FreeSurface(rond);

    SDL_Quit();

    return EXIT_SUCCESS;
}

Merci d'avance !

C_C

Messages : 3
Date d'inscription : 28/03/2009

Revenir en haut Aller en bas

SDL : jeu Morpion Empty Re: SDL : jeu Morpion

Message  -ed- Dim 5 Avr 2009 - 11:56

C_C a écrit:Je suis entrain de tenter de faire un jeu de morpion ...
Seulement, j'ai un problème : c'est toujours le joueur 2 qui joue ... j'ai donc toujours des ronds, jamais de croix

Mon code est peut être encore brouillon, et surtout je n'ai pas fini, mais ce problème m'embête ...

Pouvez vous m'aider pour ça sans trop m'aider pour la suite (j'aimerais continuer tout seul)

Bien sûr s'il y a des horreurs, dites le moi :) Je commence juste la SDL ...
[/code]
C'est
Code:

        case SDLK_9:
            if (joueurCommence == 1)
              tableauSigne[8] = 1; /* -ed- correction ... */

            else if (joueurCommence == 2)
              tableauSigne[8] = -1;

            break;

ceci a l'air de fonctionner chez moi (en simulation) :
Code:

#include <stdlib.h>
#include <stdio.h>
#if 1
#include <SDL/SDL.h>
#else
#include "sim-sdl.h"
#endif

enum
{
  I_1,
  I_2,
  I_3,
  I_4,
  I_5,
  I_6,
  I_7,
  I_8,
  I_9,
  CROIX,
  ROND
};

int main (int argc, char *argv[])
{
  SDL_Surface *ecran = NULL, *imageDeFond = NULL, *croix = NULL, *rond =
      NULL;
  SDL_Rect positionFond = { 0, 0 };
  SDL_Rect position[9] = {
      {0, 0}, {100, 0}, {200, 0}, /* 1 2 3 */
      {0, 100}, {100, 100}, {200, 100}, /* 4 5 6 */
      {0, 200}, {100, 200}, {200, 200}, /* 7 8 9 */
  };

  SDL_Event event;
  int continuer = 1, i, tableauSigne[9] = { 0 };

  int JoueurA = 1;

  positionFond.x = 0;
  positionFond.y = 0;

  SDL_Init (SDL_INIT_VIDEO);

  SDL_WM_SetIcon (SDL_LoadBMP ("morpionIco2.bmp"), NULL);

  ecran = SDL_SetVideoMode (300, 300, 32, SDL_HWSURFACE);
  SDL_WM_SetCaption ("Morpion", NULL);

  imageDeFond = SDL_LoadBMP ("Grille-Morpion.bmp");
  SDL_BlitSurface (imageDeFond, NULL, ecran, &positionFond);

  croix = SDL_LoadBMP ("Croix-Morpion.bmp");
  rond = SDL_LoadBMP ("Rond-Morpion.bmp");

/* boucle de jeu */
  while (continuer)
  {
      SDL_Flip (ecran);

      SDL_WaitEvent (&event);

/* évènement */
      switch (event.type)
      {
      case SDL_QUIT:
        continuer = 0;
        break;

      case SDL_KEYDOWN:
        switch (event.key.keysym.sym)
        {
        case SDLK_ESCAPE:
            continuer = 0;
            break;

        case SDLK_1:
            if (JoueurA)
              tableauSigne[I_1] = CROIX;
            else
              tableauSigne[I_1] = ROND;

            break;

        case SDLK_2:
            if (JoueurA)
              tableauSigne[I_2] = CROIX;
            else
              tableauSigne[I_2] = ROND;

            break;

        case SDLK_3:
            if (JoueurA)
              tableauSigne[I_3] = CROIX;
            else
              tableauSigne[I_3] = ROND;
            break;

        case SDLK_4:
            if (JoueurA)
              tableauSigne[I_4] = CROIX;
            else
              tableauSigne[I_4] = ROND;
            break;

        case SDLK_5:
            if (JoueurA)
              tableauSigne[I_5] = CROIX;
            else
              tableauSigne[I_5] = ROND;
            break;

        case SDLK_6:
            if (JoueurA)
              tableauSigne[I_6] = CROIX;
            else
              tableauSigne[I_6] = ROND;
            break;

        case SDLK_7:
            if (JoueurA)
              tableauSigne[I_7] = CROIX;
            else
              tableauSigne[I_7] = ROND;
            break;

        case SDLK_8:
            if (JoueurA)
              tableauSigne[I_8] = CROIX;
            else
              tableauSigne[I_8] = ROND;
            break;

        case SDLK_9:
            if (JoueurA)
              tableauSigne[I_9] = CROIX;
            else
              tableauSigne[I_9] = ROND;
            break;

        default:
            ;
        }
        break;
      }

      for (i = 0; i < 9; i++)
      {
        if (tableauSigne[i] == CROIX)
            SDL_BlitSurface (croix, NULL, ecran, &position[i]);

        else if (tableauSigne[i] == ROND)
            SDL_BlitSurface (rond, NULL, ecran, &position[i]);
      }

/* changement de joueur */
      JoueurA = !JoueurA;
  }

  SDL_FreeSurface (imageDeFond);
  SDL_FreeSurface (croix);
  SDL_FreeSurface (rond);

  SDL_Quit ();

  (void) argc;
  (void) argv;
  return EXIT_SUCCESS;
}

-ed-
-ed-
Admin
Admin

Messages : 290
Date d'inscription : 26/05/2008
Age : 67
Localisation : Paris 14eme arrondissement (75, France)

http://bien-programmer.fr

Revenir en haut Aller en bas

Revenir en haut


 
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum
Ne ratez plus aucun deal !
Abonnez-vous pour recevoir par notification une sélection des meilleurs deals chaque jour.
IgnorerAutoriser