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

C Discussion :

Un problème avec un jeu de tennis


Sujet :

C

  1. #21
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par Electroniktor
    Si c'est évident, c'est même ce que j'ai fait en premier, mais si je supprime ce getch() , la raquette n'apparait plus .
    Et il faut bien dire au programme la manière dont c est déterminé (là c'est grâce à la simple pression d'une touche sur la clavier, donc getch() ) ?!
    Bah, relis ma réponse complètement...

  2. #22
    Membre du Club Avatar de Electroniktor
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 150
    Points : 55
    Points
    55
    Par défaut
    J'ai essayé de faire passer la fonction move_plot() en paramètre de la fonction move_ball() mais je n'y arrive pas . J'avais mis ...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    static void move_ball (struct ball *this, struct plot *this)
    ... mais je ne peux pas mettre deux fois *this.

    Alors j'aimerais savoir si à la place de this, je peux mettre n'importe quel mot, ou si il y a des mots bien spéciaux ?!

  3. #23
    Expert éminent sénior

    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    10 610
    Détails du profil
    Informations personnelles :
    Âge : 66
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 10 610
    Points : 17 916
    Points
    17 916
    Billets dans le blog
    2
    Par défaut
    Citation Envoyé par Electroniktor
    Alors j'aimerais savoir si à la place de this, je peux mettre n'importe quel mot, ou si il y a des mots bien spéciaux ?!
    bien sûr que tu peux mettre n'importe quoi....

    Emmanuel met this pour se rapprocher de ce qui se fait en langage objet.. mais ça ne va pas plus loin..

  4. #24
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par Electroniktor
    J'ai essayé de faire passer la fonction move_plot() en paramètre
    Tu veux sans doute dire la structure ?
    de la fonction move_ball() mais je n'y arrive pas . J'avais mis ...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    static void move_ball (struct ball *this, struct plot *this)
    ... mais je ne peux pas mettre deux fois *this.

    Alors j'aimerais savoir si à la place de this, je peux mettre n'importe quel mot, ou si il y a des mots bien spéciaux ?!
    Je mettrais p_ball et p_plot... tu fais comme tu veux, du moment que c'est clair. Evite les horreurs genre a, b ou pa, pb ou p1, p2...

  5. #25
    Membre du Club Avatar de Electroniktor
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 150
    Points : 55
    Points
    55
    Par défaut
    Bon ben j'ai fait le code , mais le jeu a parfois un comportement bizare : parfois, la balle est renvoyée lorsque il n'y a pas de raquette ! Et je ne vois d'où cela vient ...
    Le voilà :
    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio2.h>
    #include "affichage.h"
     
    #define MS(ms) (CLOCKS_PER_SEC / (1000.0 / (ms)))
     
    #define X_MIN  17
    #define X_MAX  65
    #define Y_MIN  6
    #define Y_MAX  22
     
    enum dir
    { NW, NE, SE, SW };
     
    struct ball
    {
       int x;
       int y;
       enum dir dir;
    };
     
    struct plot
    {
       int x;
       int y ;
    };
     
    int end = 0;
     
    static void move_ball (struct ball *this, struct plot *that, int *end)
    {
       if (this->x == 41)
       {
          gotoxy (this->x, this->y), putch ('|');
       }
       else
       {
          gotoxy (this->x, this->y), putch (' ');
       }
     
       switch (this->dir)
       {
       case NW:
          this->x--;
          this->y--;
     
          if (this->x == that->y && this->y == Y_MIN)
          {
             this->dir = SE;
             this->x++;
             this->y++;
          }
          else if (this->x == that->y)
          {
             this->dir = NE;
             this->x++;
          }
          else if (this->y == Y_MIN)
          {
             this->dir = SW;
             this->y++;
          }
          else if (this->x == X_MIN)
          {
             *end = 1;
          }
          break;
     
       case NE:
          this->x++;
          this->y--;
     
          if (this->x == X_MAX && this->y == Y_MIN)
          {
             this->dir = SW;
             this->x--;
             this->y++;
          }
          else if (this->x == X_MAX)
          {
             this->dir = NW;
             this->x--;
          }
          else if (this->y == Y_MIN)
          {
             this->dir = SE;
             this->y++;
          }
          break;
     
       case SE:
          this->x++;
          this->y++;
     
          if (this->x == X_MAX && this->y == Y_MAX)
          {
             this->dir = NW;
             this->x--;
             this->y--;
          }
          else if (this->x == X_MAX)
          {
             this->dir = SW;
             this->x--;
          }
          else if (this->y == Y_MAX)
          {
             this->dir = NE;
             this->y--;
          }
          break;
     
       case SW:
          this->x--;
          this->y++;
     
          if (this->x == that->y && this->y == Y_MAX)
          {
             this->dir = NE;
             this->x++;
             this->y--;
          }
          else if (this->x == that->y)
          {
             this->dir = SE;
             this->x++;
          }
          else if (this->y == Y_MAX)
          {
             this->dir = NW;
             this->y--;
          }
          else if(this->x == X_MIN)
          {
             *end = 1;  
          }
          break;
       }
       gotoxy (this->x, this->y), putch ('o');
    }
     
    static void move_plot (struct plot *this, int c)
    {
       switch (c)
       {
       case '2':
          if (this->y < Y_MAX - 1)
          {
             gotoxy (this->x, this->y), putch (' ');
             this->y++;
             gotoxy (this->x, this->y), putch ('|');
          }
          break;
     
       case '8':
          if (this->y > Y_MIN + 1)
          {
             gotoxy (this->x, this->y), putch (' ');
             this->y--;
             gotoxy (this->x, this->y), putch ('|');
          }
          break;
       }
    }
     
    int main (void)
    {
       struct ball ball = { 40, 15, NW };
       struct plot plot = { 19, 15 };
       clock_t echeance = clock () + MS (100);
     
       titre();
       terrain();
     
       /* positions initiales */
       gotoxy (ball.x, ball.y), putch ('o');
       gotoxy (plot.x, plot.y), putch ('|');
     
       /* mouvement */
       do
       {
          if (clock () > echeance)
          {
             move_ball (&ball, &plot, &end);
             echeance = clock () + MS (100);
          }
          if (kbhit ())
          {
             int c = getch ();
             switch (c)
             {
             case '2':
             case '8':
                move_plot (&plot, c);
                break;
             case 27:
                end = 1;
             }
          }
       } while (!end);
     
       getch();
       return 0;
    }

  6. #26
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par Electroniktor
    Bon ben j'ai fait le code , mais le jeu a parfois un comportement bizare : parfois, la balle est renvoyée lorsque il n'y a pas de raquette
    Bah, il y a de l'idée, mais il faut plus réfléchir à l'algorithme, bien poser les conditions, bien commenter etc.
    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
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio2.h>
    #include <windows.h>
     
    #if 0
    #include "affichage.h"
    #endif
     
    #define MS(ms) (CLOCKS_PER_SEC / (1000.0 / (ms)))
     
    #define DELAY 250
     
    #define X_MIN  17
    #define X_MAX  65
    #define Y_MIN  6
    #define Y_MAX  22
     
    enum dir
    { NW, NE, SE, SW };
     
    struct ball
    {
       int x;
       int y;
       enum dir dir;
    };
     
    struct plot
    {
       int x;
       int y;
    };
     
    int end = 0;
     
    static void move_ball (struct ball *this, struct plot *p_plot, int *end)
    {
       /* Si il n'y a pas collision avec la raquette */
       if (!(this->x == p_plot->x && this->y == p_plot->y))
       {
          gotoxy (this->x, this->y), putch (' ');
       }
     
       switch (this->dir)
       {
       case NW:
          this->x--;
          this->y--;
     
          /* Si il y a collision avec la raquette */
          if (this->x == p_plot->x && this->y == p_plot->y)
          {
             if (this->y == Y_MIN)
             {
                /* rebond a 180 degre */
                this->dir = SE;
                this->x++;
                this->y++;
             }
             else
             {
                /* rebond a 90 degre */
                this->dir = NE;
                this->x++;
             }
          }
          /* si touche le cote */
          else if (this->y == Y_MIN)
          {
             /* rebond a 90 degre */
             this->dir = SW;
             this->y++;
          }
          /* si touche le fond ouest : perdu */
          else if (this->x == X_MIN)
          {
             *end = 1;
          }
          break;
     
       case NE:
          this->x++;
          this->y--;
     
          if (this->x == X_MAX && this->y == Y_MIN)
          {
             this->dir = SW;
             this->x--;
             this->y++;
          }
          else if (this->x == X_MAX)
          {
             this->dir = NW;
             this->x--;
          }
          else if (this->y == Y_MIN)
          {
             this->dir = SE;
             this->y++;
          }
          break;
     
       case SE:
          this->x++;
          this->y++;
     
          if (this->x == X_MAX && this->y == Y_MAX)
          {
             this->dir = NW;
             this->x--;
             this->y--;
          }
          else if (this->x == X_MAX)
          {
             this->dir = SW;
             this->x--;
          }
          else if (this->y == Y_MAX)
          {
             this->dir = NE;
             this->y--;
          }
          break;
     
       case SW:
          this->x--;
          this->y++;
     
          /* Si il y a collision avec la raquette */
          if (this->x == p_plot->x && this->y == p_plot->y)
          {
             if (this->y == Y_MAX)
             {
                /* rebond a 180 degre */
                this->dir = NE;
                this->x++;
                this->y--;
             }
             else
             {
                /* rebond a 90 degre */
                this->dir = SE;
                this->x++;
             }
          }
          /* si touche le cote */
          else if (this->y == Y_MAX)
          {
             /* rebond a 90 degre */
             this->dir = NW;
             this->y--;
          }
          /* si touche le fond ouest : perdu */
          else if (this->x == X_MIN)
          {
             *end = 1;
          }
     
          break;
       }
       gotoxy (this->x, this->y), putch ('o');
    }
     
    static void move_plot (struct plot *this, int c)
    {
       switch (c)
       {
       case '2':
          if (this->y < Y_MAX - 1)
          {
             gotoxy (this->x, this->y), putch (' ');
             this->y++;
             gotoxy (this->x, this->y), putch ('|');
          }
          break;
     
       case '8':
          if (this->y > Y_MIN + 1)
          {
             gotoxy (this->x, this->y), putch (' ');
             this->y--;
             gotoxy (this->x, this->y), putch ('|');
          }
          break;
       }
    }
     
    int main (void)
    {
       struct ball ball = { 40, 15, NW };
       struct plot plot = { 19, 15 };
       clock_t echeance = clock () + MS (DELAY);
     
    #if 0
       titre ();
       terrain ();
    #endif
     
       /* positions initiales */
       gotoxy (ball.x, ball.y), putch ('o');
       gotoxy (plot.x, plot.y), putch ('|');
     
       /* mouvement */
       do
       {
          if (clock () > echeance)
          {
             move_ball (&ball, &plot, &end);
             echeance = clock () + MS (DELAY);
          }
          if (kbhit ())
          {
             int c = getch ();
             switch (c)
             {
             case '2':
             case '8':
                move_plot (&plot, c);
                break;
             case 27:
                end = 1;
             }
          }
     
          Sleep(1);
       }
       while (!end);
     
       return 0;
    }

  7. #27
    Membre du Club Avatar de Electroniktor
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 150
    Points : 55
    Points
    55
    Par défaut
    C'est vrai que je ne suis pas très fort au niveau algorythmie, mais cela va venir ... enfin j'espère !

    En tout cas, j'ai modifié un peu ton code car cela n'affichait pas le titre et le terrain, et j'ai fait deux trois autres changements sans importance !

    Je vais maintenant essayé de faire une raquette composée de deux | qui renvoit la balle dans différentes directions suivant la partie de la raquette touchée. Je vais aussi faire un compteur de coup qui augmente la vitesse de la balle tous les 5 coups. Cela me fera ainsi le mode entraînement de mon jeu.
    Je m'attaquerai ensuite à un mode deux joueurs (je ne pense que cela sera très difficile !) et un mode un joueur contre l'ordinateur (là ce sera un peu plus dur à cause de l'algorythme à faire !) où on pourra enregistrer le meilleur score !

    Et merci beaucoup pour le code !

  8. #28
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par Electroniktor
    C'est vrai que je ne suis pas très fort au niveau algorythmie, mais cela va venir ... enfin j'espère !
    Faut pas hésiter à faire des petits croquis, ça aide à réfléchir...
    Images attachées Images attachées   

  9. #29
    Membre du Club Avatar de Electroniktor
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 150
    Points : 55
    Points
    55
    Par défaut
    Oui j'ai fait un petit croquis du comportement que j'attend de la future raquette. Le voiçi. Je vais m'occuper de le coder demain.

  10. #30
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par frp31
    je vais probablement dire une betise ou etre tres incomplet
    mais pour faire plusieurs taches a la fois ne suffit t il pas de laisser le systeme gerer ca ? en creant un processus fils pour gerer la balle ?
    Bien sûr que c'est faisable, mais je préfèrerais qu'on utilise les pthreads qui sont plus portables, de même que je préfèrerais PDCurses à conio... Mais bon, le P.O. est parti comme ça, ce qui compte c'est l'algo. Le portage est un autre problème.

    Ne pas oublier non plus qu'il faut savoir détecter les collisions. Il faut donc pouvoir partager des données entre processus (balle, raquette...)... Compliqué. Là encore, les threads sont beaucoup plus simples.

  11. #31
    Membre du Club Avatar de Electroniktor
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 150
    Points : 55
    Points
    55
    Par défaut
    Euh ... c'est quoi cette bibliothèque PDCurses ?

  12. #32
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par Electroniktor
    Euh ... c'est quoi cette bibliothèque PDCurses ?
    Marche plus ton

    http://pdcurses.sourceforge.net/

  13. #33
    Membre du Club Avatar de Electroniktor
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 150
    Points : 55
    Points
    55
    Par défaut
    Si mais je n'ai trouvé que des pages en anglais et d'après ce que j'ai compris, cela ne fonctionne que sur les sytèmes UNIX !
    Je voulais donc savoir si cela était vrai vu que je suis sou windows !
    Sinon, si je peux l'utiliser sous windows, je pourrais faire plein de trucs car les quelques fonctions présentées que j'ai trouvé m'ont l'air très utiles pour faire des jeux en console comme j'aime faire (bien que la plupart se retrouvent dans conio !)

  14. #34
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par Electroniktor
    Si mais je n'ai trouvé que des pages en anglais et d'après ce que j'ai compris, cela ne fonctionne que sur les sytèmes UNIX !
    Bah, non, justement, PDCurses est portable, comme son nom l'indique... Mais on ne vas pas changer de bibliothèque maintenant. On verra plus tard comment le faire proprement.

  15. #35
    Membre du Club Avatar de Electroniktor
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 150
    Points : 55
    Points
    55
    Par défaut
    Bon ben voilà j'ai codé la raquette à trois parties qui renvoie la balle dans diférente directions selon la partie touchée (voir dessin).
    Et en plus, ca marche !!!
    Le problème est que j'ai voulu faire un compteur de coups et qu'il ne marche pas !
    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
    272
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio2.h>
    #include <windows.h>
     
    #include "affichage.h"
     
    #define MS(ms) (CLOCKS_PER_SEC / (1000.0 / (ms)))
    #define DELAY 250
     
    #define X_MIN  17
    #define X_MAX  65
    #define Y_MIN  6
    #define Y_MAX  22
     
    enum dir
    { NW, NE, SE, SW };
     
    struct ball
    {
       int x;
       int y;
       enum dir dir;
    };
     
    struct plot
    {
       int x;
       int y1;
       int y2;
       int y3;
    };
     
    int end = 0;
    long compteur = 0;
     
    static void move_ball (struct ball *this, struct plot *p_plot, int *end, long *compteur)
    {
       /* Si il n'y a pas collision avec la raquette */
       if (!(this->x == p_plot->x && this->y == p_plot->y1 || this->x == p_plot->x && this->y == p_plot->y2 || this->x == p_plot->x && this->y == p_plot->y3))
       {
          if (this->x == 41)
          {
              gotoxy (this->x, this->y), putch ('|');
          }
          else if (this->x == X_MIN + 1)
          {
              gotoxy (this->x, this->y), putch ('o');
          }
          else
          {
              gotoxy (this->x, this->y), putch (' ');
          }
       }
     
       switch (this->dir)
       {
       case NW:
          this->x--;
          this->y--;
     
          /* Si il y a collision avec la partie haute de la raquette */
          if (this->x == p_plot->x && this->y == p_plot->y1)
          {
                /* rebond a 90 degre */
                this->dir = NE;
                this->x++;
                *compteur++;
          }
          /* Si il y a collision avec la partie centrale de la raquette */
          else if (this->x == p_plot->x && this->y == p_plot->y2)
          {
               this->dir = SE;
               this->x++;
               this->y++;
               *compteur++;
          }
          /* Si il y a collision avec la partie basse de la raquette */
          else if (this->x == p_plot->x && this->y == p_plot->y3)
          {
               this->dir = SE;
               this->x++;
               this->y++;
               *compteur++;
          }
          /* si touche le cote */
          else if (this->y == Y_MIN)
          {
             /* rebond a 90 degre */
             this->dir = SW;
             this->y++;
          }
          /* si touche le fond ouest : perdu */
          else if (this->x == X_MIN)
          {
             getch();
             *end = 1;
          }
          break;
     
       case NE:
          this->x++;
          this->y--;
     
          if (this->x == X_MAX && this->y == Y_MIN)
          {
             this->dir = SW;
             this->x--;
             this->y++;
          }
          else if (this->x == X_MAX)
          {
             this->dir = NW;
             this->x--;
          }
          else if (this->y == Y_MIN)
          {
             this->dir = SE;
             this->y++;
          }
          break;
     
       case SE:
          this->x++;
          this->y++;
     
          if (this->x == X_MAX && this->y == Y_MAX)
          {
             this->dir = NW;
             this->x--;
             this->y--;
          }
          else if (this->x == X_MAX)
          {
             this->dir = SW;
             this->x--;
          }
          else if (this->y == Y_MAX)
          {
             this->dir = NE;
             this->y--;
          }
          break;
     
       case SW:
          this->x--;
          this->y++;
     
          /* Si il y a collision avec la partie haute de la raquette */
          if (this->x == p_plot->x && this->y == p_plot->y1)
          {   
                this->dir = NE;
                this->x++;
                this->y--;
                *compteur++;
          }
          /* Si il y a collision avec la partie centrale de la raquette */
          else if(this->x == p_plot->x && this->y == p_plot->y2)
          {
                this->dir = NE;
                this->x++;
                this->y--;
                *compteur++;
          }
          /* Si il y a collision avec la partie basse de la raquette */
          else if (this->x == p_plot->x && this->y == p_plot->y3)
          {
               this->dir = SE;
               this->x++;
               *compteur++;
          }
     
          /* si touche le cote */
          else if (this->y == Y_MAX)
          {
             /* rebond a 90 degre */
             this->dir = NW;
             this->y--;
          }
          /* si touche le fond ouest : perdu */
          else if (this->x == X_MIN)
          {
             getch();
             *end = 1;
          }
     
          break;
       }
       gotoxy (this->x, this->y), putch ('o');
    }
     
    static void move_plot (struct plot *this, int c)
    {
       switch (c)
       {
       case '2':
          if (this->y3 < Y_MAX - 1)
          {
             this->y1++;
             gotoxy( this->x, this->y1), putch('|');
             this->y2++;
             gotoxy( this->x, this->y2), putch('|');
             this->y3++;
             gotoxy( this->x, this->y3), putch('|');
     
             gotoxy( this->x, this->y1 - 1), putch(' ');
          } 
          break;
     
       case '8':
          if (this->y1 > Y_MIN + 1)
          {
             this->y1--;
             gotoxy( this->x, this->y1), putch('|');
             this->y2--;
             gotoxy( this->x, this->y2), putch('|');
             this->y3--;
             gotoxy( this->x, this->y3), putch('|');
     
             gotoxy( this->x, this->y3 + 1), putch(' ');
          }
          break;
       }
    }
     
    int main (void)
    {
       struct ball ball = { 40, 15, NW };
       struct plot plot = { 19, 14, 15, 16 };
       clock_t echeance = clock () + MS (DELAY);
     
       titre ();
       terrain ();
     
       /* positions initiales */
       gotoxy (ball.x, ball.y), putch ('o');
       gotoxy (plot.x, plot.y1), putch ('|');
       gotoxy (plot.x, plot.y2), putch ('|');
       gotoxy (plot.x, plot.y3), putch ('|');
     
       /* mouvement */
       do
       {
          if (clock () > echeance)
          {
             move_ball (&ball, &plot, &end, &compteur);
             echeance = clock () + MS (DELAY);
          }
          if (kbhit ())
          {
             int c = getch ();
             switch (c)
             {
             case '2':
             case '8':
                move_plot (&plot, c);
                break;
             case 27:
                end = 1;
             }
          }
          gotoxy( 1, 1);
          printf("%ld", compteur);
     
          Sleep(1);
       }
       while (!end);
     
       return 0;
     
    }

  16. #36
    Membre du Club Avatar de Electroniktor
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 150
    Points : 55
    Points
    55
    Par défaut
    Il vaudrait peut-être mieux que je fasse une fonction :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    void vitesse (struct plot *this, struct move_ball *that, *compteur, *DELAY)
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    */ en remplacant */ 
    #define DELAY 250 
    */ par */
    long DELAY = 250;
    Cette fonction comptera le nombre de coups en testant : et en changeant la valeur de DELAY (= vitesse) tous les 5 coups selon l'échelle suivante :
    • 0 --> 250
    • 5 --> 200
    • 10 --> 150
    • 15 --> 125
    • 20 --> 100
    • 25 --> 90
    • 30 --> 80
    • 35 --> 75
    • 40 --> 70
    • 45 --> 65
    • 50 --> 60
    • .....

  17. #37
    Membre du Club Avatar de Electroniktor
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 150
    Points : 55
    Points
    55
    Par défaut
    Alors j'ai fait une fonction vitesse mais je ne suis pas allé plus loin car déjà le compteur de coups ne marche pas !
    Ensuite, pour règler la vitesse selon le nombre de coups, je pensais faire un switch sur compteur mais je me demandais, si je fais ...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    case 5 :
    DELAY = 200;
    break;
    case 10 :
    *DELAY = 150;
    break;
    ... est ce que lorsque compteur est égal à 7, DELAY reste égal à 200 ? (j'ai la honte là ! )
    Voiçi le début de ma fonction :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    static void vitesse (struct ball *this, struct plot *p_plot, long *compteur, long *DELAY)
    {
       if (this->x == p_plot->x && this->y == p_plot->y1 || this->x == p_plot->x && this->y == p_plot->y2 || this->x == p_plot->x && this->y == p_plot->y3)
       {
          *compteur++;
          gotoxy( 1, 1);
          printf("%ld", *compteur);
       }
    }

  18. #38
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par Electroniktor
    Alors j'ai fait une fonction vitesse mais je ne suis pas allé plus loin car déjà le compteur de coups ne marche pas !
    <...>
    Voiçi le début de ma fonction :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    static void vitesse (struct ball *this, struct plot *p_plot, long *compteur, long *DELAY)
    {
       if (this->x == p_plot->x && this->y == p_plot->y1 || this->x == p_plot->x && this->y == p_plot->y2 || this->x == p_plot->x && this->y == p_plot->y3)
       {
          *compteur++;
    Ne fait pas ce que tu crois. :
    C'est la valeur pointée que tu veux modifier et non le pointeur :

  19. #39
    Membre du Club Avatar de Electroniktor
    Profil pro
    Inscrit en
    Juin 2007
    Messages
    150
    Détails du profil
    Informations personnelles :
    Âge : 31
    Localisation : France

    Informations forums :
    Inscription : Juin 2007
    Messages : 150
    Points : 55
    Points
    55
    Par défaut
    Voilà mon code. Le compteur marche mais un peu trop : le compteur augmente pendant toute la durée où la balle touche la raquette, ce qui donne à la fin de mon prmier coup un score de 122 ! Tu remarqueras que j'ai changé les valeurs des define car j'ai changé les dimensions du terrain !
    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
    272
    273
    274
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <conio2.h>
    #include <windows.h>
     
    #include "affichage.h"
     
    #define MS(ms) (CLOCKS_PER_SEC / (1000.0 / (ms)))
     
    #define X_MIN  11
    #define X_MAX  71
    #define Y_MIN  6
    #define Y_MAX  22
     
    enum dir
    { NW, NE, SE, SW };
     
    struct ball
    {
       int x;
       int y;
       enum dir dir;
    };
     
    struct plot
    {
       int x;
       int y1;
       int y2;
       int y3;
    };
     
    int end = 0;
    long compteur = 0;
    long DELAY = 250;
     
    static void vitesse (struct ball *this, struct plot *p_plot, long *compteur, long *DELAY)
    {
       if (this->x == p_plot->x +1 && this->y == p_plot->y1 || this->x == p_plot->x +1 && this->y == p_plot->y2 || this->x == p_plot->x +1 && this->y == p_plot->y3)
       {
          (*compteur)++;
          gotoxy( 1, 1);
          printf("%ld", *compteur);
       }
    }
     
    static void move_ball (struct ball *this, struct plot *p_plot, int *end)
    {
       /* Si il n'y a pas collision avec la raquette */
       if (!(this->x == p_plot->x && this->y == p_plot->y1 || this->x == p_plot->x && this->y == p_plot->y2 || this->x == p_plot->x && this->y == p_plot->y3))
       {
          if (this->x == 41)
          {
              gotoxy (this->x, this->y), putch ('|');
          }
          else if (this->x == X_MIN + 1)
          {
              gotoxy (this->x, this->y), putch ('o');
          }
          else
          {
              gotoxy (this->x, this->y), putch (' ');
          }
       }
     
       switch (this->dir)
       {
       case NW:
          this->x--;
          this->y--;
     
          /* Si il y a collision avec la partie haute de la raquette */
          if (this->x == p_plot->x && this->y == p_plot->y1)
          {
                /* rebond a 90 degre */
                this->dir = NE;
                this->x++;
          }
          /* Si il y a collision avec la partie centrale de la raquette */
          else if (this->x == p_plot->x && this->y == p_plot->y2)
          {
               this->dir = SE;
               this->x++;
               this->y++;
          }
          /* Si il y a collision avec la partie basse de la raquette */
          else if (this->x == p_plot->x && this->y == p_plot->y3)
          {
               this->dir = SE;
               this->x++;
               this->y++;
          }
          /* si touche le cote */
          else if (this->y == Y_MIN)
          {
             /* rebond a 90 degre */
             this->dir = SW;
             this->y++;
          }
          /* si touche le fond ouest : perdu */
          else if (this->x == X_MIN)
          {
             getch();
             *end = 1;
          }
          break;
     
       case NE:
          this->x++;
          this->y--;
     
          if (this->x == X_MAX && this->y == Y_MIN)
          {
             this->dir = SW;
             this->x--;
             this->y++;
          }
          else if (this->x == X_MAX)
          {
             this->dir = NW;
             this->x--;
          }
          else if (this->y == Y_MIN)
          {
             this->dir = SE;
             this->y++;
          }
          break;
     
       case SE:
          this->x++;
          this->y++;
     
          if (this->x == X_MAX && this->y == Y_MAX)
          {
             this->dir = NW;
             this->x--;
             this->y--;
          }
          else if (this->x == X_MAX)
          {
             this->dir = SW;
             this->x--;
          }
          else if (this->y == Y_MAX)
          {
             this->dir = NE;
             this->y--;
          }
          break;
     
       case SW:
          this->x--;
          this->y++;
     
          /* Si il y a collision avec la partie haute de la raquette */
          if (this->x == p_plot->x && this->y == p_plot->y1)
          {
                this->dir = NE;
                this->x++;
                this->y--;
          }
          /* Si il y a collision avec la partie centrale de la raquette */
          else if(this->x == p_plot->x && this->y == p_plot->y2)
          {
                this->dir = NE;
                this->x++;
                this->y--;
          }
          /* Si il y a collision avec la partie basse de la raquette */
          else if (this->x == p_plot->x && this->y == p_plot->y3)
          {
               this->dir = SE;
               this->x++;
          }
     
          /* si touche le cote */
          else if (this->y == Y_MAX)
          {
             /* rebond a 90 degre */
             this->dir = NW;
             this->y--;
          }
          /* si touche le fond ouest : perdu */
          else if (this->x == X_MIN)
          {
             getch();
             *end = 1;
          }
     
          break;
       }
       gotoxy (this->x, this->y), putch ('o');
    }
     
    static void move_plot (struct plot *this, int c)
    {
       switch (c)
       {
       case '2':
          if (this->y3 < Y_MAX - 1)
          {
             this->y1++;
             gotoxy( this->x, this->y1), putch('|');
             this->y2++;
             gotoxy( this->x, this->y2), putch('|');
             this->y3++;
             gotoxy( this->x, this->y3), putch('|');
     
             gotoxy( this->x, this->y1 - 1), putch(' ');
          }
          break;
     
       case '8':
          if (this->y1 > Y_MIN + 1)
          {
             this->y1--;
             gotoxy( this->x, this->y1), putch('|');
             this->y2--;
             gotoxy( this->x, this->y2), putch('|');
             this->y3--;
             gotoxy( this->x, this->y3), putch('|');
     
             gotoxy( this->x, this->y3 + 1), putch(' ');
          }
          break;
       }
    }
     
    int main (void)
    {
       struct ball ball = { 40, 15, NW };
       struct plot plot = { 13, 14, 15, 16 };
       clock_t echeance = clock () + MS (DELAY);
     
       titre ();
       terrain ();
     
       /* positions initiales */
       gotoxy (ball.x, ball.y), putch ('o');
       gotoxy (plot.x, plot.y1), putch ('|');
       gotoxy (plot.x, plot.y2), putch ('|');
       gotoxy (plot.x, plot.y3), putch ('|');
     
       /* mouvement */
       do
       {
          if (clock () > echeance)
          {
             move_ball (&ball, &plot, &end);
             echeance = clock () + MS (DELAY);
          }
          if (kbhit ())
          {
             int c = getch ();
             switch (c)
             {
             case '2':
             case '8':
                move_plot (&plot, c);
                break;
             case 27:
                end = 1;
             }
          }
          vitesse (&ball, &plot, &compteur, &DELAY);
          Sleep(1);
       }
       while (!end);
     
       return 0;
     
    }

  20. #40
    Expert éminent sénior
    Avatar de Emmanuel Delahaye
    Profil pro
    Retraité
    Inscrit en
    Décembre 2003
    Messages
    14 512
    Détails du profil
    Informations personnelles :
    Âge : 67
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Retraité

    Informations forums :
    Inscription : Décembre 2003
    Messages : 14 512
    Points : 20 985
    Points
    20 985
    Par défaut
    Citation Envoyé par Electroniktor
    Voilà mon code. Le compteur marche mais un peu trop : le compteur augmente pendant toute la durée où la balle touche la raquette, ce qui donne à la fin de mon prmier coup un score de 122 !
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    static void vitesse (struct ball *this, struct plot *p_plot, long *compteur, long *DELAY)
    {
       if (this->x == p_plot->x +1 && this->y == p_plot->y1 || this->x == p_plot->x +1 && this->y == p_plot->y2 || this->x == p_plot->x +1 && this->y == p_plot->y3)
    Il y a probablement une erreur avec cette expression. Si tu n'es pas certain de la priorité des opérateurs, mets des parenthèses. Je suppose que tu veux :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
       if ((this->x == p_plot->x +1 && this->y == p_plot->y1) 
        || (this->x == p_plot->x +1 && this->y == p_plot->y2)
        || (this->x == p_plot->x +1 && this->y == p_plot->y3))
    et tu remarqueras la différence de présentation, qui permet un contrôle visuel immédiat de la 'régularité' du code...

    D'ailleurs, on voit que 'this->x == p_plot->x +1' peut être mis en facteur :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
       if (this->x == p_plot->x +1 
       && (this->y == p_plot->y1 
        || this->y == p_plot->y2
        || this->y == p_plot->y3))

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 4 PremièrePremière 1234 DernièreDernière

Discussions similaires

  1. Problème avec un jeu
    Par Alex' dans le forum Windows XP
    Réponses: 15
    Dernier message: 25/09/2009, 08h41
  2. [SDL et C] Problème avec mon jeu de la vie
    Par _SamSoft_ dans le forum Développement 2D, 3D et Jeux
    Réponses: 4
    Dernier message: 24/05/2008, 09h43
  3. [Installation]Problème avec le jeu de caractère NLS
    Par Benzeghiba dans le forum Oracle
    Réponses: 2
    Dernier message: 27/12/2006, 08h35
  4. Problème avec le jeu : "la bataille"
    Par rouliane dans le forum C++
    Réponses: 5
    Dernier message: 21/12/2006, 12h36
  5. Problème avec un jeu
    Par CCin dans le forum C
    Réponses: 6
    Dernier message: 25/10/2005, 18h10

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