Problème dans mon code VBA

Fermé
CB - 9 avril 2023 à 23:45
f894009 Messages postés 17240 Date d'inscription dimanche 25 novembre 2007 Statut Membre Dernière intervention 18 février 2025 - 10 avril 2023 à 07:27

Bonjour,

j'ai un projet à réaliser qui consiste à créer un jeu sur les feuilles de calcul Excel à l'aide de la programmation VBA.

Je réalise actuellement un Snake, cependant je rencontre plusieurs problèmes.

Tout d'abord lorsque mon serpent mange une pomme rouge, toutes les cellules affichent automatiquement un chiffre ou un # alors que j'aimerais qu'aucun caractère ne s'affiche (voir photo).

Enfin, j'aimerais pouvoir intégrer une variable qui réinitialise le jeu.

ci-joint mon code VBA ainsi qu'une capture d'écran de ma feuille Excel.

Merci d'avance pour votre aide

cordialement

CB

Sub gauche()
Dim i, j, TV, TH, p As Long
    
For i = 2 To 19
For j = 2 To 29
        If Cells(i, j) = "O" Then
        TV = i
        TH = j
        End If
Next j
Next i
    
' le serpent prend un mur
' cad le case a gauche est noire
    
If Cells(TV, TH - 1).Interior.ColorIndex = 1 Then
Exit Sub
End If
    
'si le serpent se mord la queue
'cad si la cellule est verte

If Cells(TV, TH - 1).Interior.ColorIndex = 4 Then
MsgBox ("c'est perdu !")
End If
    
' la cellule est vise et le serpent se déplace
' cad la cellule a gauche du serpent est blanche
    
If Cells(TV, TH - 1).Interior.ColorIndex = 2 Then
    
        ' le serpent n'a encore rien mangé
        
        If Cells(1, 40) = 0 Then
        Cells(TV, TH - 1).Interior.ColorIndex = 4
        Cells(TV, TH - 1) = "O"
        Cells(TV, TH - 1).Font.ColorIndex = 1
        Cells(TV, TH).Interior.ColorIndex = 2
        Cells(TV, TH).ClearContents
        End If
        
        ' le serpent a déjà mangé
        If Cells(1, 40) > 0 Then
        
        ' la tete se déplace vers la gauche
        Cells(TV, TH - 1).Interior.ColorIndex = 4
        Cells(TV, TH - 1) = "O"
        Cells(TV, TH - 1).Font.ColorIndex = 1
        
        'on diminue tous les chiffres de 1
            ' si on obtient 0 => on supprime
            For i = 2 To 19
            For j = 2 To 29
                
                If Cells(i, j) <> "O" And Cells(i, j) <> " " Then
                Cells(i, j) = Cells(i, j) - 1
                If Cells(i, j) = 0 Then
                Cells(i, j).Interior.ColorIndex = 2
                Cells(i, j).ClearContents
                End If
                End If
            
            Next j
            Next i
        
        
        ' l'ancienne tete prend la valeur du score
        Cells(TV, TH) = Cells(1, 40)
        
        End If
        
    
    
    End If
'fin de l'hypothèse le serpent se déplace

'si la cellule est rouge, cad que le serpent mange une pomme
If Cells(TV, TH - 1).Interior.ColorIndex = 3 Then
'gestion de la nouvelle cellule
Cells(TV, TH - 1).Interior.ColorIndex = 4
Cells(TV, TH - 1) = "O"
Cells(TV, TH - 1).Font.ColorIndex = 1
'gestion de l'ancienne tete
Cells(1, 40) = Cells(1, 40) + 1
Cells(TV, TH) = Cells(1, 40)
End If
' génération pomme
i = Int(Rnd * 19) + 1
j = Int(Rnd * 29) + 1
p = Int(Rnd * 99) + 1

If p > 60 Then
If Cells(i, j).Interior.ColorIndex = 2 Then
Cells(i, j).Interior.ColorIndex = 3
End If
End If

End Sub
Sub droite()
Dim i, j, TV, TH, p As Long
    
For i = 2 To 19
For j = 2 To 29
        If Cells(i, j) = "O" Then
        TV = i
        TH = j
        End If
Next j
Next i
    
' le serpent prend un mur
' cad le case a gauche est noire
    
If Cells(TV, TH + 1).Interior.ColorIndex = 1 Then
Exit Sub
End If
    
'si le serpent se mord la queue
'cad si la cellule est verte

