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

Macros et VBA Excel Discussion :

Jeu VBA Problème


Sujet :

Macros et VBA Excel

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 22
    Par défaut Jeu VBA Problème
    Bonsoir !
    Je suis en train de réalisé un jeu awalé sur vba avec deux options: 1 joueur contre le pc et l'autre option : deux joueurs.
    J'ai un soucis c'est que je suis pas trés bon j'ai fait tout ce qui était mis en forme mais j'ai un problème sur ma boucle.
    Je pense qu'il y a plus simple et plus rapide ça va faire quelques temps que je suis dessus. Voici mon code ; j'ai 12 commandbutton et ça doit s'incrémenter automatiquement pour les deux joueurs chacun leur tour.

    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
    Private Sub CommandButton1_Click()
        If TextBox1.Value = 4 Then
    TextBox1.Value = 0
    TextBox2.Value = TextBox2 + 1
    TextBox3.Value = TextBox3 + 1
    TextBox4.Value = TextBox4 + 1
    TextBox5.Value = TextBox5 + 1
     
        If TextBox1.Value = 5 Then
    TextBox1.Value = 0
    TextBox2.Value = TextBox2 + 1
    TextBox3.Value = TextBox3 + 1
    TextBox4.Value = TextBox4 + 1
    TextBox5.Value = TextBox5 + 1
    TextBox6.Value = TextBox6 + 1
     
     If TextBox1.Value = 6 Then
    TextBox1.Value = 0
    TextBox2.Value = TextBox2 + 1
    TextBox3.Value = TextBox3 + 1
    TextBox4.Value = TextBox4 + 1
    TextBox5.Value = TextBox5 + 1
    TextBox6.Value = TextBox6 + 1
    TextBox7.Value = TextBox7 + 1
     
    End If
     
    End Sub
    Merci de votre aide d'avance (si vous souhaitez je peux l'envoyer car à expliquer c'est un peu compliquer). Merci

  2. #2
    Inactif  

    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    4 555
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 4 555
    Par défaut
    Bonjour,

    Juste une indication, dont j'espère que tu sauras profiter astucieusement :

    Me.controls("toto" & x) se refère aucontrôle nommé "totox"
    et je vois que, par exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    If TextBox1.Value = 4 Then
    TextBox1.Value = 0
    TextBox2.Value = TextBox2 + 1
    TextBox3.Value = TextBox3 + 1
    TextBox4.Value = TextBox4 + 1
    TextBox5.Value = TextBox5 + 1
    tu vas jusqu'à me.controls("Textbox" & n + 1) lorsque Me.Controls("TextBox" & "1").value = n
    A toi de jouer...

  3. #3
    Membre émérite
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    753
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 753
    Par défaut
    Petite précision: ce mode opératoire suppose que tu ne fais plus de tests et que tu fais une boucle allant de 1 à n+1. Ca paraît évident pour nous, mais ça l'est peut-être moin pour l'auteur de post, donc je pense qu'il n'est pas mauvais de le dire explicitement.

  4. #4
    Inactif  

    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    4 555
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 4 555
    Par défaut
    Ah...
    Plus explicitement ?
    D'accord :
    Voilà par quoi le code qu'il nous a montré peut être remplacé :

    (à main levée) :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Private Sub CommandButton1_Click()
        For i = 4 To 6
          traitons i
        Next
    End Sub
     
    Private Sub traitons(num As Integer)
      If Textbox1.Value = num Then
        Textbox1.Value = 0
        For i = 2 To num + 1
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
        Next
    End Sub
    L'exemple me parait clair. Le mécanisme devrait alors être compris et il devrait pouvoir s'en inspirer et l'appliquer partout où il lui paraîtra utile et plus sage de le faire.
    Le reste n'est que de la gymnastique décisionnelle...

  5. #5
    Membre émérite
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    753
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 753
    Par défaut
    Oups: tu n'as pas bien compris ce que je voulais dire.

    Je voulais juste dire qu'il fallait lui dire explicitement (comme je l'ai fait) qu'il fallait se servir d'une boucle. Mais bon si tu veux tu peux encore enlever ton code pour le faire réfléchir.

  6. #6
    Inactif  

    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    4 555
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 4 555
    Par défaut
    Non...
    Ce code lui permettra de mieux comprendre
    et je suis presque certain de ce qu'il aura l'occasion de se servir ailleurs dans son appli de ce genre de mécanisme, en l'adaptant, à chaque fois, au contexte de son jeu.

  7. #7
    Membre averti
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 22
    Par défaut Jeu vba
    Bonsoir !

    Tout d'abord, merci de m'avoir répondu. J'ai testé le code que vous m'avez donné et ça pêche sur deux points :
    "traitons i" il me met type d'argument byref incompatible. Puis moi même je comprends pas pourquoi vous avez utilisez ça ?
    et le deuxième :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
    à quoi correspond ?
    J'essais de comprendre mais sinon le reste je pense que j'ai compris le reste.

    Merci Merci

  8. #8
    Inactif  

    Profil pro
    Inscrit en
    Juillet 2007
    Messages
    4 555
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2007
    Messages : 4 555
    Par défaut
    Bon,

    1) mets donc tout simplement dim i as integer avant chaque boucle for i ...
    2) ouvre ton aide en ligne (sur ta machine) sur le mot Controls (collection Controls)... Il n'est pas possible de transformer cette discussion en cours complet sur chaque point, y compris lorsqu'il est exposé dans l'aide en ligne...
    Désolé ...

  9. #9
    Membre averti
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 22
    Par défaut
    merci pour tes conseils. ça fonctionne à peu près. Voici mon code entier :
    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
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    Private Sub UserForm_Initialize()
    'For i = 0 To 28
    'Controls(i).Enabled = True
     '   Next i
     
     
     
        TextBox1.Value = 4
        TextBox2.Value = 4
        TextBox3.Value = 4
        TextBox4.Value = 4
        TextBox5.Value = 4
        TextBox6.Value = 4
        TextBox7.Value = 4
        TextBox8.Value = 4
        TextBox9.Value = 4
        TextBox10.Value = 4
        TextBox11.Value = 4
        TextBox12.Value = 4
     
    'For i = 17 To 22
    'Controls(i).Enabled = False
    '    Next i
    End Sub
     
     
    Private Sub TextBox1_Change()
        b = ActiveWorkbook.Path
        a = b & "\" & TextBox1.Value & ".jpg "
        CommandButton1.Picture = LoadPicture(a)
     
    End Sub
    Private Sub TextBox10_Change()
        b = ActiveWorkbook.Path
        a = b & "\" & TextBox10.Value & ".jpg "
        CommandButton10.Picture = LoadPicture(a)
     
    End Sub
    Private Sub TextBox11_Change()
        b = ActiveWorkbook.Path
        a = b & "\" & TextBox11.Value & ".jpg "
        CommandButton11.Picture = LoadPicture(a)
     
    End Sub
    Private Sub TextBox12_Change()
        b = ActiveWorkbook.Path
        a = b & "\" & TextBox12.Value & ".jpg "
        CommandButton12.Picture = LoadPicture(a)
     
    End Sub
    Private Sub TextBox2_Change()
        b = ActiveWorkbook.Path
        a = b & "\" & TextBox2.Value & ".jpg "
        CommandButton2.Picture = LoadPicture(a)
     
    End Sub
    Private Sub TextBox3_Change()
        b = ActiveWorkbook.Path
        a = b & "\" & TextBox3.Value & ".jpg "
        CommandButton3.Picture = LoadPicture(a)
     
    End Sub
    Private Sub TextBox4_Change()
        b = ActiveWorkbook.Path
        a = b & "\" & TextBox4.Value & ".jpg "
        CommandButton4.Picture = LoadPicture(a)
     
    End Sub
    Private Sub TextBox5_Change()
        b = ActiveWorkbook.Path
        a = b & "\" & TextBox5.Value & ".jpg "
        CommandButton5.Picture = LoadPicture(a)
     
    End Sub
    Private Sub TextBox6_Change()
        b = ActiveWorkbook.Path
        a = b & "\" & TextBox6.Value & ".jpg "
        CommandButton6.Picture = LoadPicture(a)
     
    End Sub
    Private Sub TextBox7_Change()
        b = ActiveWorkbook.Path
        a = b & "\" & TextBox7.Value & ".jpg "
        CommandButton7.Picture = LoadPicture(a)
     
    End Sub
    Private Sub TextBox8_Change()
        b = ActiveWorkbook.Path
        a = b & "\" & TextBox8.Value & ".jpg "
        CommandButton8.Picture = LoadPicture(a)
     
    End Sub
    Private Sub TextBox9_Change()
        b = ActiveWorkbook.Path
        a = b & "\" & TextBox9.Value & ".jpg "
        CommandButton9.Picture = LoadPicture(a)
     
    End Sub
     
    Private Sub CommandButton1_Click()
       Dim i As Integer
        For i = 1 To 30
          traitons i
        Next
    End Sub
     
    Private Sub traitons(num As Integer)
      If TextBox1.Value = num Then
        TextBox1.Value = 0
        For i = 2 To num + 1
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
        Next
        End If
    End Sub
     
    Private Sub CommandButton2_Click()
       Dim i As Integer
        For i = 1 To 30
          traitons2 i
        Next
    End Sub
     
    Private Sub traitons2(num As Integer)
     
      If TextBox2.Value = num Then
      TextBox2.Value = 0
        For i = 3 To num + 1
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
       Next
        End If
    End Sub
     
    Private Sub CommandButton3_Click()
       Dim i As Integer
        For i = 1 To 30
          traitons3 i
        Next
    End Sub
     
    Private Sub traitons3(num As Integer)
     
      If TextBox3.Value = num Then
      TextBox3.Value = 0
        For i = 4 To num + 3
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
       Next
        End If
    End Sub
     
    Private Sub CommandButton4_Click()
       Dim i As Integer
        For i = 1 To 30
          traitons4 i
        Next
    End Sub
     
    Private Sub traitons4(num As Integer)
     
      If TextBox4.Value = num Then
      TextBox4.Value = 0
        For i = 5 To num + 1
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
       Next
        End If
    End Sub
     
    Private Sub CommandButton5_Click()
       Dim i As Integer
        For i = 1 To 30
          traitons5 i
        Next
    End Sub
     
    Private Sub traitons5(num As Integer)
     
      If TextBox5.Value = num Then
      TextBox5.Value = 0
        For i = 6 To num + 1
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
       Next
        End If
    End Sub
     
    Private Sub CommandButton6_Click()
       Dim i As Integer
        For i = 1 To 30
          traitons6 i
        Next
    End Sub
     
    Private Sub traitons6(num As Integer)
     
      If TextBox6.Value = num Then
      TextBox6.Value = 0
        For i = 7 To num + 1
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
       Next
        End If
    End Sub
     
    Private Sub CommandButton7_Click()
       Dim i As Integer
        For i = 1 To 30
          traitons7 i
        Next
    End Sub
     
    Private Sub traitons7(num As Integer)
     
      If TextBox7.Value = num Then
      TextBox7.Value = 0
        For i = 8 To num + 1
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
       Next
        End If
    End Sub
     
    Private Sub CommandButton8_Click()
       Dim i As Integer
        For i = 1 To 30
          traitons8 i
        Next
    End Sub
     
    Private Sub traitons8(num As Integer)
     
      If TextBox8.Value = num Then
      TextBox8.Value = 0
        For i = 9 To num + 1
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
       Next
        End If
    End Sub
     
    Private Sub CommandButton9_Click()
       Dim i As Integer
        For i = 1 To 30
          traitons9 i
        Next
    End Sub
     
    Private Sub traitons9(num As Integer)
     
      If TextBox9.Value = num Then
      TextBox9.Value = 0
        For i = 10 To num + 1
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
       Next
        End If
    End Sub
     
    Private Sub CommandButton10_Click()
       Dim i As Integer
        For i = 1 To 30
          traitons10 i
        Next
    End Sub
     
    Private Sub traitons10(num As Integer)
     
      If TextBox10.Value = num Then
      TextBox10.Value = 0
        For i = 11 To num + 1
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
       Next
        End If
    End Sub
     
    Private Sub CommandButton11_Click()
       Dim i As Integer
        For i = 1 To 30
          traitons11 i
        Next
    End Sub
     
    Private Sub traitons11(num As Integer)
     
      If TextBox11.Value = num Then
      TextBox11.Value = 0
        For i = 12 To num + 1
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
       Next
        End If
    End Sub
     
    Private Sub CommandButton12_Click()
       Dim i As Integer
        For i = 1 To 30
          traitons12 i
        Next
    End Sub
     
    Private Sub traitons12(num As Integer)
     
      If TextBox12.Value = num Then
      TextBox12.Value = 0
        For i = 13 To num + 12
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
       Next
        End If
    End Sub
    Mes deux questions sont :

    Pourquoi quand je clique sur le deuxième, 3ème etc... cmdbutton ça n'incrémente pas les 4 images suivantes ? Si je mets pour le 2ème cmdbutton :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Private Sub traitons2(num As Integer)
     
      If TextBox2.Value = num Then
      TextBox2.Value = 0
        For i = 3 To num + 1 (fici pour que ça fonctionne bien faudrait que je mette for i =4 mais ça fonctionne pas dans tous les cas)
          Me.Controls("TextBox" & i).Value = Me.Controls("TextBox" & i).Value + 1
       Next
        End If
    End Sub
    Pourquoi sur les dernièrs cmdbutton 11 et 12 ça n'incrémente pas le 1er, 2ème etc... que ça fasse pas le tour ? ça fait le tour dans un sens mais pas dans l'autre.

    Merci d'avance

  10. #10
    Membre chevronné
    Profil pro
    Inscrit en
    Février 2007
    Messages
    491
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2007
    Messages : 491

  11. #11
    Membre averti
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    22
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 22
    Par défaut
    merci de votre aide ça a résolu quelques points

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. problème avec l'apostrophe dans une requête
    Par mika0102 dans le forum VBA Access
    Réponses: 7
    Dernier message: 09/03/2019, 16h51
  2. [DEBUTANT - VBA] Problèmes de ComboBox
    Par _Maniak dans le forum Général VBA
    Réponses: 13
    Dernier message: 10/03/2006, 10h02
  3. Réponses: 2
    Dernier message: 17/10/2005, 14h58
  4. [Excel - VBA] Problème de suppression de lignes...
    Par beholder2 dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 28/01/2005, 17h27
  5. [Requete SQL en VBA] Problème avec la fonction FLOOR
    Par zubral dans le forum Langage SQL
    Réponses: 4
    Dernier message: 13/07/2004, 13h24

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