If Cells(TV, TH + 1).Interior.ColorIndex = 4 Then
MsgBox ("c'est perdu !")
End If
    
' la cellule est vise et le serpent se déplace
' cad la cellule a gauche du serpent est blanche
    
If Cells(TV, TH + 1).Interior.ColorIndex = 2 Then
    
        ' le serpent n'a encore rien mangé
        
        If Cells(1, 40) = 0 Then
        Cells(TV, TH + 1).Interior.ColorIndex = 4
        Cells(TV, TH + 1) = "O"
        Cells(TV, TH + 1).Font.ColorIndex = 1
        Cells(TV, TH).Interior.ColorIndex = 2
        Cells(TV, TH).ClearContents
        End If
        
        ' le serpent a déjà mangé
        If Cells(1, 40) > 0 Then
        
        ' la tete se déplace vers la gauche
        Cells(TV, TH + 1).Interior.ColorIndex = 4
        Cells(TV, TH + 1) = "O"
        Cells(TV, TH + 1).Font.ColorIndex = 1
        
        'on diminue tous les chiffres de 1
            ' si on obtient 0 => on supprime
            For i = 2 To 19
            For j = 2 To 29
                
                If Cells(i, j) <> "O" And Cells(i, j) <> " " Then
                Cells(i, j) = Cells(i, j) - 1
                If Cells(i, j) = 0 Then
                Cells(i, j).Interior.ColorIndex = 2
                Cells(i, j).ClearContents
                End If
                End If
            
            Next j
            Next i
        
        
        ' l'ancienne tete prend la valeur du score
        Cells(TV, TH) = Cells(1, 40)
        
        End If
        
    
    
    End If
'fin de l'hypothèse le serpent se déplace

'si la cellule est rouge, cad que le serpent mange une pomme
If Cells(TV, TH + 1).Interior.ColorIndex = 3 Then
'gestion de la nouvelle cellule
Cells(TV, TH + 1).Interior.ColorIndex = 4
Cells(TV, TH + 1) = "O"
Cells(TV, TH + 1).Font.ColorIndex = 1
'gestion de l'ancienne tete
Cells(1, 40) = Cells(1, 40) + 1
Cells(TV, TH) = Cells(1, 40)
End If
' génération pomme
i = Int(Rnd * 19) + 1
j = Int(Rnd * 29) + 1
p = Int(Rnd * 99) + 1

If p > 60 Then
If Cells(i, j).Interior.ColorIndex = 2 Then
Cells(i, j).Interior.ColorIndex = 3
End If
End If


End Sub
Sub haut()
Dim i, j, TV, TH, p As Long
    
For i = 2 To 19
For j = 2 To 29
        If Cells(i, j) = "O" Then
        TV = i
        TH = j
        End If
Next j
Next i
    
' le serpent prend un mur
' cad le case a gauche est noire
    
If Cells(TV - 1, TH).Interior.ColorIndex = 1 Then
Exit Sub
End If
    
'si le serpent se mord la queue
'cad si la cellule est verte

If Cells(TV - 1, TH).Interior.ColorIndex = 4 Then
MsgBox ("c'est perdu !")
End If
    
' la cellule est vise et le serpent se déplace
' cad la cellule a gauche du serpent est blanche
    
If Cells(TV - 1, TH).Interior.ColorIndex = 2 Then
    
        ' le serpent n'a encore rien mangé
        
        If Cells(1, 40) = 0 Then
        Cells(TV - 1, TH).Interior.ColorIndex = 4
        Cells(TV - 1, TH) = "O"
        Cells(TV - 1, TH).Font.ColorIndex = 1
        Cells(TV, TH).Interior.ColorIndex = 2
        Cells(TV, TH).ClearContents
        End If
        
        ' le serpent a déjà mangé
        If Cells(1, 40) > 0 Then
        
        ' la tete se déplace vers la gauche
        Cells(TV - 1, TH).Interior.ColorIndex = 4
        Cells(TV - 1, TH) = "O"
        Cells(TV - 1, TH).Font.ColorIndex = 1
        
        'on diminue tous les chiffres de 1
            ' si on obtient 0 => on supprime
            For i = 2 To 19
            For j = 2 To 29
                
                If Cells(i, j) <> "O" And Cells(i, j) <> " " Then
                Cells(i, j) = Cells(i, j) - 1
                If Cells(i, j) = 0 Then
                Cells(i, j).Interior.ColorIndex = 2
                Cells(i, j).ClearContents
                End If
                End If
            
            Next j
            Next i
        
        
        ' l'ancienne tete prend la valeur du score
        Cells(TV, TH) = Cells(1, 40)
        
        End If
        
    
    
    End If
'fin de l'hypothèse le serpent se déplace

'si la cellule est rouge, cad que le serpent mange une pomme
If Cells(TV - 1, TH).Interior.ColorIndex = 3 Then
'gestion de la nouvelle cellule
Cells(TV - 1, TH).Interior.ColorIndex = 4
Cells(TV - 1, TH) = "O"
Cells(TV - 1, TH).Font.ColorIndex = 1
'gestion de l'ancienne tete
Cells(1, 40) = Cells(1, 40) + 1
Cells(TV, TH) = Cells(1, 40)
End If
' génération pomme
i = Int(Rnd * 19) + 1
j = Int(Rnd * 29) + 1
p = Int(Rnd * 99) + 1

If p > 60 Then
If Cells(i, j).Interior.ColorIndex = 2 Then
Cells(i, j).Interior.ColorIndex = 3
End If
End If


End Sub
Sub bas()
Dim i, j, TV, TH, p As Long
    
For i = 2 To 19
For j = 2 To 29
        If Cells(i, j) = "O" Then
        TV = i
        TH = j
        End If
Next j
Next i
    
' le serpent prend un mur
' cad le case a gauche est noire
    
If Cells(TV + 1, TH).Interior.ColorIndex = 1 Then
Exit Sub
End If
    
'si le serpent se mord la queue
'cad si la cellule est verte

If Cells(TV + 1, TH).Interior.ColorIndex = 4 Then
MsgBox ("c'est perdu !")
End If
    
' la cellule est vise et le serpent se déplace
' cad la cellule a gauche du serpent est blanche
    
If Cells(TV + 1, TH).Interior.ColorIndex = 2 Then
    
        ' le serpent n'a encore rien mangé
        
        If Cells(1, 40) = 0 Then
        Cells(TV + 1, TH).Interior.ColorIndex = 4
        Cells(TV + 1, TH) = "O"
        Cells(TV + 1, TH).Font.ColorIndex = 1
        Cells(TV, TH).Interior.ColorIndex = 2
        Cells(TV, TH).ClearContents
        End If
        
        ' le serpent a déjà mangé
        If Cells(1, 40) > 0 Then
        
        ' la tete se déplace vers la gauche
        Cells(TV + 1, TH).Interior.ColorIndex = 4
        Cells(TV + 1, TH) = "O"
        Cells(TV + 1, TH).Font.ColorIndex = 1
        
        'on diminue tous les chiffres de 1
            ' si on obtient 0 => on supprime
            For i = 2 To 19
            For j = 2 To 29
                
                If Cells(i, j) <> "O" And Cells(i, j) <> " " Then
                Cells(i, j) = Cells(i, j) - 1
                If Cells(i, j) = 0 Then
                Cells(i, j).Interior.ColorIndex = 2
                Cells(i, j).ClearContents
                End If
                End If
            
            Next j
            Next i
        
        
        ' l'ancienne tete prend la valeur du score
        Cells(TV, TH) = Cells(1, 40)
        
        End If
        
    
    
    End If
'fin de l'hypothèse le serpent se déplace

'si la cellule est rouge, cad que le serpent mange une pomme
If Cells(TV + 1, TH).Interior.ColorIndex = 3 Then
'gestion de la nouvelle cellule
Cells(TV + 1, TH).Interior.ColorIndex = 4
Cells(TV + 1, TH) = "O"
Cells(TV + 1, TH).Font.ColorIndex = 1
'gestion de l'ancienne tete
Cells(1, 40) = Cells(1, 40) + 1
Cells(TV, TH) = Cells(1, 40)
End If
' génération pomme
i = Int(Rnd * 19) + 1
j = Int(Rnd * 29) + 1
p = Int(Rnd * 99) + 1

If p > 60 Then
If Cells(i, j).Interior.ColorIndex = 2 Then
Cells(i, j).Interior.ColorIndex = 3
End If
End If

End Sub

Macintosh / Safari 16.3

A voir également:

1 réponse

f894009 Messages postés 17240 Date d'inscription dimanche 25 novembre 2007 Statut Membre Dernière intervention 18 février 2025 1 713
10 avril 2023 à 07:27

Bonjour,

A priori vos cellules ont des # car elles ne sont pas assez larges

Un fichier aurait ete preferable!

0